game state and restart, but at what cost?? (my mental health)
This commit is contained in:
parent
4b9101ad2c
commit
002ad4a162
11 changed files with 143 additions and 23 deletions
87
src/core/game_state.rs
Normal file
87
src/core/game_state.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
use bevy::{ecs::system::SystemParam, prelude::*};
|
||||
|
||||
use crate::core::{kirby::Kirby, wave::BubbleWaves};
|
||||
|
||||
#[derive(Event)]
|
||||
struct EndGameEvent {
|
||||
pub reason: EndGameReason,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum EndGameReason {
|
||||
Die,
|
||||
Victory,
|
||||
}
|
||||
|
||||
fn no_kirbies_emetter(mut commands: Commands, query: Query<(), With<Kirby>>) {
|
||||
if query.is_empty() {
|
||||
commands.trigger(EndGameEvent { reason: EndGameReason::Die });
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GameState {
|
||||
#[default]
|
||||
Starting,
|
||||
Running,
|
||||
Paused,
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
|
||||
pub struct GameStartupSet;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct DispawnOnGameOver;
|
||||
|
||||
pub trait Resetable {
|
||||
fn reset(&mut self);
|
||||
}
|
||||
|
||||
fn reset_resource<T: Resource + Resetable>(mut res: ResMut<T>) {
|
||||
res.reset();
|
||||
}
|
||||
|
||||
#[derive(SystemParam)]
|
||||
struct GameRes<'w> {
|
||||
bubble_waves: ResMut<'w, BubbleWaves>,
|
||||
}
|
||||
|
||||
fn on_reset_world(
|
||||
_: On<ResetWorldEvent>,
|
||||
mut commands: Commands,
|
||||
entities: Query<Entity, With<DispawnOnGameOver>>,
|
||||
game_res: GameRes,
|
||||
) {
|
||||
for e in &entities {
|
||||
commands.entity(e).despawn();
|
||||
}
|
||||
reset_resource(game_res.bubble_waves);
|
||||
println!("Set state");
|
||||
commands.set_state(GameState::Starting);
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
struct ResetWorldEvent;
|
||||
|
||||
fn on_end_game(event: On<EndGameEvent>, mut commands: Commands) {
|
||||
let reason = event.reason;
|
||||
println!("end game reason {:?}", reason);
|
||||
commands.trigger(ResetWorldEvent);
|
||||
}
|
||||
|
||||
fn change_to_running_state(mut commands: Commands) {
|
||||
println!("Running state !");
|
||||
commands.set_state(GameState::Running);
|
||||
}
|
||||
|
||||
pub struct GameEventsPlugin;
|
||||
|
||||
impl Plugin for GameEventsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_state::<GameState>()
|
||||
.add_systems(Update, no_kirbies_emetter.run_if(in_state(GameState::Running)))
|
||||
.add_systems(OnEnter(GameState::Starting), change_to_running_state.after(GameStartupSet))
|
||||
.add_observer(on_end_game)
|
||||
.add_observer(on_reset_world);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue