summaryrefslogtreecommitdiff
path: root/src/systems.rs
blob: d00abc882e6383553e577fa76a2a58596f321bfd (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

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

use super::components::{
	Position,
	Visible,
	Controller
};

use super::controls::Control;

use super::resources::TopView;

pub struct Draw;

impl <'a> System<'a> for Draw {
	
	type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Visible>, Write<'a, TopView>);
	
	fn run(&mut self, (pos, vis, mut view): Self::SystemData) {
		view.cells.clear();
		for (pos, vis) in (&pos, &vis).join(){
			if pos.x >= 0 && pos.y >= 0 && pos.x < view.width && pos.y < view.height {
				view.cells.entry(*pos).or_insert(Vec::new()).push(vis.clone());
				view.cells.get_mut(pos).unwrap().sort_by(|a, b| b.height.partial_cmp(&a.height).unwrap());
			}
		}
	}
}

// struct Control;
// impl <'a> System <'a> for Control {
// 	type SystemData = WriteStorage<'a, Controller>;
// 	fn run (&mut self, mut controller: Self::SystemData) {
// 		for controller in &mut controller.join()
// 	}
// }

pub struct Move;
impl <'a> System<'a> for Move {
	type SystemData = (WriteStorage<'a, Controller>, WriteStorage<'a, Position>);
	fn run(&mut self, (mut controller, mut pos): Self::SystemData) {
		for (controller, pos) in (&mut controller, &mut pos).join(){
			match &controller.0 {
				Control::Move(direction) => {
					let (dx, dy) = direction.to_position();
					pos.x += dx;
					pos.y += dy;
				}
				_ => {}
			}
		}
	}
}


pub struct ClearControllers;
impl <'a> System<'a> for ClearControllers {
	type SystemData = (Entities<'a>, WriteStorage<'a, Controller>);
	fn run(&mut self, (entities, mut controllers): Self::SystemData) {
		let mut ents = Vec::new();
		for (ent, _controller) in (&*entities, &controllers).join() {
			ents.push(ent);
		}
		for ent in ents {
			controllers.remove(ent);
		}
	}
}