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