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

use std::collections::HashSet;

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

use crate::{
	components::{
		Controller,
		Position,
		Talkable,
		Notification,
		Ear,
		Visible
	},
	controls::{Control},
	resources::{Ground},
};

pub struct Talk;
impl <'a> System<'a> for Talk {
	type SystemData = (
		ReadStorage<'a, Controller>,
		ReadStorage<'a, Position>,
		Read<'a, Ground>,
		ReadStorage<'a, Talkable>,
		WriteStorage<'a, Ear>,
		ReadStorage<'a, Visible>
	);
	
	fn run(&mut self, (controllers, positions, ground, talkables, mut ears, visibles): Self::SystemData) {
		for (controller, position, ear) in (&controllers, &positions, &mut ears).join(){
			match &controller.control {
				Control::Interact(directions, None) => {
					'targets: for direction in directions {
						let pos = position.pos + direction.to_position();
						for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) {
							if let Some(Talkable{text}) = talkables.get(*ent) {
								let name = visibles.get(*ent).map(|v| v.name.clone());
								ear.sounds.push(Notification::Sound{text: text.clone(), source: name});
								break 'targets;
							}
						}
					}
				}
				_ => {}
			}
		}
	}
}