summaryrefslogtreecommitdiff
path: root/src/systems/replace.rs
blob: 4ed516b5b966846aa0acdbd112d76a3abbaea3c0 (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

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

use crate::{
	components::{
		Position,
		Substitute,
		Removed,
		Serialise
	},
	resources::{NewEntities}
};


pub struct Replace;
impl <'a> System<'a> for Replace {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Position>,
		Write<'a, NewEntities>,
		ReadStorage<'a, Substitute>,
		WriteStorage<'a, Removed>,
		ReadStorage<'a, Serialise>
	);
	
	fn run(&mut self, (entities, positions, mut new, substitutes, mut removeds, serialisations): Self::SystemData) {
		for (entity, position, substitute, serialisation) in (&entities, &positions, &substitutes, (&serialisations).maybe()).join(){
			// todo: better error handling
			let mut template = substitute.into.clone();
			if let Some(serialise) = serialisation {
				// todo: extraction?
				template = template.merge(serialise.template.clone());
			}
			let preent = new.encyclopedia.construct(&template).unwrap();
			new.to_build.push((position.pos, preent));
			removeds.insert(entity, Removed).unwrap();
		}
	}
}