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

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

use crate::{
	ComponentWrapper,
	components::{
		Position,
		Build,
		Trigger,
		TriggerBox,
		TimeOffset
	},
	resources::{NewEntities},
};


pub struct Building;
impl <'a> System<'a> for Building{
	type SystemData = (
		ReadStorage<'a, Position>,
		Write<'a, NewEntities>,
		ReadStorage<'a, TriggerBox>,
		ReadStorage<'a, Build>,
		ReadStorage<'a, TimeOffset>
	);
	
	fn run(&mut self, (positions, mut new, triggerboxes, builds, time_offsets): Self::SystemData) {
		for (position, triggerbox, build, time_offset) in (&positions, &triggerboxes, &builds, (&time_offsets).maybe()).join(){
			if triggerbox.has_message(&[Trigger::Build, Trigger::Change]) {
				// todo: better error handling
				let mut preent = new.encyclopedia.construct(&build.obj).unwrap();
				if let Some(time) = time_offset {
					preent.push(ComponentWrapper::TimeOffset(time.clone()));
				}
				new.to_build.push((position.pos, preent));
			}
		}
	}
}