parent
8b6387737c
commit
d55773cdc5
@ -1,61 +1,61 @@
|
||||
use crate::structs::BlockType;
|
||||
use crate::config::GRID_WIDTH;
|
||||
use crate::structs::BlockType;
|
||||
use sfml::system::Vector2i;
|
||||
|
||||
pub struct Block<'a> {
|
||||
pub block_type: &'a BlockType,
|
||||
pub position: Vector2i,
|
||||
pub rotation_state: i32
|
||||
pub block_type: &'a BlockType,
|
||||
pub position: Vector2i,
|
||||
pub rotation_state: i32,
|
||||
}
|
||||
|
||||
impl<'a> Block<'a> {
|
||||
pub fn new(block_type: &'a BlockType, ) -> Self {
|
||||
Self {
|
||||
block_type,
|
||||
position: Vector2i::new(
|
||||
GRID_WIDTH as i32 / 2 - block_type.grid[0].len() as i32 / 2,
|
||||
0
|
||||
),
|
||||
rotation_state: 0
|
||||
}
|
||||
}
|
||||
pub fn new(block_type: &'a BlockType) -> Self {
|
||||
Self {
|
||||
block_type,
|
||||
position: Vector2i::new(
|
||||
GRID_WIDTH as i32 / 2 - block_type.grid[0].len() as i32 / 2,
|
||||
0,
|
||||
),
|
||||
rotation_state: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tiles(&mut self) -> Vec<Vector2i> {
|
||||
let mut tiles: Vec<Vector2i> = Vec::new();
|
||||
for (y, row) in self.block_type.grid.iter().enumerate() {
|
||||
let y = y as i32;
|
||||
for (x, cell) in row.iter().enumerate() {
|
||||
let x = x as i32;
|
||||
if !cell {
|
||||
continue;
|
||||
}
|
||||
let mut rotated = Vector2i::new(x as i32, y as i32);
|
||||
if self.block_type.rotate {
|
||||
let center_x = row.len() as i32 / 2;
|
||||
let center_y = self.block_type.grid.len() as i32 / 2;
|
||||
let offset_x = x - center_x;
|
||||
let offset_y = y - center_y;
|
||||
match self.rotation_state {
|
||||
0 => {},
|
||||
1 => {
|
||||
rotated.x = center_x + offset_y;
|
||||
rotated.y = center_y - offset_x;
|
||||
},
|
||||
2 => {
|
||||
rotated.x = center_x - offset_x;
|
||||
rotated.y = center_y - offset_y;
|
||||
},
|
||||
3 => {
|
||||
rotated.x = center_x - offset_y;
|
||||
rotated.y = center_y + offset_x;
|
||||
}
|
||||
_ => self.rotation_state %= 4
|
||||
}
|
||||
}
|
||||
let global = self.position + rotated;
|
||||
tiles.push(global);
|
||||
}
|
||||
}
|
||||
tiles
|
||||
}
|
||||
}
|
||||
pub fn get_tiles(&mut self) -> Vec<Vector2i> {
|
||||
let mut tiles: Vec<Vector2i> = Vec::new();
|
||||
for (y, row) in self.block_type.grid.iter().enumerate() {
|
||||
let y = y as i32;
|
||||
for (x, cell) in row.iter().enumerate() {
|
||||
let x = x as i32;
|
||||
if !cell {
|
||||
continue;
|
||||
}
|
||||
let mut rotated = Vector2i::new(x as i32, y as i32);
|
||||
if self.block_type.rotate {
|
||||
let center_x = row.len() as i32 / 2;
|
||||
let center_y = self.block_type.grid.len() as i32 / 2;
|
||||
let offset_x = x - center_x;
|
||||
let offset_y = y - center_y;
|
||||
match self.rotation_state {
|
||||
0 => {}
|
||||
1 => {
|
||||
rotated.x = center_x + offset_y;
|
||||
rotated.y = center_y - offset_x;
|
||||
}
|
||||
2 => {
|
||||
rotated.x = center_x - offset_x;
|
||||
rotated.y = center_y - offset_y;
|
||||
}
|
||||
3 => {
|
||||
rotated.x = center_x - offset_y;
|
||||
rotated.y = center_y + offset_x;
|
||||
}
|
||||
_ => self.rotation_state %= 4,
|
||||
}
|
||||
}
|
||||
let global = self.position + rotated;
|
||||
tiles.push(global);
|
||||
}
|
||||
}
|
||||
tiles
|
||||
}
|
||||
}
|
||||
|
@ -1,151 +1,128 @@
|
||||
use crate::structs::TileType;
|
||||
use crate::config::TILE_SIZE;
|
||||
use crate::structs::TileType;
|
||||
use sfml::graphics::IntRect;
|
||||
|
||||
pub struct BlockType {
|
||||
pub tile_type: TileType,
|
||||
pub grid: Vec<Vec<bool>>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub starting_line: u32,
|
||||
pub rotate: bool
|
||||
pub tile_type: TileType,
|
||||
pub grid: Vec<Vec<bool>>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub starting_line: u32,
|
||||
pub rotate: bool,
|
||||
}
|
||||
|
||||
impl BlockType {
|
||||
pub fn new(tile_type: TileType, grid: Vec<Vec<bool>>, rotate: bool) -> Self {
|
||||
let mut width: u32 = 0;
|
||||
let mut height: u32 = 0;
|
||||
let mut starting_line: u32 = 0;
|
||||
for (y, row) in grid.iter().enumerate() {
|
||||
let mut has_content = false;
|
||||
for (x, cell) in row.iter().enumerate() {
|
||||
if *cell {
|
||||
width = std::cmp::max(width, x as u32 + 1);
|
||||
has_content = true;
|
||||
}
|
||||
}
|
||||
if has_content {
|
||||
if height == 0 {
|
||||
starting_line = y as u32;
|
||||
}
|
||||
height = y as u32 + 1 - starting_line;
|
||||
}
|
||||
}
|
||||
Self {
|
||||
tile_type,
|
||||
grid,
|
||||
width,
|
||||
height,
|
||||
starting_line,
|
||||
rotate
|
||||
}
|
||||
}
|
||||
pub fn new(tile_type: TileType, grid: Vec<Vec<bool>>, rotate: bool) -> Self {
|
||||
let mut width: u32 = 0;
|
||||
let mut height: u32 = 0;
|
||||
let mut starting_line: u32 = 0;
|
||||
for (y, row) in grid.iter().enumerate() {
|
||||
let mut has_content = false;
|
||||
for (x, cell) in row.iter().enumerate() {
|
||||
if *cell {
|
||||
width = std::cmp::max(width, x as u32 + 1);
|
||||
has_content = true;
|
||||
}
|
||||
}
|
||||
if has_content {
|
||||
if height == 0 {
|
||||
starting_line = y as u32;
|
||||
}
|
||||
height = y as u32 + 1 - starting_line;
|
||||
}
|
||||
}
|
||||
Self {
|
||||
tile_type,
|
||||
grid,
|
||||
width,
|
||||
height,
|
||||
starting_line,
|
||||
rotate,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_list() -> Vec<Self> {
|
||||
let mut list = Vec::new();
|
||||
let tile_size = TILE_SIZE as i32;
|
||||
pub fn init_list() -> Vec<Self> {
|
||||
let mut list = Vec::new();
|
||||
let tile_size = TILE_SIZE as i32;
|
||||
|
||||
const Y: bool = true;
|
||||
const N: bool = false;
|
||||
const Y: bool = true;
|
||||
const N: bool = false;
|
||||
|
||||
// I block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(0, 0, tile_size, tile_size),
|
||||
IntRect::new(0, tile_size, tile_size, tile_size)
|
||||
),
|
||||
vec![
|
||||
vec![N, N, N, N],
|
||||
vec![Y, Y, Y, Y],
|
||||
vec![N, N, N, N],
|
||||
vec![N, N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
// I block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(0, 0, tile_size, tile_size),
|
||||
IntRect::new(0, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![N, N, N, N],
|
||||
vec![Y, Y, Y, Y],
|
||||
vec![N, N, N, N],
|
||||
vec![N, N, N, N],
|
||||
],
|
||||
true,
|
||||
));
|
||||
|
||||
// J Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![Y, N, N],
|
||||
vec![Y, Y, Y],
|
||||
vec![N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
// J Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![Y, N, N], vec![Y, Y, Y], vec![N, N, N]],
|
||||
true,
|
||||
));
|
||||
|
||||
// L Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 2, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 2, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![N, N, Y],
|
||||
vec![Y, Y, Y],
|
||||
vec![N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
// L Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 2, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 2, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![N, N, Y], vec![Y, Y, Y], vec![N, N, N]],
|
||||
true,
|
||||
));
|
||||
|
||||
// O Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 3, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 3, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![Y, Y],
|
||||
vec![Y, Y]
|
||||
],
|
||||
false
|
||||
));
|
||||
// O Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 3, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 3, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![Y, Y], vec![Y, Y]],
|
||||
false,
|
||||
));
|
||||
|
||||
// S Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 4, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 4, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![N, Y, Y],
|
||||
vec![Y, Y, N],
|
||||
vec![N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
// S Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 4, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 4, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![N, Y, Y], vec![Y, Y, N], vec![N, N, N]],
|
||||
true,
|
||||
));
|
||||
|
||||
// T Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 5, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 5, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![
|
||||
vec![N, Y, N],
|
||||
vec![Y, Y, Y],
|
||||
vec![N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
// T Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 5, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 5, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![N, Y, N], vec![Y, Y, Y], vec![N, N, N]],
|
||||
true,
|
||||
));
|
||||
|
||||
// Z Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 6, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 6, tile_size, tile_size, tile_size)
|
||||
),
|
||||
vec![
|
||||
vec![Y, Y, N],
|
||||
vec![N, Y, Y],
|
||||
vec![N, N, N]
|
||||
],
|
||||
true
|
||||
));
|
||||
|
||||
list
|
||||
}
|
||||
}
|
||||
// Z Block
|
||||
list.push(Self::new(
|
||||
TileType::new(
|
||||
IntRect::new(tile_size * 6, 0, tile_size, tile_size),
|
||||
IntRect::new(tile_size * 6, tile_size, tile_size, tile_size),
|
||||
),
|
||||
vec![vec![Y, Y, N], vec![N, Y, Y], vec![N, N, N]],
|
||||
true,
|
||||
));
|
||||
|
||||
list
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue