diff --git a/src/engine/resources.rs b/src/engine/resources.rs index e7ad019..3e39d29 100644 --- a/src/engine/resources.rs +++ b/src/engine/resources.rs @@ -140,6 +140,7 @@ pub struct Engine { pub lock_delay_left: u8, pub lock_delay_resets: u8, pub lock_delay_resets_left: u8, + pub lock_delay_active: bool, pub need_to_lock: bool, // when lock resets ended } @@ -160,6 +161,7 @@ impl Default for Engine { lock_delay_left: 30, lock_delay_resets: 15, lock_delay_resets_left: 15, + lock_delay_active: false, need_to_lock: false, randomizer: Box::new(Bag{}), } @@ -244,20 +246,15 @@ impl Engine { y } - fn lock_delay_check(&mut self, shift: (i8, i8)){ + fn reset_lock_delay(&mut self, shift: (i8, i8)){ match self.rotation_system.lock_delay_mode { LockDelayMode::Disabled => {}, - LockDelayMode::Gravity => {}, - LockDelayMode::ResetOnYChange => { - if shift.1 < 0 { - self.lock_delay_left = self.lock_delay; - if self.lock_delay_resets_left == 0{ - self.need_to_lock = true; - }else{ - self.lock_delay_resets_left -= 1; - } + LockDelayMode::Gravity => { + if self.position_is_valid((self.current_piece.as_ref().unwrap().position.0, self.current_piece.as_ref().unwrap().position.1-1), self.current_piece.as_ref().unwrap().rotation) { + self.lock_delay_active = false; } }, + LockDelayMode::ResetOnYChange => {}, LockDelayMode::ResetOnMovementLimited => { if !self.position_is_valid((self.current_piece.as_ref().unwrap().position.0, self.current_piece.as_ref().unwrap().position.1-1), self.current_piece.as_ref().unwrap().rotation){ self.lock_delay_left = self.lock_delay; @@ -265,12 +262,14 @@ impl Engine { self.need_to_lock = true; }else{ self.lock_delay_resets_left -= 1; + self.lock_delay_active = false; } } }, LockDelayMode::ResetOnMovement => { if !self.position_is_valid((self.current_piece.as_ref().unwrap().position.0, self.current_piece.as_ref().unwrap().position.1-1), self.current_piece.as_ref().unwrap().rotation){ self.lock_delay_left = self.lock_delay; + self.lock_delay_active = false; } }, } @@ -286,7 +285,7 @@ impl Engine { }else{ 1 }; - self.lock_delay_check((0, 0)); + self.reset_lock_delay((0, 0)); for test in &self.rotation_system.kicks[self.current_piece.as_ref().unwrap().id][self.current_piece.as_ref().unwrap().rotation][id_for_kicks]{ let future_position = (self.current_piece.as_ref().unwrap().position.0 + test.0 as isize, self.current_piece.as_ref().unwrap().position.1 + test.1 as isize); if self.position_is_valid(future_position, future_rotation) { @@ -307,7 +306,7 @@ impl Engine { self.current_piece.as_ref().unwrap().position.1 + shift.1 as isize // future Y ); if self.position_is_valid(future_position, self.current_piece.as_ref().unwrap().rotation) { - if shift.0 != 0 {self.lock_delay_check(shift);} + if shift.0 != 0 {self.reset_lock_delay(shift);} self.current_piece.as_mut().unwrap().position = future_position; true }else { diff --git a/src/engine/systems.rs b/src/engine/systems.rs index 64ea343..cf21c5e 100644 --- a/src/engine/systems.rs +++ b/src/engine/systems.rs @@ -1,6 +1,6 @@ -use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; +use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates}; use crate::engine::components::*; -use super::{resources::Engine, GameloopStates, GameStates}; +use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; const MINO_SIZE: f32 = 20.0; @@ -10,15 +10,23 @@ pub fn init_engine( mut materials: ResMut>, mut engine: ResMut, mut next_state: ResMut>, -){ +) { commands.spawn(( MaterialMesh2dBundle { - mesh: meshes.add(Mesh::from(shape::Quad{ size: Vec2 { x: engine.board.width as f32 * MINO_SIZE, y: engine.board.height as f32 * MINO_SIZE }, flip: false })).into(), + mesh: meshes + .add(Mesh::from(shape::Quad { + size: Vec2 { + x: engine.board.width as f32 * MINO_SIZE, + y: engine.board.height as f32 * MINO_SIZE, + }, + flip: false, + })) + .into(), material: materials.add(ColorMaterial::from(Color::PURPLE)), transform: Transform::from_xyz(0.0, 0.0, -1.0), ..default() }, - BoardVisual{} + BoardVisual {}, )); engine.init("SRS"); next_state.set(GameloopStates::Falling); @@ -28,8 +36,8 @@ pub fn draw_board( mut commands: Commands, engine: Res, all_minos: Query>, - asset_server: Res -){ + asset_server: Res, +) { for mino in all_minos.iter() { commands.entity(mino).despawn(); } @@ -44,14 +52,19 @@ pub fn draw_board( commands.spawn(( SpriteBundle { transform: Transform::from_xyz( - x*MINO_SIZE-(engine.board.width as f32)/2.0*MINO_SIZE+MINO_SIZE/2.0, - y*MINO_SIZE-(engine.board.height as f32)/2.0*MINO_SIZE+MINO_SIZE/2.0, - 0.0 + x * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0, + y * MINO_SIZE - (engine.board.height as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0, + 0.0, ), texture: asset_server.load("default_mino.png"), - sprite: Sprite { + sprite: Sprite { color: mino.color, - custom_size: Some(Vec2 {x: MINO_SIZE, y: MINO_SIZE}), + custom_size: Some(Vec2 { + x: MINO_SIZE, + y: MINO_SIZE, + }), ..default() }, ..default() @@ -59,7 +72,7 @@ pub fn draw_board( *mino, )); } - None => {}, + None => {} }; x += 1.0; } @@ -72,50 +85,68 @@ pub fn draw_board( Some(piece) => { x = piece.position.0 as f32; y = piece.position.1 as f32; - for mino in &engine.rotation_system.pieces[piece.id][piece.rotation]{ + for mino in &engine.rotation_system.pieces[piece.id][piece.rotation] { commands.spawn(( SpriteBundle { transform: Transform::from_xyz( - x*MINO_SIZE - (engine.board.width as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.0 as f32 * MINO_SIZE, - y*MINO_SIZE - (engine.board.height as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.1 as f32 * MINO_SIZE, - 1.0 + x * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.0 as f32 * MINO_SIZE, + y * MINO_SIZE - (engine.board.height as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.1 as f32 * MINO_SIZE, + 1.0, ), texture: asset_server.load("default_mino.png"), - sprite: Sprite { + sprite: Sprite { color: engine.rotation_system.colours[piece.id], - custom_size: Some(Vec2 {x: MINO_SIZE, y: MINO_SIZE}), + custom_size: Some(Vec2 { + x: MINO_SIZE, + y: MINO_SIZE, + }), ..default() }, ..default() }, - Mino{color: engine.rotation_system.colours[piece.id]}, + Mino { + color: engine.rotation_system.colours[piece.id], + }, )); } - }, - None => {}, + } + None => {} } // draw next queue if engine.board.show_next > 0 { y = 8.0; - for i in 0..engine.board.show_next as usize{ - for mino in &engine.rotation_system.pieces[engine.next_queue[i].id][engine.next_queue[i].rotation]{ + for i in 0..engine.board.show_next as usize { + for mino in &engine.rotation_system.pieces[engine.next_queue[i].id] + [engine.next_queue[i].rotation] + { commands.spawn(( SpriteBundle { transform: Transform::from_xyz( - 11.0*MINO_SIZE - (engine.board.width as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.0 as f32 * MINO_SIZE, - y* MINO_SIZE + MINO_SIZE/2.0 + mino.1 as f32 * MINO_SIZE, - 0.0 + 11.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.0 as f32 * MINO_SIZE, + y * MINO_SIZE + MINO_SIZE / 2.0 + mino.1 as f32 * MINO_SIZE, + 0.0, ), texture: asset_server.load("default_mino.png"), - sprite: Sprite { + sprite: Sprite { color: engine.rotation_system.colours[engine.next_queue[i].id], - custom_size: Some(Vec2 {x: MINO_SIZE, y: MINO_SIZE}), + custom_size: Some(Vec2 { + x: MINO_SIZE, + y: MINO_SIZE, + }), ..default() }, ..default() }, - Mino{color: engine.rotation_system.colours[engine.next_queue[i].id]}, + Mino { + color: engine.rotation_system.colours[engine.next_queue[i].id], + }, )); } y -= 4.0; @@ -125,56 +156,78 @@ pub fn draw_board( // draw hold match engine.hold.as_ref() { Some(piece) => { - for mino in &engine.rotation_system.pieces[piece.id][piece.rotation]{ + for mino in &engine.rotation_system.pieces[piece.id][piece.rotation] { commands.spawn(( SpriteBundle { transform: Transform::from_xyz( - -5.0*MINO_SIZE - (engine.board.width as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.0 as f32 * MINO_SIZE, - -2.0*MINO_SIZE + (engine.board.height as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.1 as f32 * MINO_SIZE, - 0.0 + -5.0 * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.0 as f32 * MINO_SIZE, + -2.0 * MINO_SIZE + + (engine.board.height as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.1 as f32 * MINO_SIZE, + 0.0, ), texture: asset_server.load("default_mino.png"), - sprite: Sprite { + sprite: Sprite { color: piece.color, - custom_size: Some(Vec2 {x: MINO_SIZE, y: MINO_SIZE}), + custom_size: Some(Vec2 { + x: MINO_SIZE, + y: MINO_SIZE, + }), ..default() }, ..default() }, - Mino{color: piece.color}, + Mino { color: piece.color }, )); } - }, - None => {}, + } + None => {} } - + // draw shadow if engine.board.show_shadow { match engine.current_piece.as_ref() { Some(piece) => { x = piece.position.0 as f32; y = engine.lowest_point_under_current_piece() as f32; - for mino in &engine.rotation_system.pieces[piece.id][piece.rotation]{ + for mino in &engine.rotation_system.pieces[piece.id][piece.rotation] { commands.spawn(( SpriteBundle { transform: Transform::from_xyz( - x*MINO_SIZE - (engine.board.width as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.0 as f32 * MINO_SIZE, - y*MINO_SIZE - (engine.board.height as f32)/2.0*MINO_SIZE + MINO_SIZE/2.0 + mino.1 as f32 * MINO_SIZE, - 0.0 + x * MINO_SIZE - (engine.board.width as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.0 as f32 * MINO_SIZE, + y * MINO_SIZE - (engine.board.height as f32) / 2.0 * MINO_SIZE + + MINO_SIZE / 2.0 + + mino.1 as f32 * MINO_SIZE, + 0.0, ), texture: asset_server.load("default_mino.png"), - sprite: Sprite { - color: Color::Rgba { red: 1.0, green: 1.0, blue: 1.0, alpha: 0.1 }, - custom_size: Some(Vec2 {x: MINO_SIZE, y: MINO_SIZE}), + sprite: Sprite { + color: Color::Rgba { + red: 1.0, + green: 1.0, + blue: 1.0, + alpha: 0.1, + }, + custom_size: Some(Vec2 { + x: MINO_SIZE, + y: MINO_SIZE, + }), ..default() }, ..default() }, - Mino{color: engine.rotation_system.colours[piece.id]}, + Mino { + color: engine.rotation_system.colours[piece.id], + }, )); } - }, - None => {}, + } + None => {} } } } @@ -184,8 +237,10 @@ pub fn receive_input( mut engine: ResMut, state: Res>, mut next_state: ResMut>, -){ - if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X]) && state.get() == &GameloopStates::Falling { +) { + if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X]) + && state.get() == &GameloopStates::Falling + { engine.rotate_current_piece(1); } if keyboard_input.just_pressed(KeyCode::Z) && state.get() == &GameloopStates::Falling { @@ -225,15 +280,11 @@ pub fn receive_input( } } -pub fn das_and_arr( - mut engine: ResMut, - time: Res