GameStates introduced

This commit is contained in:
Rendo 2025-11-16 13:51:34 +05:00
commit b7aff68e3a
7 changed files with 40 additions and 12 deletions

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use rand::{prelude::*, rng}; use rand::{prelude::*, rng};
use crate::{ use crate::{
FIRST_CORNER_X, FIRST_CORNER_Y, SECOND_CORNER_X, SECOND_CORNER_Y, FIRST_CORNER_X, FIRST_CORNER_Y, GameState, SECOND_CORNER_X, SECOND_CORNER_Y,
collision::{Collided, Collider}, collision::{Collided, Collider},
damagable::Damagable, damagable::Damagable,
projectile::Projectile, projectile::Projectile,
@ -15,8 +15,11 @@ pub struct AsteroidPlugin;
impl Plugin for AsteroidPlugin { impl Plugin for AsteroidPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_asteroids) app.add_systems(OnEnter(GameState::Game), setup_asteroids)
.add_systems(FixedUpdate, rotate_asteroid); .add_systems(
FixedUpdate,
rotate_asteroid.run_if(in_state(GameState::Game)),
);
} }
} }

View file

@ -1,10 +1,15 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::GameState;
pub struct CollisionPlugin; pub struct CollisionPlugin;
impl Plugin for CollisionPlugin { impl Plugin for CollisionPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(FixedUpdate, calculate_collisions); app.add_systems(
FixedUpdate,
calculate_collisions.run_if(in_state(GameState::Game)),
);
} }
} }

View file

@ -1,11 +1,16 @@
use bevy::prelude::*; use bevy::prelude::*;
use std::default::Default; use std::default::Default;
use crate::GameState;
pub struct DamagablePlugin; pub struct DamagablePlugin;
impl Plugin for DamagablePlugin { impl Plugin for DamagablePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(FixedPostUpdate, broken_ships_cleanup); app.add_systems(
FixedPostUpdate,
broken_ships_cleanup.run_if(in_state(GameState::Game)),
);
} }
} }

View file

@ -20,6 +20,13 @@ const FIRST_CORNER_Y: f32 = -512.;
const SECOND_CORNER_X: f32 = 512.; const SECOND_CORNER_X: f32 = 512.;
const SECOND_CORNER_Y: f32 = 512.; const SECOND_CORNER_Y: f32 = 512.;
#[derive(States, Debug, Clone, Hash, PartialEq, Eq)]
pub enum GameState {
InMenu,
Game,
Gameover,
}
fn main() { fn main() {
App::new() App::new()
.add_plugins( .add_plugins(
@ -37,6 +44,7 @@ fn main() {
}) })
.set(ImagePlugin::default_nearest()), .set(ImagePlugin::default_nearest()),
) )
.insert_state(GameState::Game)
.add_plugins(VelocityPlugin) .add_plugins(VelocityPlugin)
.add_plugins(CollisionPlugin) .add_plugins(CollisionPlugin)
.add_plugins(ShipsPlugin) .add_plugins(ShipsPlugin)

View file

@ -1,6 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ use crate::{
GameState,
collision::{Collided, Collider}, collision::{Collided, Collider},
damagable::Damagable, damagable::Damagable,
ships::Factions, ships::Factions,
@ -31,8 +32,11 @@ impl Projectile {
impl Plugin for ProjectilePlugin { impl Plugin for ProjectilePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, projectile_startup) app.add_systems(OnEnter(GameState::Game), projectile_startup)
.add_systems(FixedPreUpdate, projectile_despawn_countdown); .add_systems(
FixedPreUpdate,
projectile_despawn_countdown.run_if(in_state(GameState::Game)),
);
} }
} }

View file

@ -1,6 +1,6 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::projectile::spawn_projectile; use crate::GameState;
pub mod enemy; pub mod enemy;
pub mod player; pub mod player;
@ -39,11 +39,11 @@ pub struct ShipsPlugin;
impl Plugin for ShipsPlugin { impl Plugin for ShipsPlugin {
fn build(&self, app: &mut bevy::app::App) { fn build(&self, app: &mut bevy::app::App) {
app.insert_resource(Time::<Fixed>::from_hz(60.)) app.insert_resource(Time::<Fixed>::from_hz(60.))
.add_systems(Startup, startup) .add_systems(OnEnter(GameState::Game), startup)
.add_systems( .add_systems(
FixedUpdate, FixedUpdate,
( (
player::player_movement_system, player::player_movement_system.run_if(in_state(GameState::Game)),
player::player_shooting_system, player::player_shooting_system,
), ),
); );

View file

@ -1,14 +1,17 @@
use bevy::prelude::*; use bevy::prelude::*;
use std::default::Default; use std::default::Default;
use crate::{FIRST_CORNER_X, FIRST_CORNER_Y, SECOND_CORNER_X, SECOND_CORNER_Y}; use crate::{FIRST_CORNER_X, FIRST_CORNER_Y, GameState, SECOND_CORNER_X, SECOND_CORNER_Y};
pub struct VelocityPlugin; pub struct VelocityPlugin;
impl Plugin for VelocityPlugin { impl Plugin for VelocityPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(Time::<Fixed>::from_hz(60.0)) app.insert_resource(Time::<Fixed>::from_hz(60.0))
.add_systems(FixedUpdate, movement_system); .add_systems(
FixedUpdate,
movement_system.run_if(in_state(GameState::Game)),
);
} }
} }