summaryrefslogtreecommitdiff
path: root/src/systems/spawn.rs
blob: 903b9b27cd4e40f18af4282545568da94a3fc624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

use std::collections::HashMap;

use specs::{
	WriteStorage,
	ReadStorage,
	Write,
	Read,
	System,
	Join
};

use crate::{
	components::{
		Position,
		Spawner,
		Clan,
		Home
	},
	resources::{NewEntities, Time},
	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, Time>
	);
	
	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 *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;
						match new.encyclopedia.construct(&spawner.template) {
							Ok(mut preent) => {
								preent.push(ComponentWrapper::Clan(spawner.clan.clone()));
								preent.push(ComponentWrapper::Home(Home{home: position.pos}));
								new.to_build.push((position.pos, preent));
							}
							Err(err) => {println!("Error: can not spawn entity from spawner: {}", err);}
						}
					}
				} else {
					spawner.last_spawn = Some(time.time)
				}
			}
		}
	}
}