76 lines
1.7 KiB
Rust
76 lines
1.7 KiB
Rust
use crate::random::RandomSystem;
|
|
use crate::Tetromino;
|
|
use std::collections::VecDeque;
|
|
|
|
#[derive(Copy, Clone)]
|
|
enum MinoColor {
|
|
Cyan,
|
|
Yellow,
|
|
Purple,
|
|
Green,
|
|
Red,
|
|
Blue,
|
|
Orange,
|
|
Gray,
|
|
}
|
|
|
|
enum Movement {
|
|
Rotate,
|
|
Gravity,
|
|
Translation,
|
|
}
|
|
|
|
pub struct Position {
|
|
x: u8,
|
|
y: u8,
|
|
}
|
|
|
|
pub struct PlayField {
|
|
can_swap_hold: bool,
|
|
hold_piece: Option<Tetromino>,
|
|
field: [[Option<MinoColor>; 10]; 40], // access via y, x
|
|
active_piece: Tetromino,
|
|
active_pos: Position,
|
|
bag: RandomSystem,
|
|
next_pieces: VecDeque<Tetromino>,
|
|
last_movement: Movement,
|
|
}
|
|
|
|
impl PlayField {
|
|
pub fn new() -> Self {
|
|
let mut bag = RandomSystem::new();
|
|
let active_piece = bag.get_tetrino();
|
|
let mut next_pieces = VecDeque::with_capacity(3);
|
|
for _ in 0..next_pieces.len() {
|
|
next_pieces.push_back(bag.get_tetrino());
|
|
}
|
|
|
|
PlayField {
|
|
can_swap_hold: true,
|
|
hold_piece: None,
|
|
field: [[None; 10]; 40],
|
|
active_piece,
|
|
active_pos: Position { x: 0, y: 0 },
|
|
bag,
|
|
next_pieces,
|
|
last_movement: Movement::Gravity,
|
|
}
|
|
}
|
|
|
|
fn get_new_piece(&mut self) {
|
|
self.active_piece = self
|
|
.next_pieces
|
|
.pop_front()
|
|
.expect("visible queue to be populated");
|
|
self.next_pieces.push_back(self.bag.get_tetrino());
|
|
self.reset_position();
|
|
self.can_swap_hold = true;
|
|
}
|
|
|
|
/// Place the current Tetromino at its default spawn position and
|
|
/// orientation. Used for hold-swapping or spawning in a new piece.
|
|
fn reset_position(&mut self) {
|
|
self.last_movement = Movement::Gravity;
|
|
unimplemented!();
|
|
}
|
|
}
|