From 10abb0a67609476d4d759c47ac0e2b7d4a619a83 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Thu, 25 Aug 2022 11:31:12 +0200 Subject: [PATCH] Reset level --- README.md | 2 +- src/game.rs | 49 ++++++++++++++++++++++++++++++++++--------------- src/levels.rs | 10 ++-------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index db5e8dc..abc7271 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ * **Move**: arrows * **Switch character**: Tab * **Level up**: Enter (when character is white) +* **Reset**: R ## TODO @@ -15,7 +16,6 @@ * level design * (?) can jump only from a surface (no mid-air jump) * (?) multiplayer -* level reset * more audio ## Build diff --git a/src/game.rs b/src/game.rs index 484dd21..8e47fb1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -41,12 +41,14 @@ impl Plugin for GamePlugin { .with_system(crate::levels::post_setup_level) .with_system(change_character_system) .with_system(player_movement_system) + .with_system(level_keyboard_system) .with_system(move_camera) .with_system(character_particle_effect_system), ) .add_system_set( SystemSet::on_update(AppState::Win) .with_system(player_movement_system) + .with_system(level_keyboard_system) .with_system(move_camera) .with_system(character_particle_effect_system), ) @@ -56,7 +58,7 @@ impl Plugin for GamePlugin { // Events -pub struct LevelStartupEvent(pub Entity); +pub struct LevelStartupEvent; // Resources @@ -94,19 +96,15 @@ pub struct Player; // Systems fn setup( - mut commands: Commands, mut current_level: ResMut, mut level_startup_event: EventWriter, mut camera_query: Query<&mut Transform, With>, ) { - let level_id = LevelId(current_level.0.map_or(0, |level_id| level_id.0 + 1)); - crate::levels::setup_level( - &mut commands, - &mut current_level, - &mut level_startup_event, - &mut camera_query, - level_id, - ); + if current_level.0.is_none() { + current_level.0 = Some(LevelId(0)); + } + + crate::levels::setup_level(&mut level_startup_event, &mut camera_query); } pub fn spawn_characters>( @@ -300,7 +298,6 @@ fn change_character_system( fn player_movement_system( keyboard_input: Res>, mut characters: Query<&mut Velocity, With>, - mut app_state: ResMut>, audio: Res>, ) { let right_pressed: bool = @@ -316,10 +313,6 @@ fn player_movement_system( velocity.linvel.y = 500.; } } - - if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) { - app_state.replace(AppState::Game).unwrap(); - } } fn character_particle_effect_system( @@ -374,3 +367,29 @@ fn move_camera( camera_transform.translation.z = 999.0; } } + +fn level_keyboard_system( + mut commands: Commands, + mut current_level: ResMut, + mut level_startup_event: EventWriter, + mut camera_query: Query<&mut Transform, With>, + keyboard_input: Res>, + level_query: Query>, + mut app_state: ResMut>, +) { + if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) { + current_level.0 = Some(LevelId( + current_level.0.map_or(0, |level_id| level_id.0 + 1), + )); + app_state.replace(AppState::Game).unwrap(); + } + + if keyboard_input.just_pressed(KeyCode::R) { + for entity in level_query.iter() { + commands.entity(entity).despawn_recursive(); + } + if app_state.replace(AppState::Game).is_err() { + crate::levels::setup_level(&mut level_startup_event, &mut camera_query); + } + } +} diff --git a/src/levels.rs b/src/levels.rs index ac00218..c6f446e 100644 --- a/src/levels.rs +++ b/src/levels.rs @@ -9,18 +9,12 @@ use crate::game::*; use bevy::prelude::*; pub fn setup_level( - commands: &mut Commands, - current_level: &mut ResMut, level_startup_event: &mut EventWriter, camera_query: &mut Query<&mut Transform, With>, - level_id: LevelId, ) { - let level_entity = commands.spawn().insert(Level).id(); - current_level.0 = Some(level_id); - camera_query.single_mut().translation = Default::default(); - level_startup_event.send(LevelStartupEvent(level_entity)); + level_startup_event.send(LevelStartupEvent); } pub fn despawn_level(mut commands: Commands, level_query: Query>) { @@ -40,7 +34,7 @@ pub fn post_setup_level( asset_server: Res, audio: Res>, ) { - for LevelStartupEvent(_level_entity) in level_startup_event.iter() { + for _ in level_startup_event.iter() { if let Some(level_id) = current_level.0 { match level_id.0 { 0 => level0::setup(