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
* **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

View file

@ -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<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>,
) {
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<I: IntoIterator<Item = (Transform, Color)>>(
@ -300,7 +298,6 @@ fn change_character_system(
fn player_movement_system(
keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<&mut Velocity, With<Player>>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
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<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::*;
pub fn setup_level(
commands: &mut Commands,
current_level: &mut ResMut<CurrentLevel>,
level_startup_event: &mut EventWriter<LevelStartupEvent>,
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();
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>>) {
@ -40,7 +34,7 @@ pub fn post_setup_level(
asset_server: Res<AssetServer>,
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 {
match level_id.0 {
0 => level0::setup(