-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add periodically spawning powerups to FFA and CTF (#156)
Now that support for powerups has been implemented we can start adding them to the game modes. This PR adds all the normal periodically-spawning powerups for both FFA and CTF (the 3 infernos, and the base shields for FFA).
- Loading branch information
1 parent
783ffef
commit c874876
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use crate::protocol::{MobType, Position}; | ||
use crate::{event::Frame, Entity, EventHandler}; | ||
|
||
#[derive(Copy, Clone)] | ||
enum SpawnerState { | ||
Spawned(Entity), | ||
Unspawned(Instant), | ||
} | ||
|
||
pub struct PeriodicPowerupSpawner { | ||
state: SpawnerState, | ||
interval: Duration, | ||
mob: MobType, | ||
pos: Position, | ||
} | ||
|
||
impl PeriodicPowerupSpawner { | ||
pub fn new(mob: MobType, pos: Position, interval: Duration) -> Self { | ||
Self { | ||
mob, | ||
pos, | ||
interval, | ||
state: SpawnerState::Unspawned(Instant::now()), | ||
} | ||
} | ||
|
||
pub fn inferno(pos: Position, interval: Duration) -> Self { | ||
Self::new(MobType::Inferno, pos, interval) | ||
} | ||
|
||
pub fn shield(pos: Position, interval: Duration) -> Self { | ||
Self::new(MobType::Shield, pos, interval) | ||
} | ||
} | ||
|
||
impl EventHandler<Frame> for PeriodicPowerupSpawner { | ||
fn on_event(&mut self, _: &Frame, game: &mut crate::AirmashGame) { | ||
let frame = game.this_frame(); | ||
|
||
match self.state { | ||
SpawnerState::Spawned(entity) => { | ||
if !game.world.contains(entity) { | ||
self.state = SpawnerState::Unspawned(frame + self.interval); | ||
} | ||
} | ||
SpawnerState::Unspawned(next) => { | ||
if frame > next { | ||
let entity = game.spawn_mob(self.mob, self.pos, Duration::from_secs(60)); | ||
self.state = SpawnerState::Spawned(entity); | ||
} | ||
} | ||
} | ||
} | ||
} |