diff options
| author | troido <troido@protonmail.com> | 2020-04-13 14:16:35 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-13 14:16:35 +0200 |
| commit | ee200f3003acdfdfde1a746246a7a4669188eb18 (patch) | |
| tree | b15ac6b53f52e72b586c529fba550368e1ca6f68 | |
| parent | ab67714238c162646af10334715c6de41939c83f (diff) | |
spawners now also use timer component and triggers
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 10 | ||||
| -rw-r--r-- | content/maps/room.json | 2 | ||||
| -rw-r--r-- | src/components/messages.rs | 4 | ||||
| -rw-r--r-- | src/components/mod.rs | 4 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 6 | ||||
| -rw-r--r-- | src/systems/clear.rs | 13 | ||||
| -rw-r--r-- | src/systems/spawn.rs | 22 |
7 files changed, 33 insertions, 28 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index f95de5e..68acc02 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -193,12 +193,16 @@ "spawner": { "arguments": [["template", "template"], ["amount", "int", 1], ["delay", "int", 0], ["clan", "string", ""], ["initial_spawn", "bool", true]], "components": [ + ["Timer", { + "delay": ["arg", "delay"], + "spread": 0.9, + "trigger": "spawn", + "target_time": 0 + }], ["Spawner", { "template": ["arg", "template"], "amount": ["arg", "amount"], - "delay": ["arg", "delay"], - "clan": ["arg", "clan"], - "initial_spawn": ["arg", "initial_spawn"] + "clan": ["arg", "clan"] }] ] }, diff --git a/content/maps/room.json b/content/maps/room.json index 728ee4d..41663fc 100644 --- a/content/maps/room.json +++ b/content/maps/room.json @@ -44,7 +44,7 @@ "1": {"type": "portal", "kwargs": {"destination": "smallview"}}, "^": ["grass", "spiketrap"], "d": ["grass", {"type": "spawner", "kwargs": {"template": {"type": "dummy"}, "delay": 100}}], - "r": ["grass", {"type": "spawner", "kwargs": {"template": {"type": "rat"}, "amount": 3, "clan": "rats", "delay": 200}}], + "r": ["grass", {"type": "spawner", "kwargs": {"template": {"type": "rat"}, "amount": 10, "clan": "rats", "delay": 20}}], "V": ["grass", "radishplant"], "/": ["grass", "sword"], "D": ["ground", "closeddoor"], diff --git a/src/components/messages.rs b/src/components/messages.rs index aa928f0..2c0ffd5 100644 --- a/src/components/messages.rs +++ b/src/components/messages.rs @@ -79,6 +79,7 @@ pub enum Trigger { Loot, Remove, Build, + Spawn, // combination triggers Die, // Remove + Loot Change // Remove + Build @@ -88,9 +89,10 @@ impl Trigger { pub fn from_str(txt: &str) -> Option<Self> { Some(match txt { "loot" => Self::Loot, - "die" => Self::Die, "remove" => Self::Remove, "build" => Self::Build, + "spawn" => Self::Spawn, + "die" => Self::Die, "change" => Self::Change, _ => {return None} }) diff --git a/src/components/mod.rs b/src/components/mod.rs index b1faf08..35f757e 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -180,10 +180,8 @@ pub struct Home { #[storage(HashMapStorage)] pub struct Spawner { pub amount: usize, - pub delay: i64, pub clan: Clan, - pub template: Template, - pub last_spawn: Option<Timestamp> + pub template: Template } #[derive(Component, Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 996d922..8f493a3 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -164,10 +164,9 @@ components!( Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}}; Autofight () {Autofight::default()}; MonsterAI (move_chance: Float, homesickness: Float, view_distance: Int); - Spawner (amount: Int, delay: Int, clan: String, template: Template, initial_spawn: Bool) { + Spawner (amount: Int, clan: String, template: Template) { Spawner{ amount: amount as usize, - delay, clan: Clan{name: if clan == "" { format!("$random({})", rand::thread_rng().gen::<u32>()) @@ -175,8 +174,7 @@ components!( clan } }, - template: template.unsaved(), - last_spawn: if initial_spawn {Some(Timestamp(-delay))} else {None} + template: template.unsaved() } }; Clan (name: String); diff --git a/src/systems/clear.rs b/src/systems/clear.rs index 64588d3..8d15835 100644 --- a/src/systems/clear.rs +++ b/src/systems/clear.rs @@ -1,19 +1,24 @@ use specs::{ Write, + WriteStorage, System }; use crate::{ - resources::Ground + resources::Ground, + components::TriggerBox }; pub struct Clear; impl <'a> System<'a> for Clear { - type SystemData = - Write<'a, Ground>; - fn run(&mut self, mut ground: Self::SystemData) { + type SystemData = ( + Write<'a, Ground>, + WriteStorage<'a, TriggerBox> + ); + fn run(&mut self, (mut ground, mut triggerboxes): Self::SystemData) { ground.changes.clear(); + triggerboxes.clear(); } } diff --git a/src/systems/spawn.rs b/src/systems/spawn.rs index 903b9b2..ad9e8c1 100644 --- a/src/systems/spawn.rs +++ b/src/systems/spawn.rs @@ -5,7 +5,6 @@ use specs::{ WriteStorage, ReadStorage, Write, - Read, System, Join }; @@ -15,9 +14,11 @@ use crate::{ Position, Spawner, Clan, - Home + Home, + TriggerBox, + Trigger }, - resources::{NewEntities, Time}, + resources::{NewEntities}, componentwrapper::ComponentWrapper }; @@ -30,20 +31,19 @@ impl <'a> System<'a> for Spawn { Write<'a, NewEntities>, WriteStorage<'a, Spawner>, ReadStorage<'a, Clan>, - Read<'a, Time> + ReadStorage<'a, TriggerBox> ); - fn run(&mut self, (positions, mut new, mut spawners, clans, time): Self::SystemData) { + fn run(&mut self, (positions, mut new, mut spawners, clans, triggerboxes): 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 *clan_nums.get(&spawner.clan).unwrap_or(&0) < spawner.amount { - if let Some(last_spawn) = spawner.last_spawn { - if time.time > last_spawn + spawner.delay { - spawner.last_spawn = None; + for (spawner, position, triggerbox) in (&mut spawners, &positions, &triggerboxes).join() { + for message in triggerbox.messages.iter() { + if *message == Trigger::Spawn { + if *clan_nums.get(&spawner.clan).unwrap_or(&0) < spawner.amount { match new.encyclopedia.construct(&spawner.template) { Ok(mut preent) => { preent.push(ComponentWrapper::Clan(spawner.clan.clone())); @@ -53,8 +53,6 @@ impl <'a> System<'a> for Spawn { Err(err) => {println!("Error: can not spawn entity from spawner: {}", err);} } } - } else { - spawner.last_spawn = Some(time.time) } } } |
