From 18410079c6e684048a75bb874c3e0702751a9805 Mon Sep 17 00:00:00 2001 From: dan63047 Date: Thu, 16 Nov 2023 18:54:36 +0300 Subject: [PATCH] Reset button now works --- src/engine/mod.rs | 7 ++++--- src/engine/systems.rs | 48 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 24f112c..af154c6 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -16,7 +16,7 @@ impl Plugin for UBSGEngine{ add_state::(). add_state::(). insert_resource(Engine::default()). - add_systems(Startup, init_engine.run_if(in_state(GameStates::Gameplay))). + //add_systems(Startup, init_engine). add_systems(Startup, spawn_hud). add_systems(OnEnter(GameloopStates::Init), init_engine). add_systems(Update, receive_input.run_if(in_state(GameStates::Gameplay))). @@ -24,14 +24,15 @@ impl Plugin for UBSGEngine{ add_systems(FixedUpdate, gameloop.run_if(in_state(GameStates::Gameplay)).run_if(in_state(GameloopStates::Falling))). add_systems(OnEnter(GameloopStates::AfterLocking), after_locking_routine). add_systems(OnEnter(GameloopStates::Spawn), spawn_routine). - add_systems(Update, draw_board); + add_systems(Update, draw_board.run_if(in_state(GameStates::Gameplay))). + add_systems(Update, receive_input_on_game_over.run_if(in_state(GameStates::GameOver))); } } #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] pub enum GameStates{ - Init, #[default] + Init, Gameplay, Pause, GameOver diff --git a/src/engine/systems.rs b/src/engine/systems.rs index b5ef271..0701981 100644 --- a/src/engine/systems.rs +++ b/src/engine/systems.rs @@ -4,14 +4,23 @@ use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; const MINO_SIZE: f32 = 20.0; +pub fn reset_engine(mut commands: Commands){ + commands.remove_resource::(); + commands.insert_resource::(Engine::default()); +} + pub fn init_engine( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, mut engine: ResMut, + mut old_board: Query>, mut next_state: ResMut>, + mut game_next_state: ResMut>, ) { - //commands.insert_resource(Engine::default()); + for board in old_board.iter() { + commands.entity(board).despawn(); + } commands.spawn(( MaterialMesh2dBundle { mesh: meshes @@ -29,7 +38,8 @@ pub fn init_engine( }, BoardVisual {}, )); - engine.init("SRS", Box::new(BagX2::create())); + engine.init("NRS", Box::new(RandomWithoutDirectRepetition::create())); + game_next_state.set(GameStates::Gameplay); next_state.set(GameloopStates::Falling); } @@ -121,22 +131,23 @@ pub fn draw_board( // 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] + let mut drawed = 0; + for mino in &engine.next_queue { + for tile in &engine.rotation_system.pieces[mino.id] + [mino.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, + + tile.0 as f32 * MINO_SIZE, + y * MINO_SIZE + MINO_SIZE / 2.0 + tile.1 as f32 * MINO_SIZE, 0.0, ), texture: asset_server.load("default_mino.png"), sprite: Sprite { - color: engine.rotation_system.colours[engine.next_queue[i].id], + color: engine.rotation_system.colours[mino.id], custom_size: Some(Vec2 { x: MINO_SIZE, y: MINO_SIZE, @@ -146,11 +157,15 @@ pub fn draw_board( ..default() }, Mino { - color: engine.rotation_system.colours[engine.next_queue[i].id], + color: engine.rotation_system.colours[mino.id], }, )); } y -= 4.0; + drawed += 1; + if drawed >= engine.board.show_next { + break; + } } } @@ -238,8 +253,11 @@ pub fn receive_input( mut engine: ResMut, state: Res>, mut next_state: ResMut>, + mut game_next_state: ResMut>, + mut commands: Commands ) { if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init { + reset_engine(commands); next_state.set(GameloopStates::Init); } if keyboard_input.any_just_pressed([KeyCode::Up, KeyCode::X]) @@ -284,6 +302,18 @@ pub fn receive_input( } } +pub fn receive_input_on_game_over( + keyboard_input: Res>, + state: Res>, + mut next_state: ResMut>, + mut commands: Commands +){ + if keyboard_input.just_pressed(KeyCode::R) && state.get() != &GameloopStates::Init { + reset_engine(commands); + next_state.set(GameloopStates::Init); + } +} + pub fn das_and_arr(mut engine: ResMut, time: Res