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
|
use specs::{
Entities,
ReadStorage,
WriteStorage,
System,
Join,
Read,
Write
};
use crate::{
components::{
Controller,
Position,
Exchanger,
Notification,
Ear,
Inventory,
Visible
},
controls::{Control},
resources::{Ground, NewEntities},
util::strip_prefix
};
pub struct Exchange;
impl <'a> System<'a> for Exchange {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Controller>,
ReadStorage<'a, Position>,
Read<'a, Ground>,
ReadStorage<'a, Exchanger>,
Write<'a, NewEntities>,
WriteStorage<'a, Ear>,
WriteStorage<'a, Inventory>,
ReadStorage<'a, Visible>
);
fn run(&mut self, (entities, controllers, positions, ground, exchangers, new, mut ears, mut inventories, visibles): Self::SystemData) {
for (actor, controller, position) in (&entities, &controllers, &positions).join(){
let ear = ears.get_mut(actor);
match &controller.control {
Control::Interact(directions, arg) => {
for (ent, exchanger) in ground.components_near(position.pos, directions, &exchangers) {
let prefix = exchanger.prefix.as_str();
let name = visibles.get(ent).map(|v| v.name.as_str());
if let Some(txt) = arg {
if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) {
if let Some(exchange) = exchanger.exchanges.get(action) {
if exchange.can_trade(inventory){
exchange.trade(inventory, &new.encyclopedia);
say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name);
} else {
say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name);
}
} else {
say(ear, format!("Invalid option: {}", action), name);
}
break;
}
} else if let Some(ear) = ear {
ear.sounds.push(Notification::Options{
description: "".to_string(),
options: exchanger.exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect()
});
break;
}
}
}
_ => {}
}
}
}
}
fn say(maybe_ear: Option<&mut Ear>, text: String, source: Option<&str>){
if let Some(ear) = maybe_ear {
ear.sounds.push(Notification::Sound{text, source: source.map(|s| s.to_string())});
}
}
|