summaryrefslogtreecommitdiff
path: root/src/systems/moving.rs
blob: daa1eb45655125b36d2c4eed025843dfb0f945bf (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
78
79
80
81

use std::collections::HashSet;

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

use crate::{
	Pos,
	components::{
		Controller,
		Blocking,
		Position,
		Floor,
		Moved,
		Entered
	},
	controls::{
		Control
	},
	resources::{
		Size,
		Ground
	},
};


pub struct Move;
impl <'a> System<'a> for Move {
	type SystemData = (
		Entities<'a>,
		ReadStorage<'a, Controller>,
		WriteStorage<'a, Position>,
		Read<'a, Size>,
		ReadStorage<'a, Blocking>,
		Write<'a, Ground>,
		ReadStorage<'a, Floor>,
		WriteStorage<'a, Moved>,
		WriteStorage<'a, Entered>
	);
	
	fn run(&mut self, (entities, controllers, mut positions, size, blocking, mut ground, floor, mut moved, mut entered): Self::SystemData) {
		moved.clear();
		entered.clear();
		for (ent, controller, mut pos) in (&entities, &controllers, &mut positions.restrict_mut()).join(){
			match &controller.0 {
				Control::Move(direction) => {
					let newpos = (pos.get_unchecked().pos + direction.to_position()).clamp(Pos::new(0, 0), Pos::new(size.width - 1, size.height - 1));
					let mut blocked = false;
					let mut on_floor = false;
					for ent in ground.cells.get(&newpos).unwrap_or(&HashSet::new()) {
						if blocking.get(*ent).is_some(){
							blocked = true;
							break;
						}
						if floor.get(*ent).is_some(){
							on_floor = true;
						}
					}
					if !blocked && on_floor {
						let mut pos_mut = pos.get_mut_unchecked();
						moved.insert(ent, Moved{from: pos_mut.pos}).expect("can't insert Moved");
						ground.cells.get_mut(&pos_mut.pos).unwrap().remove(&ent);
						pos_mut.pos = newpos;
						ground.cells.entry(newpos).or_insert_with(HashSet::new).insert(ent);
						for ent in ground.cells.get(&newpos).unwrap() {
							let _ = entered.insert(*ent, Entered);
						}
					}
				}
				_ => {}
			}
		}
	}
}