summaryrefslogtreecommitdiff
path: root/src/systems/growth.rs
blob: b1b1af54d5b279aa968a55d38642895c5065d17d (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

use rand;

use specs::{
	WriteStorage,
	Entities,
	System,
	Join,
	Read
};

use crate::{
	components::{
		Grow,
		OwnTime,
		TriggerBox
	},
	resources::{Time}
};


pub struct Growth;
impl <'a> System<'a> for Growth{
	type SystemData = (
		Entities<'a>,
		WriteStorage<'a, Grow>,
		WriteStorage<'a, TriggerBox>,
		Read<'a, Time>,
		WriteStorage<'a, OwnTime>
	);
	
	fn run(&mut self, (entities, mut grows, mut triggerboxes, time, mut own_times): Self::SystemData) {
		for (entity, grow) in (&entities, &mut grows).join(){
			if grow.target_time == None {
				let creation_time = own_times.get(entity).map(|ct| ct.time).unwrap_or(time.time);
				let duration = grow.delay as f64 * (1.0 + rand::random::<f64>()) / (if rand::random() {1.0} else {2.0});
				grow.target_time = Some(creation_time + duration as i64);
			}
			let target_time = grow.target_time.unwrap();
			if target_time <= time.time {
				if target_time + 1 < time.time {
					own_times.insert(entity, OwnTime{time: target_time + 1}).unwrap();
				}
				TriggerBox::add_message(&mut triggerboxes, entity, grow.trigger);
			}
		}
	}
}