Skip to content

Commit

Permalink
make difficulty increase with wave n
Browse files Browse the repository at this point in the history
  • Loading branch information
caengen committed Jul 24, 2023
1 parent ac4f92d commit a830038
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/enter_stage/systems.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::time::Duration;

use crate::{
game::prelude::{EnemySpawn, SplitTimer, Stage, StageHandle, Wave},
game::prelude::{EnemySpawn, SplitTimer, Stage, StageHandle, Wave, WaveSpawnCount},
GameState,
};
use bevy::prelude::*;
Expand Down Expand Up @@ -31,6 +31,7 @@ pub fn setup_resources(
Duration::from_millis((stage.split_interval_secs(0) * 1000.0) as u64),
TimerMode::Repeating,
)));
commands.insert_resource(WaveSpawnCount(0));
}

pub fn show_stage_intro(
Expand Down
3 changes: 2 additions & 1 deletion src/game/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy::{prelude::*, utils::HashSet};
use derive_more::From;

pub const PLAYER_MISSILE_SPEED: f32 = 250.0;
pub const MAX_AMMO: u8 = 30;

#[derive(From)]
pub enum Scoring {
Expand Down Expand Up @@ -171,7 +172,7 @@ pub struct TextColor;
#[derive(Component)]
pub struct DropBombTimer(pub Timer);
#[derive(Component)]
pub struct MissileReserve(pub usize);
pub struct MissileReserve(pub u8);
#[derive(Component)]
pub struct City;
#[derive(Component)]
Expand Down
13 changes: 9 additions & 4 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use self::{
systems::{
ammo_ui, animate_sprite_indices, animate_sprite_steps, change_colors, defeat, drop_bombs,
explode_city, explosion_event_listener_system, explosion_system, flame_engulf_system,
game_keys, game_over_ui, gizmo_missile_trails, missile_arrival_event_listner, move_cursor,
move_missile, move_ufo, reset_game_listener, rotate_player, score_ui, setup_player,
spawn_enemies, split_missiles, teardown, wave_ui,
game_keys, game_over_ui, gizmo_missile_trails, is_wave_finished,
missile_arrival_event_listner, move_cursor, move_missile, move_ufo, reset_game_listener,
rotate_player, score_ui, setup_player, spawn_enemies, split_missiles, teardown,
wave_complete, wave_ui,
},
};
use crate::GameState;
Expand Down Expand Up @@ -46,7 +47,9 @@ impl Plugin for GamePlugin {
flick_system,
change_colors,
(
spawn_enemies,
spawn_enemies.run_if(
in_state(GameState::InGame).and_then(not(is_wave_finished)),
),
split_missiles,
move_missile,
gizmo_missile_trails,
Expand All @@ -63,6 +66,8 @@ impl Plugin for GamePlugin {
rotate_player,
defeat,
stage_colors.after(spawn_enemies),
(wave_complete)
.run_if(in_state(GameState::InGame).and_then(is_wave_finished)),
)
.run_if(in_state(GameState::InGame)),
// run these systems if we are in the GameOver state
Expand Down
2 changes: 2 additions & 0 deletions src/game/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,5 @@ pub struct EnemySpawn(pub Timer);

#[derive(Resource)]
pub struct SplitTimer(pub Timer);
#[derive(Resource)]
pub struct WaveSpawnCount(pub usize);
33 changes: 29 additions & 4 deletions src/game/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use super::{
City, Cursor, Destroyed, DropBombTimer, Enemy, Engulfable, Explodable, Explosion,
ExplosionEvent, ExplosionMode, FlameRadius, Foreground, Health, IdCounter, Missile,
MissileArrivalEvent, MissileReserve, Player, Score, Scoring, SpawnPoint, Stepper,
TargetLock, Ufo, PLAYER_MISSILE_SPEED,
TargetLock, Ufo, MAX_AMMO, PLAYER_MISSILE_SPEED,
},
effects::{Flick, TimedRemoval},
prelude::{color_from_vec, EnemySpawn, SplitTimer, Stage, StageHandle, Wave},
prelude::{color_from_vec, EnemySpawn, SplitTimer, Stage, StageHandle, Wave, WaveSpawnCount},
};

pub fn game_keys(
Expand Down Expand Up @@ -198,6 +198,28 @@ pub fn split_missiles(
}
}

pub fn is_wave_finished(
stage: Res<StageHandle>,
stages: Res<Assets<Stage>>,
wave: Res<Wave>,
spawn_count: Res<WaveSpawnCount>,
) -> bool {
let stage = stages.get(&stage.0).unwrap();
stage.enemies_count(wave.0) <= spawn_count.0
}

pub fn wave_complete(
mut wave: ResMut<Wave>,
mut spawn_count: ResMut<WaveSpawnCount>,
mut missile_ammo: Query<&mut MissileReserve, With<Player>>,
) {
wave.0 += 1;
spawn_count.0 = 0;
for mut ammo in missile_ammo.iter_mut() {
ammo.0 = MAX_AMMO;
}
}

pub fn spawn_enemies(
mut id_counter: ResMut<IdCounter>,
mut commands: Commands,
Expand All @@ -208,6 +230,7 @@ pub fn spawn_enemies(
stage: Res<StageHandle>,
stages: Res<Assets<Stage>>,
wave: Res<Wave>,
mut spawn_count: ResMut<WaveSpawnCount>,
) {
enemy_spawn.0.tick(time.delta());

Expand All @@ -217,12 +240,13 @@ pub fn spawn_enemies(

enemy_spawn.0.reset();

let mut rng = RngComponent::from(&mut global_rng);
let stage = stages.get(&stage.0).unwrap();
let mut rng = RngComponent::from(&mut global_rng);

// spawn ufo
if rng.chance(stage.ufo_chance(wave.0)) {
spawner::ufo(&mut commands, &mut rng, images.cursor.clone());
spawn_count.0 += 1;
}

for _ in 0..=rng.usize(stage.missile_spawn_min(wave.0)..stage.missile_spawn_max(wave.0)) {
Expand All @@ -234,6 +258,7 @@ pub fn spawn_enemies(
&stage,
None,
);
spawn_count.0 += 1;
}
}

Expand Down Expand Up @@ -652,7 +677,7 @@ pub fn setup_player(mut commands: Commands, images: Res<ImageAssets>) {
Player,
Cannon,
Health { max: 3, current: 3 },
MissileReserve(30),
MissileReserve(MAX_AMMO),
Foreground,
))
.id();
Expand Down

0 comments on commit a830038

Please sign in to comment.