summaryrefslogtreecommitdiff
path: root/src/systems/create.rs
blob: 8463f9ee7dcde9d802df7f15a1ca6fde322ad668 (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::{
	Read,
	Write,
	WriteStorage,
	System,
	Join,
	Entities,
	LazyUpdate,
	Builder
};

use crate::components::{New, Position};
use crate::resources::{NewEntities};



pub struct Create;
impl <'a> System<'a> for Create {
	type SystemData = (
		Entities<'a>,
		Write<'a, NewEntities>,
		Read<'a, LazyUpdate>,
		WriteStorage<'a, New>
	);
	
	fn run(&mut self, (entities, mut new_entities, updater, mut new): Self::SystemData) {
		{
			let mut ents = Vec::new();
			for (ent, _new) in (&entities, &new).join() {
				ents.push(ent);
			}
			for ent in ents {
				new.remove(ent);
			}
		}
		for (pos, preentity) in &new_entities.to_build {
			let mut builder = updater.create_entity(&entities);
			for comp in preentity {
				builder = comp.build(builder);
			}
			builder.with(Position::new(*pos)).with(New).build();
		}
		new_entities.to_build.clear();
	}
}