diff options
| author | troido <troido@protonmail.com> | 2020-03-03 13:36:44 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-03 13:36:44 +0100 |
| commit | b2157791dfcaab18ec7f8ebb958341fe325cf419 (patch) | |
| tree | f94e638573878599b0ee108a0b06fbe1c8f69ef9 /src/systems/spawn.rs | |
| parent | a8d0e075613cc973b66c37510103108362fe7d3d (diff) | |
added spawners an not-saved assemblages/templates
Diffstat (limited to 'src/systems/spawn.rs')
| -rw-r--r-- | src/systems/spawn.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/systems/spawn.rs b/src/systems/spawn.rs new file mode 100644 index 0000000..6bb4912 --- /dev/null +++ b/src/systems/spawn.rs @@ -0,0 +1,50 @@ + +use std::collections::HashMap; + +use specs::{ + WriteStorage, + ReadStorage, + Write, + Read, + System, + Join +}; + +use crate::{ + components::{ + Position, + Spawner, + Clan + }, + resources::{NewEntities, TimeStamp}, + componentwrapper::ComponentWrapper +}; + + + +pub struct Spawn; +impl <'a> System<'a> for Spawn { + type SystemData = ( + ReadStorage<'a, Position>, + Write<'a, NewEntities>, + WriteStorage<'a, Spawner>, + ReadStorage<'a, Clan>, + Read<'a, TimeStamp> + ); + + fn run(&mut self, (positions, mut new, mut spawners, clans, time): Self::SystemData) { + let mut clan_nums: HashMap<&Clan, usize> = HashMap::new(); + for clan in (&clans).join() { + let n: usize = *clan_nums.entry(clan).or_insert(0); + clan_nums.insert(clan, n+1); + } + for (spawner, position) in (&mut spawners, &positions).join() { + if time.time > spawner.last_spawn + spawner.delay && *clan_nums.get(&spawner.clan).unwrap_or(&0) < spawner.amount { + spawner.last_spawn = time.time; + let mut preent = new.encyclopedia.construct(&spawner.template).expect("unable to spawn entity from spawner"); + preent.push(ComponentWrapper::Clan(spawner.clan.clone())); + new.to_build.push((position.pos, preent)); + } + } + } +} |
