Skip to content

Commit 3bf66e7

Browse files
committed
✨ states
1 parent a26ff73 commit 3bf66e7

File tree

7 files changed

+70
-27
lines changed

7 files changed

+70
-27
lines changed

src/castles.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
common::Rewards,
1010
health::Health,
1111
racks::Rack,
12+
states::GameState,
1213
teams::{Team, Teams},
1314
};
1415

@@ -65,8 +66,8 @@ pub struct CastlesPlugin;
6566

6667
impl Plugin for CastlesPlugin {
6768
fn build(&self, app: &mut App) {
68-
app.add_systems(Startup, setup)
69-
.add_systems(PostUpdate, destroy);
69+
app.add_systems(OnEnter(GameState::Game), setup)
70+
.add_systems(PostUpdate, destroy.run_if(in_state(GameState::Game)));
7071
}
7172
}
7273

src/main.rs

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod minions;
66
mod physics;
77
mod player;
88
mod racks;
9+
mod states;
910
mod teams;
1011

1112
use audio::AudioPlugin;
@@ -23,6 +24,7 @@ use minions::MinionsPlugin;
2324
use physics::PhysicsPlugin;
2425
use player::LocalPlayerPlugin;
2526
use racks::RacksPlugin;
27+
use states::GameState;
2628
use teams::TeamsPlugin;
2729
use xxhash_rust::xxh3::xxh3_64;
2830

@@ -51,6 +53,9 @@ fn main() {
5153
}),
5254
RngPlugin::new().with_rng_seed(xxh3_64(seed)),
5355
PhysicsPlugin,
56+
))
57+
.add_state::<GameState>()
58+
.add_plugins((
5459
TeamsPlugin,
5560
MinionsPlugin,
5661
RacksPlugin,
@@ -74,5 +79,20 @@ fn main() {
7479
gravity: Vec2::new(0.0, 0.0),
7580
..default()
7681
})
82+
.insert_resource(GameTimer(Timer::from_seconds(1.0, TimerMode::Once)))
83+
.add_systems(Update, countdown)
7784
.run();
7885
}
86+
87+
#[derive(Resource, Deref, DerefMut)]
88+
struct GameTimer(Timer);
89+
90+
fn countdown(
91+
mut game_state: ResMut<NextState<GameState>>,
92+
time: Res<Time>,
93+
mut timer: ResMut<GameTimer>,
94+
) {
95+
if timer.tick(time.delta()).finished() {
96+
game_state.set(GameState::Game);
97+
}
98+
}

src/minions.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{common::*, health::Health, physics::CollisionEvent, teams::Team};
1+
use crate::{common::*, health::Health, physics::CollisionEvent, states::GameState, teams::Team};
22
use bevy::{
33
prelude::*,
44
sprite::MaterialMesh2dBundle,
@@ -40,9 +40,13 @@ impl Plugin for MinionsPlugin {
4040
check_collisions_minions,
4141
decay_life,
4242
explosion_damage,
43-
),
43+
)
44+
.run_if(in_state(GameState::Game)),
4445
)
45-
.add_systems(PostUpdate, (destroy_minions, destroy_after_timer));
46+
.add_systems(
47+
PostUpdate,
48+
(destroy_minions, destroy_after_timer).run_if(in_state(GameState::Game)),
49+
);
4650
}
4751
}
4852

src/physics.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::{collections::HashMap, hash::Hash, hash::Hasher};
33
use bevy::prelude::*;
44
use bevy_rapier2d::prelude::RapierContext;
55

6+
use crate::states::GameState;
7+
68
#[derive(Event)]
79
pub enum CollisionEvent {
810
/// Event occurring when two colliders start colliding
@@ -77,7 +79,10 @@ impl Plugin for PhysicsPlugin {
7779
fn build(&self, app: &mut App) {
7880
app.insert_resource(Collisions::new())
7981
.add_event::<CollisionEvent>()
80-
.add_systems(PostUpdate, check_collisions);
82+
.add_systems(
83+
PostUpdate,
84+
check_collisions.run_if(in_state(GameState::Game)),
85+
);
8186
}
8287
}
8388

src/player.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::common::*;
22
use crate::health::Health;
33
use crate::physics::CollisionEvent;
44
use crate::racks::{RackBundle, RACK_GOLD_VALUE};
5+
use crate::states::GameState;
56
use crate::teams::{Team, Teams};
67
use bevy::sprite::MaterialMesh2dBundle;
78
use bevy::window::PrimaryWindow;
@@ -76,24 +77,26 @@ pub struct LocalPlayerPlugin;
7677

7778
impl Plugin for LocalPlayerPlugin {
7879
fn build(&self, app: &mut App) {
79-
app.add_systems(Startup, (setup, setup_ui)).add_systems(
80-
Update,
81-
(
82-
// movements
83-
update_axes,
84-
keyboard_movements,
85-
mouse_movements,
86-
// actions
87-
update_button_values,
88-
mouse_actions,
89-
keyboard_actions,
90-
// others
91-
check_collisions_sword,
92-
update_ui,
93-
update_sword,
94-
update_cooldowns,
95-
),
96-
);
80+
app.add_systems(OnEnter(GameState::Game), (setup, setup_ui))
81+
.add_systems(
82+
Update,
83+
(
84+
// movements
85+
update_axes,
86+
keyboard_movements,
87+
mouse_movements,
88+
// actions
89+
update_button_values,
90+
mouse_actions,
91+
keyboard_actions,
92+
// others
93+
check_collisions_sword,
94+
update_ui,
95+
update_sword,
96+
update_cooldowns,
97+
)
98+
.run_if(in_state(GameState::Game)),
99+
);
97100
}
98101
}
99102

src/racks.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
common::Rewards,
1313
health::Health,
1414
minions::MinionBundle,
15+
states::GameState,
1516
teams::{Team, Teams},
1617
};
1718

@@ -80,9 +81,9 @@ pub struct RacksPlugin;
8081

8182
impl Plugin for RacksPlugin {
8283
fn build(&self, app: &mut App) {
83-
app.add_systems(Startup, setup)
84-
.add_systems(Update, spawn_minions)
85-
.add_systems(PostUpdate, destroy);
84+
app.add_systems(OnEnter(GameState::Game), setup)
85+
.add_systems(Update, spawn_minions.run_if(in_state(GameState::Game)))
86+
.add_systems(PostUpdate, destroy.run_if(in_state(GameState::Game)));
8687
}
8788
}
8889

src/states.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bevy::ecs::schedule::States;
2+
3+
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
4+
pub enum GameState {
5+
#[default]
6+
Splash,
7+
// Pause,
8+
Game,
9+
}

0 commit comments

Comments
 (0)