Reset level

This commit is contained in:
Pascal Engélibert 2022-08-25 11:31:12 +02:00
parent 56fea25a87
commit 10abb0a676
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
3 changed files with 37 additions and 24 deletions

View file

@ -5,6 +5,7 @@
* **Move**: arrows * **Move**: arrows
* **Switch character**: Tab * **Switch character**: Tab
* **Level up**: Enter (when character is white) * **Level up**: Enter (when character is white)
* **Reset**: R
## TODO ## TODO
@ -15,7 +16,6 @@
* level design * level design
* (?) can jump only from a surface (no mid-air jump) * (?) can jump only from a surface (no mid-air jump)
* (?) multiplayer * (?) multiplayer
* level reset
* more audio * more audio
## Build ## Build

View file

@ -41,12 +41,14 @@ impl Plugin for GamePlugin {
.with_system(crate::levels::post_setup_level) .with_system(crate::levels::post_setup_level)
.with_system(change_character_system) .with_system(change_character_system)
.with_system(player_movement_system) .with_system(player_movement_system)
.with_system(level_keyboard_system)
.with_system(move_camera) .with_system(move_camera)
.with_system(character_particle_effect_system), .with_system(character_particle_effect_system),
) )
.add_system_set( .add_system_set(
SystemSet::on_update(AppState::Win) SystemSet::on_update(AppState::Win)
.with_system(player_movement_system) .with_system(player_movement_system)
.with_system(level_keyboard_system)
.with_system(move_camera) .with_system(move_camera)
.with_system(character_particle_effect_system), .with_system(character_particle_effect_system),
) )
@ -56,7 +58,7 @@ impl Plugin for GamePlugin {
// Events // Events
pub struct LevelStartupEvent(pub Entity); pub struct LevelStartupEvent;
// Resources // Resources
@ -94,19 +96,15 @@ pub struct Player;
// Systems // Systems
fn setup( fn setup(
mut commands: Commands,
mut current_level: ResMut<CurrentLevel>, mut current_level: ResMut<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>, mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>, mut camera_query: Query<&mut Transform, With<Camera>>,
) { ) {
let level_id = LevelId(current_level.0.map_or(0, |level_id| level_id.0 + 1)); if current_level.0.is_none() {
crate::levels::setup_level( current_level.0 = Some(LevelId(0));
&mut commands, }
&mut current_level,
&mut level_startup_event, crate::levels::setup_level(&mut level_startup_event, &mut camera_query);
&mut camera_query,
level_id,
);
} }
pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>( pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
@ -300,7 +298,6 @@ fn change_character_system(
fn player_movement_system( fn player_movement_system(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<&mut Velocity, With<Player>>, mut characters: Query<&mut Velocity, With<Player>>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>, audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) { ) {
let right_pressed: bool = let right_pressed: bool =
@ -316,10 +313,6 @@ fn player_movement_system(
velocity.linvel.y = 500.; 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( fn character_particle_effect_system(
@ -374,3 +367,29 @@ fn move_camera(
camera_transform.translation.z = 999.0; camera_transform.translation.z = 999.0;
} }
} }
fn level_keyboard_system(
mut commands: Commands,
mut current_level: ResMut<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>,
keyboard_input: Res<Input<KeyCode>>,
level_query: Query<Entity, With<Level>>,
mut app_state: ResMut<State<AppState>>,
) {
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);
}
}
}

View file

@ -9,18 +9,12 @@ use crate::game::*;
use bevy::prelude::*; use bevy::prelude::*;
pub fn setup_level( pub fn setup_level(
commands: &mut Commands,
current_level: &mut ResMut<CurrentLevel>,
level_startup_event: &mut EventWriter<LevelStartupEvent>, level_startup_event: &mut EventWriter<LevelStartupEvent>,
camera_query: &mut Query<&mut Transform, With<Camera>>, camera_query: &mut Query<&mut Transform, With<Camera>>,
level_id: LevelId,
) { ) {
let level_entity = commands.spawn().insert(Level).id();
current_level.0 = Some(level_id);
camera_query.single_mut().translation = Default::default(); 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<Entity, With<Level>>) { pub fn despawn_level(mut commands: Commands, level_query: Query<Entity, With<Level>>) {
@ -40,7 +34,7 @@ pub fn post_setup_level(
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>, audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) { ) {
for LevelStartupEvent(_level_entity) in level_startup_event.iter() { for _ in level_startup_event.iter() {
if let Some(level_id) = current_level.0 { if let Some(level_id) = current_level.0 {
match level_id.0 { match level_id.0 {
0 => level0::setup( 0 => level0::setup(