NRS and RandomWithoutDirectRepetition (Not tested)

This commit is contained in:
dan63047 2023-11-15 14:55:47 +03:00
parent da41c6e030
commit 3ec1b672bd
5 changed files with 125 additions and 26 deletions

View File

@ -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))).

View File

@ -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<Piece>;
fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece>;
}
pub struct Bag {}
pub struct RandomWithoutDirectRepetition {}
impl Randomizer for Bag {
fn populate_next(&self, pieces_data: &PiecesData) -> Vec<Piece> {
fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
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<Piece> {
let random_number = random::<usize>() % pieces_data.pieces.len();
vec![Piece::create(pieces_data, random_number, board_width, board_height)]
}
}

View File

@ -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();
}

View File

@ -19,7 +19,9 @@ pub struct PiecesData {
pub colours: Vec<Color>,
// 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
};

View File

@ -11,6 +11,7 @@ pub fn init_engine(
mut engine: ResMut<Engine>,
mut next_state: ResMut<NextState<GameloopStates>>,
) {
//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<State<GameloopStates>>,
mut next_state: ResMut<NextState<GameloopStates>>,
) {
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
{