From 3ec1b672bda3d259b17e821934b8262802fa9fd4 Mon Sep 17 00:00:00 2001 From: dan63047 Date: Wed, 15 Nov 2023 14:55:47 +0300 Subject: [PATCH] NRS and RandomWithoutDirectRepetition (Not tested) --- src/engine/mod.rs | 1 + src/engine/randomizers.rs | 17 +++-- src/engine/resources.rs | 13 ++-- src/engine/rotation_systems.rs | 114 ++++++++++++++++++++++++++++----- src/engine/systems.rs | 6 +- 5 files changed, 125 insertions(+), 26 deletions(-) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index b24a178..24f112c 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -18,6 +18,7 @@ impl Plugin for UBSGEngine{ insert_resource(Engine::default()). add_systems(Startup, init_engine.run_if(in_state(GameStates::Gameplay))). add_systems(Startup, spawn_hud). + add_systems(OnEnter(GameloopStates::Init), init_engine). add_systems(Update, receive_input.run_if(in_state(GameStates::Gameplay))). add_systems(Update, das_and_arr.run_if(in_state(GameStates::Gameplay))). add_systems(FixedUpdate, gameloop.run_if(in_state(GameStates::Gameplay)).run_if(in_state(GameloopStates::Falling))). diff --git a/src/engine/randomizers.rs b/src/engine/randomizers.rs index 9e2df39..0f58920 100644 --- a/src/engine/randomizers.rs +++ b/src/engine/randomizers.rs @@ -1,24 +1,33 @@ -use rand::seq::SliceRandom; +use rand::{seq::SliceRandom, random}; use rand::thread_rng; use super::{rotation_systems::PiecesData, resources::Piece}; pub trait Randomizer{ - fn populate_next(&self, pieces_data: &PiecesData) -> Vec; + fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec; } pub struct Bag {} +pub struct RandomWithoutDirectRepetition {} + impl Randomizer for Bag { - fn populate_next(&self, pieces_data: &PiecesData) -> Vec { + fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec { let mut bag = vec![]; let mut id: usize = 0; for _ in &pieces_data.pieces{ - bag.insert(id, Piece::create(pieces_data, id)); + bag.insert(id, Piece::create(pieces_data, id, board_width, board_height)); id += 1; } let mut rng = thread_rng(); bag.shuffle(&mut rng); bag } +} + +impl Randomizer for RandomWithoutDirectRepetition { + fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec { + let random_number = random::() % pieces_data.pieces.len(); + vec![Piece::create(pieces_data, random_number, board_width, board_height)] + } } \ No newline at end of file diff --git a/src/engine/resources.rs b/src/engine/resources.rs index 3e39d29..bb12b63 100644 --- a/src/engine/resources.rs +++ b/src/engine/resources.rs @@ -13,8 +13,11 @@ pub struct Piece{ } impl Piece { - pub fn create(pieces_data: &PiecesData, id: usize) -> Piece{ - let final_position = (3+pieces_data.spawn_offsets[id].0, 20+pieces_data.spawn_offsets[id].1); + pub fn create(pieces_data: &PiecesData, id: usize, board_width: isize, board_height: isize) -> Piece{ + let final_position = ( + board_width/2 - 2 + pieces_data.spawn_offsets[id].0, + board_height + pieces_data.height_offset + pieces_data.spawn_offsets[id].1 + ); Piece { id: id, color: pieces_data.colours[id], position: final_position, rotation: 0 } } } @@ -171,7 +174,7 @@ impl Default for Engine { impl Engine { fn from_next_to_current(&mut self){ if self.next_queue.len() <= self.board.show_next as usize { - self.next_queue.append(&mut self.randomizer.populate_next(&self.rotation_system)); + self.next_queue.append(&mut self.randomizer.populate_next(&self.rotation_system, self.board.width as isize, self.board.height as isize)); } self.current_piece = self.next_queue.first().copied(); self.next_queue.remove(0); @@ -179,7 +182,9 @@ impl Engine { pub fn init(&mut self, rotation_system: &str){ self.rotation_system = ROTATION_SYSTEMS[rotation_system].clone(); - self.next_queue.append(&mut self.randomizer.populate_next(&self.rotation_system)); + while self.next_queue.len() <= self.board.show_next as usize { + self.next_queue.append(&mut self.randomizer.populate_next(&self.rotation_system, self.board.width as isize, self.board.height as isize)); + } self.from_next_to_current(); } diff --git a/src/engine/rotation_systems.rs b/src/engine/rotation_systems.rs index 337244d..54333fb 100644 --- a/src/engine/rotation_systems.rs +++ b/src/engine/rotation_systems.rs @@ -19,7 +19,9 @@ pub struct PiecesData { pub colours: Vec, // If spawn position is fucked, it fixes it pub spawn_offsets: Vec<(isize, isize)>, - pub lock_delay_mode: LockDelayMode + pub lock_delay_mode: LockDelayMode, + // How high above field that piece should be spawned + pub height_offset: isize, } lazy_static!{ @@ -53,9 +55,6 @@ lazy_static!{ ], vec![ // O vec![(0, 0), (0, 1), (1, 1), (1, 0)], - vec![(0, 1), (1, 1), (1, 0), (0, 0)], - vec![(1, 1), (1, 0), (0, 0), (0, 1)], - vec![(1, 0), (0, 0), (0, 1), (1, 1)] ], vec![ // L vec![(2, 2), (2, 1), (1, 1), (0, 1)], @@ -148,18 +147,6 @@ lazy_static!{ vec![( 0, 0)], // 0 -> 90 vec![( 0, 0)], // 0 -> 270 ], - vec![ - vec![( 0, 0)], // 90 -> 180 - vec![( 0, 0)], // 90 -> 0 - ], - vec![ - vec![( 0, 0)], // 180 -> 270 - vec![( 0, 0)], // 180 -> 90 - ], - vec![ - vec![( 0, 0)], // 270 -> 0 - vec![( 0, 0)], // 270 -> 180 - ] ], vec![ // L vec![ @@ -216,7 +203,100 @@ lazy_static!{ (0, 0), // L (0, 0) // S ], - lock_delay_mode: LockDelayMode::ResetOnMovementLimited + lock_delay_mode: LockDelayMode::ResetOnMovementLimited, + height_offset: 0, + }); + rs.insert(String::from("NRS"), PiecesData { + pieces: vec![ + vec![ // Z + vec![(0, 1), (1, 1), (1, 0), (2, 0)], + vec![(2, 2), (2, 1), (1, 1), (1, 0)], + ], + vec![ // J + vec![(2, 0), (2, 1), (1, 1), (0, 1)], + vec![(0, 0), (1, 0), (1, 1), (1, 2)], + vec![(0, 2), (0, 1), (1, 1), (2, 1)], + vec![(2, 2), (1, 2), (1, 1), (1, 0)], + ], + vec![ // I + vec![(3, 1), (2, 1), (1, 1), (0, 1)], + vec![(2, 3), (2, 2), (2, 1), (2, 0)], + ], + vec![ // T + vec![(1, 0), (2, 1), (1, 1), (0, 1)], + vec![(0, 1), (1, 0), (1, 1), (1, 2)], + vec![(1, 2), (0, 1), (1, 1), (2, 1)], + vec![(2, 1), (1, 2), (1, 1), (1, 0)], + ], + vec![ // O + vec![(0, 0), (0, 1), (1, 1), (1, 0)], + ], + vec![ // L + vec![(0, 0), (0, 1), (1, 1), (2, 1)], + vec![(0, 2), (1, 2), (1, 1), (1, 0)], + vec![(2, 2), (2, 1), (1, 1), (0, 1)], + vec![(2, 0), (1, 0), (1, 1), (1, 2)], + ], + vec![ // S + vec![(0, 0), (1, 0), (1, 1), (2, 1)], + vec![(2, 0), (2, 1), (1, 1), (1, 2)], + ] + ], + kicks: vec![ + vec![ // Z + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // J + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // I + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // T + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // O + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // L + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + vec![ // S + vec![vec![( 0, 0)], vec![( 0, 0)]], + vec![vec![( 0, 0)], vec![( 0, 0)]], + ], + ], + colours: vec![ + Color::RED, // Z + Color::Rgba { red: 0.0, green: 0.3, blue: 1.0, alpha: 1.0 }, // J + Color::CYAN, // I + Color::Rgba { red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0 }, // T + Color::YELLOW, // O + Color::ORANGE, // L + Color::GREEN // Z + ], + spawn_offsets: vec![ + (0, 0), // Z + (0, 0), // J + (0, 0), // I + (0, 0), // T + (1, 0), // O + (0, 0), // L + (0, 0) // S + ], + height_offset: -2, + lock_delay_mode: LockDelayMode::Gravity }); rs }; diff --git a/src/engine/systems.rs b/src/engine/systems.rs index cf21c5e..2741861 100644 --- a/src/engine/systems.rs +++ b/src/engine/systems.rs @@ -11,6 +11,7 @@ pub fn init_engine( mut engine: ResMut, mut next_state: ResMut>, ) { + //commands.insert_resource(Engine::default()); commands.spawn(( MaterialMesh2dBundle { mesh: meshes @@ -28,7 +29,7 @@ pub fn init_engine( }, BoardVisual {}, )); - engine.init("SRS"); + engine.init("NRS"); next_state.set(GameloopStates::Falling); } @@ -238,6 +239,9 @@ pub fn receive_input( state: Res>, mut next_state: ResMut>, ) { + if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init { + next_state.set(GameloopStates::Init); + } if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X]) && state.get() == &GameloopStates::Falling {