summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--content/encyclopediae/default_encyclopedia.json2
-rw-r--r--content/encyclopediae/npcs.json15
-rw-r--r--content/maps/room.json3
-rw-r--r--src/assemblage.rs2
-rw-r--r--src/componentparameter.rs7
-rw-r--r--src/components/interactable.rs41
-rw-r--r--src/components/mod.rs2
-rw-r--r--src/componentwrapper.rs24
-rw-r--r--src/encyclopedia.rs2
-rw-r--r--src/parameter.rs32
-rw-r--r--src/resources/ground.rs16
-rw-r--r--src/room.rs10
-rw-r--r--src/systems/exchange.rs82
-rw-r--r--src/systems/interact.rs45
-rw-r--r--src/systems/mod.rs6
-rw-r--r--src/systems/talk.rs50
-rw-r--r--src/template.rs4
17 files changed, 235 insertions, 108 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json
index f6a3e48..fdaa42f 100644
--- a/content/encyclopediae/default_encyclopedia.json
+++ b/content/encyclopediae/default_encyclopedia.json
@@ -87,7 +87,7 @@
"height": 1,
"flags": ["Occupied"],
"components": [
- ["Interactable", {"action": ["interaction", ["say", "Good morning there, World"]]}]
+ ["Talkable", {"text": "Good morning there, World"}]
]
},
"quarry": {
diff --git a/content/encyclopediae/npcs.json b/content/encyclopediae/npcs.json
index bfaa6fe..78dda29 100644
--- a/content/encyclopediae/npcs.json
+++ b/content/encyclopediae/npcs.json
@@ -79,18 +79,21 @@
"height": 1.5,
"flags": ["Occupied"],
"components": [
- ["Interactable", {"action": ["interaction", ["reply", "did you say '{}'?"]]}]
+ ["Talkable", {"text": "Hey there, welcome to Asciifarm"}]
]
},
"trader": {
"sprite": "human",
"height": 1.5,
"components": [
- ["Interactable", {"action": ["interaction", ["exchange", ["buy ", {
- "pebble": [["radish", "radish"], ["pebble"]],
- "radishseed": [["radish"], ["radishseed", "radishseed"]],
- "carrotseed": [["radish"], ["carrotseed"]]
- }]]]}]
+ ["Exchanger", {
+ "prefix": "buy",
+ "exchanges": ["list", [
+ ["list", ["pebble", ["list", ["radish", "radish"]], ["list", ["pebble"]]]],
+ ["list", ["radishseed", ["list", ["radish"]], ["list", ["radishseed", "radishseed"]]]],
+ ["list", ["carrotseed", ["list", ["radish"]], ["list", ["carrotseed"]]]]
+ ]]
+ }]
]
}
}
diff --git a/content/maps/room.json b/content/maps/room.json
index 74cfcc0..173f79e 100644
--- a/content/maps/room.json
+++ b/content/maps/room.json
@@ -13,7 +13,7 @@
" ~~,,,,.,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,,X",
" X,,,,,.,,,,,,,,,,,~~~~,,,,,,T,,,,,,,,,,,,X",
" X,,,,,.,,,,,,,,,,,,~~~,,,,,,,,,,,,,,,,,,,X",
- " X,,,,,.,,,,,,,,,,,,~~~,,,,,T,,,,######,,,X",
+ " X,,,,,.,u,,,,,,,,,,~~~,,,,,T,,,,######,,,X",
" X,,,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X",
" X,,,t..............bbb..........D++++#,,,X",
" X,,,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X",
@@ -52,7 +52,6 @@
"u": ["ground", "dude"],
"t": ["ground", "trader"],
"P": ["ground", "pickaxe"],
- "u": ["ground", "radishes"],
"Q": "quarry",
" ": []
}
diff --git a/src/assemblage.rs b/src/assemblage.rs
index 2b60ea3..c88b39b 100644
--- a/src/assemblage.rs
+++ b/src/assemblage.rs
@@ -37,7 +37,7 @@ impl Assemblage {
(
key.clone(),
typ,
- Some(Parameter::from_typed_json(typ, def).ok_or(perr!("invalid argument default {:?} {:?}", typ, def))?)
+ Some(Parameter::from_typed_json(typ, def)?)
)
);
} else {
diff --git a/src/componentparameter.rs b/src/componentparameter.rs
index 333c6e1..08c3244 100644
--- a/src/componentparameter.rs
+++ b/src/componentparameter.rs
@@ -76,14 +76,12 @@ impl ComponentParameter {
pub fn from_json(value: &Value) -> PResult<Self> {
if !value.is_array() {
- return Ok(Self::Constant(Parameter::guess_from_json(value).ok_or(perr!("invalid component parameter {:?}", value))?));
+ return Ok(Self::Constant(Parameter::guess_from_json(value)?));
}
let paramvalue = value.get(1).ok_or(perr!("index 1 not in component parameter"))?;
let typename = value.get(0).ok_or(perr!("index 0 not in component parameter"))?.as_str().ok_or(perr!("compparam type not a string"))?;
if let Some(paramtype) = ParameterType::from_str(typename) {
- Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue).ok_or_else(||
- perr!("failed to parse parameter constant: {:?} {:?}", paramtype, paramvalue)
- )?))
+ Ok(Self::Constant(Parameter::from_typed_json(paramtype, paramvalue)?))
} else {
match typename {
"A" | "arg" => {
@@ -120,6 +118,7 @@ impl ComponentParameter {
}
}
+ #[allow(dead_code)]
pub fn get_type(&self, arguments: &[(String, ParameterType, Option<Parameter>)]) -> Result<ParameterType>{
Ok(match self {
Self::Constant(param) => param.paramtype(),
diff --git a/src/components/interactable.rs b/src/components/interactable.rs
index d97b742..63e89e3 100644
--- a/src/components/interactable.rs
+++ b/src/components/interactable.rs
@@ -8,7 +8,6 @@ use specs::{
};
use crate::{
exchange::Exchange,
- ItemId,
components::{Trigger, equipment::Stat},
RoomId
};
@@ -17,9 +16,6 @@ use crate::{
#[storage(HashMapStorage)]
pub enum Interactable {
Trigger(Trigger),
- Say(String),
- Reply(String),
- Exchange(String, HashMap<String, Exchange>),
Visit(RoomId),
Mine(Stat)
}
@@ -32,19 +28,6 @@ impl Interactable {
let arg = val.get(1)?;
Some(match typ.as_str()? {
"trigger" => Trigger(Trigger::from_str(arg.as_str()?)?),
- "say" => Say(arg.as_str()?.to_string()),
- "reply" => Reply(arg.as_str()?.to_string()),
- "exchange" => {
- let (prefix, change) = serde_json::value::from_value::<
- (String, HashMap<String, (Vec<ItemId>, Vec<ItemId>)>)
- >(arg.clone()).ok()?;
- Exchange(
- prefix,
- change.into_iter().map(
- |(id, (cost, offer))| (id, Exchange{cost, offer})
- ).collect::<HashMap<String, Exchange>>()
- )
- },
"visit" => Visit(RoomId::from_str(arg.as_str()?)),
"mine" => Mine(Stat::from_str(arg.as_str()?)?),
_ => None?
@@ -54,15 +37,6 @@ impl Interactable {
pub fn accepts_arg(&self, arg: &Option<String>) -> bool {
match self {
Trigger(_) => arg.is_none(),
- Say(_) => arg.is_none(),
- Reply(_) => arg.is_some(),
- Exchange(prefix, _exchanges) => {
- if let Some(txt) = arg {
- txt.starts_with(prefix)
- } else {
- true
- }
- },
Visit(_) => {
if let Some(txt) = arg {
txt.starts_with("visit ") || txt.starts_with("disallow ") || txt.starts_with("allow ") || txt.starts_with("whitelist")
@@ -74,3 +48,18 @@ impl Interactable {
}
}
}
+
+#[derive(Component, Debug, Clone, PartialEq)]
+#[storage(HashMapStorage)]
+pub struct Talkable {
+ pub text: String
+}
+
+
+#[derive(Component, Debug, Clone, PartialEq)]
+#[storage(HashMapStorage)]
+pub struct Exchanger {
+ pub prefix: String,
+ pub exchanges: HashMap<String, Exchange>
+}
+
diff --git a/src/components/mod.rs b/src/components/mod.rs
index ee5176e..68e666c 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -16,7 +16,7 @@ pub use messages::{
TriggerBox
};
pub use faction::Faction;
-pub use interactable::Interactable;
+pub use interactable::{Interactable, Talkable, Exchanger};
pub use equipment::Equipment;
pub use inventory::Inventory;
pub use serialise::Serialise;
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 2619d9c..638d156 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -21,6 +21,7 @@ use crate::{
fromtoparameter::FromToParameter,
Timestamp,
Template,
+ exchange::Exchange,
Pos,
Result,
aerr
@@ -50,12 +51,13 @@ macro_rules! components {
ComponentType::$comp => Ok(Self::$comp({
use crate::components::$comp;
$(
- let $paramname = <$paramtype>::from_parameter(
- parameters
+ let $paramname = {
+ let param = parameters
.remove(stringify!($paramname))
- .ok_or(aerr!("required parameter '{}'not found", stringify!($paramname)))?
- )
- .ok_or(aerr!("parameter {} is invalid type", stringify!($paramname)))?;
+ .ok_or(aerr!("required parameter '{}'not found", stringify!($paramname)))?;
+ <$paramtype>::from_parameter(param.clone())
+ .ok_or(aerr!("parameter {} is invalid type: {:?} is not of type {}", stringify!($paramname), param, stringify!($paramtype)))?
+ };
)*
$creation
@@ -233,6 +235,18 @@ components!(all:
}
};
Substitute (into: Template);
+ Talkable (text: String);
+ Exchanger (prefix: String, exchanges: Vec<(String, Vec<ItemId>, Vec<ItemId>)>) {
+ Exchanger {
+ prefix,
+ exchanges: exchanges
+ .into_iter()
+ .map(|(key, cost, offer)|
+ (key, Exchange{cost, offer})
+ )
+ .collect()
+ }
+ };
);
diff --git a/src/encyclopedia.rs b/src/encyclopedia.rs
index e100cad..70f0ac6 100644
--- a/src/encyclopedia.rs
+++ b/src/encyclopedia.rs
@@ -94,7 +94,7 @@ impl Encyclopedia {
let mut assemblage = assemblages.get(&enttype).ok_or(perr!("template name '{:?}' does not point to not an assemblage", enttype))?.clone();
for arg in assemblage.arguments.iter_mut() {
if let Some(x) = values.get(&arg.0) {
- let param = Parameter::from_typed_json(arg.1, x).ok_or(perr!("subtitution parameter has wrong type"))?;
+ let param = Parameter::from_typed_json(arg.1, x)?;
arg.2 = Some(param);
}
}
diff --git a/src/parameter.rs b/src/parameter.rs
index cee6342..e2d48dd 100644
--- a/src/parameter.rs
+++ b/src/parameter.rs
@@ -3,7 +3,9 @@ use serde_json::{Value, json};
use crate::{
Template,
components::interactable::Interactable,
- Pos
+ Pos,
+ PResult,
+ perr
};
@@ -17,10 +19,10 @@ macro_rules! parameters {
)*
}
impl Parameter {
- pub fn from_typed_json(typ: ParameterType, val: &Value) -> Option<Parameter>{
+ pub fn from_typed_json(typ: ParameterType, val: &Value) -> PResult<Parameter>{
match typ {
$(
- ParameterType::$name => Some(Self::$name({
+ ParameterType::$name => Ok(Self::$name({
let $v = val;
$fromjson
})),
@@ -63,20 +65,20 @@ macro_rules! parameters {
}
parameters!(
- String (String) string, v (v.as_str()?.to_string()) (json!(v));
- Int (i64) int, v (v.as_i64()?) (json!(v));
- Pos (Pos) pos, v (Pos::from_json(v)?) (json!(v));
- Float (f64) float, v (v.as_f64()?) (json!(v));
- Template (Template) template, v (Template::from_json(v).ok()?) (json!(["template", v.to_json()]));
- Interaction (Interactable) interaction, _v (Interactable::from_json(_v)?) (panic!("interactions can't be serialized"));
- Bool (bool) bool, v (v.as_bool()?) (json!(v));
+ String (String) string, v (v.as_str().ok_or(perr!("{:?} not a string", v))?.to_string()) (json!(v));
+ Int (i64) int, v (v.as_i64().ok_or(perr!("{:?} not an int", v))?) (json!(v));
+ Pos (Pos) pos, v (Pos::from_json(v).ok_or(perr!("{:?} not a pos", v))?) (json!(v));
+ Float (f64) float, v (v.as_f64().ok_or(perr!("{:?} not an float", v))?) (json!(v));
+ Template (Template) template, v (Template::from_json(v)?) (json!(["template", v.to_json()]));
+ Interaction (Interactable) interaction, _v (Interactable::from_json(_v).ok_or(perr!("{:?} not an interactable", _v))?) (panic!("interactions can't be serialized"));
+ Bool (bool) bool, v (v.as_bool().ok_or(perr!("{:?} not a bool", v))?) (json!(v));
List (Vec<Parameter>) list, v
({
v
- .as_array()?
+ .as_array().ok_or(perr!("{:?} not an array", v))?
.iter()
.map(|item| Parameter::guess_from_json(item))
- .collect::<Option<Vec<Parameter>>>()?
+ .collect::<PResult<Vec<Parameter>>>()?
})
(json!(["list", v.iter().map(Parameter::to_json).collect::<Vec<Value>>()]));
);
@@ -88,11 +90,11 @@ impl Parameter {
Self::String(string.to_string())
}
- pub fn guess_from_json(val: &Value) -> Option<Parameter> {
+ pub fn guess_from_json(val: &Value) -> PResult<Parameter> {
if let Some(arr) = val.as_array() {
if arr.len() == 2 && arr[0].is_string() {
let typestr = arr[0].as_str().unwrap();
- let typ = ParameterType::from_str(typestr)?;
+ let typ = ParameterType::from_str(typestr).ok_or(perr!("invalid parameter type {}", typestr))?;
return Self::from_typed_json(typ, &arr[1]);
}
}
@@ -108,7 +110,7 @@ impl Parameter {
} else if val.is_object(){
ParameterType::Template
} else {
- return None
+ return Err(perr!("can't guess the type of parameter {:?}", val));
};
Self::from_typed_json(typ, val)
}
diff --git a/src/resources/ground.rs b/src/resources/ground.rs
index 7411a15..7868be9 100644
--- a/src/resources/ground.rs
+++ b/src/resources/ground.rs
@@ -9,7 +9,8 @@ use specs::{
use crate::{
components::{Visible, Flags, Flag},
- Pos
+ Pos,
+ controls::Direction
};
#[derive(Default)]
@@ -43,6 +44,19 @@ impl Ground {
.collect()
}
+ pub fn components_near<'a, C: Component>(&self, pos: Pos, directions: &[Direction], component_type: &'a ReadStorage<C>) -> Vec<(Entity, &'a C)> {
+ let mut nearby_components: Vec<(Entity, &'a C)> = Vec::new();
+ for direction in directions {
+ let pos = pos + direction.to_position();
+ for ent in self.cells.get(&pos).unwrap_or(&HashSet::new()) {
+ if let Some(comp) = component_type.get(*ent) {
+ nearby_components.push((*ent, comp));
+ }
+ }
+ }
+ nearby_components
+ }
+
pub fn by_height(&self, pos: &Pos, visibles: &ReadStorage<Visible>, ignore: &Entity) -> Vec<Entity> {
let mut entities: Vec<Entity> = self.cells
.get(&pos).unwrap_or(&HashSet::new())
diff --git a/src/room.rs b/src/room.rs
index 0b8ee3b..9034e9e 100644
--- a/src/room.rs
+++ b/src/room.rs
@@ -70,7 +70,9 @@ use crate::{
Building,
Deduplicate,
SpawnTrigger,
- Replace
+ Replace,
+ Talk,
+ Exchange
}
};
@@ -86,12 +88,14 @@ pub fn default_dispatcher<'a, 'b>() -> Dispatcher<'a, 'b> {
.with(Take, "take", &["controlinput", "controlai"])
.with(Use, "use", &["controlinput", "controlai"])
.with(Interact, "interact", &["controlinput", "controlai"])
+ .with(Talk, "talk", &["controlinput", "controlai"])
+ .with(Exchange, "exchange", &["controlinput", "controlai"])
.with(SpawnTrigger, "spawntrigger", &["spawn", "deduplicate", "replace"])
.with(Move, "move", &["controlinput", "controlai"])
.with(Trapping, "trapping", &["move"])
.with(Fight, "fight", &["move"])
.with(Heal, "heal", &[])
- .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "spawntrigger"])
+ .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "talk", "exchange", "spawntrigger"])
.with(Die, "die", &["attacking"])
.with(DropLoot, "droploot", &["attacking"])
.with(Building, "building", &["attacking"])
@@ -125,7 +129,7 @@ impl <'a, 'b>Room<'a, 'b> {
world.insert(NewEntities::new(encyclopedia));
register_insert!(
world,
- (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute),
+ (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder, OnSpawn, Substitute, Talkable, Exchanger),
(Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions)
);
diff --git a/src/systems/exchange.rs b/src/systems/exchange.rs
new file mode 100644
index 0000000..9931bdd
--- /dev/null
+++ b/src/systems/exchange.rs
@@ -0,0 +1,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())});
+ }
+}
diff --git a/src/systems/interact.rs b/src/systems/interact.rs
index b377a7a..bce2afc 100644
--- a/src/systems/interact.rs
+++ b/src/systems/interact.rs
@@ -28,7 +28,7 @@ use crate::{
Minable
},
controls::{Control},
- resources::{Ground, NewEntities, Emigration},
+ resources::{Ground, Emigration},
hashmap,
playerstate::RoomPos,
PlayerId,
@@ -45,7 +45,6 @@ impl <'a> System<'a> for Interact {
WriteStorage<'a, ControlCooldown>,
ReadStorage<'a, Interactable>,
WriteStorage<'a, TriggerBox>,
- Write<'a, NewEntities>,
WriteStorage<'a, Ear>,
WriteStorage<'a, Inventory>,
ReadStorage<'a, Visible>,
@@ -55,21 +54,16 @@ impl <'a> System<'a> for Interact {
WriteStorage<'a, Minable>
);
- fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, new, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) {
+ fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) {
for (actor, controller, position) in (&entities, &controllers, &positions).join(){
let mut target = None;
let ear = ears.get_mut(actor);
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, arg.clone()));
- break 'targets;
- }
- }
+ for (ent, interactable) in ground.components_near(position.pos, directions, &interactables) {
+ if interactable.accepts_arg(arg){
+ target = Some((ent, interactable, arg.clone()));
+ break;
}
}
}
@@ -82,33 +76,6 @@ impl <'a> System<'a> for Interact {
Interactable::Trigger(trigger) => {
TriggerBox::add_message(&mut triggerbox, ent, *trigger);
}
- Interactable::Say(text) => {
- say(ear, text.clone(), name);
- }
- Interactable::Reply(text) => {
- say(ear, text.replace("{}", &arg.unwrap()), name);
- }
- Interactable::Exchange(prefix, exchanges) => {
- if let Some(txt) = arg {
- if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) {
- if let Some(exchange) = 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);
- }
- }
- } else if let Some(ear) = ear {
- ear.sounds.push(Notification::Options{
- description: "".to_string(),
- options: exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect()
- })
- }
- }
Interactable::Visit(dest) => {
if let Some(argument) = arg {
if let (Some(player), Some(whitelist)) = (players.get(actor), whitelists.get_mut(ent)){
diff --git a/src/systems/mod.rs b/src/systems/mod.rs
index 7323dee..76e39ce 100644
--- a/src/systems/mod.rs
+++ b/src/systems/mod.rs
@@ -24,6 +24,8 @@ mod building;
mod deduplicate;
mod spawntrigger;
mod replace;
+mod talk;
+mod exchange;
pub use self::{
controlinput::ControlInput,
@@ -50,5 +52,7 @@ pub use self::{
building::Building,
deduplicate::Deduplicate,
spawntrigger::SpawnTrigger,
- replace::Replace
+ replace::Replace,
+ talk::Talk,
+ exchange::Exchange
};
diff --git a/src/systems/talk.rs b/src/systems/talk.rs
new file mode 100644
index 0000000..50e491f
--- /dev/null
+++ b/src/systems/talk.rs
@@ -0,0 +1,50 @@
+
+
+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) => {
+ for (ent, Talkable{text}) in ground.components_near(position.pos, directions, &talkables) {
+ let name = visibles.get(ent).map(|v| v.name.clone());
+ ear.sounds.push(Notification::Sound{text: text.clone(), source: name});
+ break;
+ }
+ }
+ _ => {}
+ }
+ }
+ }
+}
+
diff --git a/src/template.rs b/src/template.rs
index 0ac03cd..7422465 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -89,11 +89,11 @@ impl Template {
let name = EntityType(val.get("type").ok_or(perr!("template doesn't have 'type'"))?.as_str().ok_or(perr!("template type not a string"))?.to_string());
let mut args = Vec::new();
for arg in val.get("args").unwrap_or(&json!([])).as_array().ok_or(perr!("template args not an array"))? {
- args.push(Parameter::guess_from_json(arg).ok_or(perr!("template arg {:?} not a parameter", arg))?);
+ args.push(Parameter::guess_from_json(arg)?);
}
let mut kwargs = HashMap::new();
for (key, arg) in val.get("kwargs").unwrap_or(&json!({})).as_object().ok_or(perr!("template kwargs not a json object"))? {
- kwargs.insert(key.to_string(), Parameter::guess_from_json(arg).ok_or(perr!("template kwarg {}: {:?} not a parameter", key, arg))?);
+ kwargs.insert(key.to_string(), Parameter::guess_from_json(arg)?);
}
let save =
if let Some(saveval) = val.get("save") {