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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
use std::collections::HashSet;
use specs::{
Entities,
ReadStorage,
WriteStorage,
System,
Join,
Read,
Write
};
use crate::{
components::{
Controller,
Position,
ControlCooldown,
Interactable,
Dead,
Removed,
Sound,
Ear,
Inventory
},
controls::{Control},
resources::{Ground, NewEntities}
};
pub struct Interact;
impl <'a> System<'a> for Interact {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Controller>,
ReadStorage<'a, Position>,
Read<'a, Ground>,
WriteStorage<'a, ControlCooldown>,
ReadStorage<'a, Interactable>,
WriteStorage<'a, Dead>,
WriteStorage<'a, Removed>,
Write<'a, NewEntities>,
WriteStorage<'a, Ear>,
WriteStorage<'a, Inventory>
);
fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads, mut removeds, mut new, mut ears, mut inventories): Self::SystemData) {
for (entity, controller, position) in (&entities, &controllers, &positions).join(){
let mut target = None;
let ear = ears.get_mut(entity);
match &controller.control {
Control::Interact(directions, arg) => {
'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(interactable) = interactables.get(*ent) {
if interactable.accepts_arg(arg){
target = Some((*ent, interactable, pos, arg.clone()));
break 'targets;
}
}
}
}
}
_ => {}
}
if let Some((ent, interactable, pos, arg)) = target {
match interactable {
Interactable::Harvest => {
deads.insert(ent, Dead).unwrap();
}
Interactable::Change(into) => {
new.create(pos, into).unwrap();
removeds.insert(ent, Removed).unwrap();
}
Interactable::Say(text) => {
say(ear, text.clone());
}
Interactable::Reply(text) => {
say(ear, text.replace("{}", &arg.unwrap()));
}
Interactable::Exchange(prefix, exchanges) => {
if let Some(txt) = arg {
if let Some(inventory) = inventories.get_mut(entity) {
if txt.starts_with(prefix){
let action = txt.split_at(prefix.len()).1;
if let Some(exchange) = exchanges.get(action) {
if exchange.can_trade(inventory){
exchange.trade(inventory, &new.encyclopedia);
say(ear, format!("Success! '{}' ({})", txt, exchange.show()));
} else {
say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()));
}
} else {
say(ear, format!("Invalid option: {}", action));
}
}
}
} else {
say(ear, format!("options: {:?}", exchanges.iter().map(|(id, exchange)|
format!("{}{}: {}", prefix, id, exchange.show())
).collect::<Vec<String>>())
);
}
}
}
cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap();
}
}
}
}
fn say(maybe_ear: Option<&mut Ear>, text: String){
if let Some(ear) = maybe_ear {
ear.sounds.push(Sound{source: None, text});
}
}
|