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

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

use crate::{
	components::{
		Controller,
		Position,
		Flags,
		Flag,
		Moved,
		Entered,
		Movable,
		ControlCooldown
	},
	controls::{
		Control
	},
	resources::{
		Ground
	},
};


pub struct Move;
impl <'a> System<'a> for Move {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		ReadStorage<'a, Flags>,
		Write<'a, Ground>,
		WriteStorage<'a, Moved>,
		WriteStorage<'a, Entered>,
		ReadStorage<'a, Movable>,
		WriteStorage<'a, ControlCooldown>
	);
	
	fn run(&mut self, (entities, controllers, mut positions, flags, mut ground, mut moved, mut entered, movables, mut cooldowns): Self::SystemData) {
		moved.clear();
		entered.clear();
		for (ent, controller, mut position, movable) in (&entities, &controllers, &mut positions, &movables).join(){
			if let Control::Move(direction) = &controller.control {
				let newpos = position.pos + direction.to_position();
				let ground_flags = ground.flags_on(newpos, &flags);
				if !ground_flags.contains(&Flag::Blocking) && ground_flags.contains(&Flag::Floor) {
					moved.insert(ent, Moved{from: position.pos}).expect("can't insert Moved");
					ground.remove(&position.pos, ent);
					position.pos = newpos;
					ground.insert(newpos, ent);
					for ent in ground.cells.get(&newpos).unwrap() {
						let _ = entered.insert(*ent, Entered);
					}
					cooldowns.insert(ent, ControlCooldown{amount: movable.cooldown}).unwrap();
				}
			}
		}
	}
}