diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/interactable.rs | 28 | ||||
| -rw-r--r-- | src/components/mod.rs | 2 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 16 | ||||
| -rw-r--r-- | src/room.rs | 6 | ||||
| -rw-r--r-- | src/systems/interact.rs | 28 | ||||
| -rw-r--r-- | src/systems/mod.rs | 2 |
6 files changed, 49 insertions, 33 deletions
diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 2e286cf..2f20966 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -8,7 +8,9 @@ use crate::{ exchange::Exchange, components::{Trigger, equipment::Stat}, RoomId, - parameter::Parameter + parameter::Parameter, + fromtoparameter::FromToParameter, + ItemId, }; #[derive(Component, Debug, Clone, PartialEq)] @@ -18,7 +20,8 @@ pub enum Interactable { Visit(RoomId), Mine(Stat), Say(String), - Reply(String) + Reply(String), + Exchange(String, HashMap<String, Exchange>), } use Interactable::*; @@ -32,7 +35,12 @@ impl Interactable { ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?), ("say", Parameter::String(s)) => Say(s.clone()), ("reply", Parameter::String(s)) => Reply(s.clone()), - _ => None? + ("exchange", p) => { + let (prefix, trades) = <(String, Vec<(String, Vec<ItemId>, Vec<ItemId>)>)>::from_parameter(p.clone())?; + let exchanges = trades.into_iter().map(|(k, cost, offer)| (k, Exchange{cost, offer})).collect(); + Exchange(prefix, exchanges) + } + _ => {return None} }) } @@ -49,15 +57,15 @@ impl Interactable { Mine(_) => 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 + } + }, } } } -#[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 f315060..38a14bd 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, Exchanger}; +pub use interactable::{Interactable}; pub use equipment::Equipment; pub use inventory::Inventory; pub use serialise::Serialise; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 96cedd3..08d95ee 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -20,7 +20,6 @@ use crate::{ fromtoparameter::FromToParameter, Timestamp, Template, - exchange::Exchange, Pos, Result, aerr @@ -184,7 +183,9 @@ components!(all: Clan (name: String); Home (home: Pos); Faction (faction: String) {Faction::from_str(faction.as_str()).ok_or(aerr!("invalid faction name"))?}; - Interactable (typ: String, arg: Parameter) {Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction"))?}; + Interactable (typ: String, arg: Parameter) { + Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction {:?} {:?}", typ, arg))? + }; Loot (loot: Vec<(Template, f64)>); Timer ( trigger: String, (panic!("can't turn trigger to string")), @@ -234,17 +235,6 @@ components!(all: } }; Substitute (into: Template); - 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/room.rs b/src/room.rs index 04e3044..56fe2e6 100644 --- a/src/room.rs +++ b/src/room.rs @@ -71,7 +71,6 @@ use crate::{ Deduplicate, SpawnTrigger, Replace, - Exchange } }; @@ -87,13 +86,12 @@ 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(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", "exchange", "spawntrigger"]) + .with(Attacking, "attacking", &["use", "trapping", "fight", "heal", "interact", "spawntrigger"]) .with(Die, "die", &["attacking"]) .with(DropLoot, "droploot", &["attacking"]) .with(Building, "building", &["attacking"]) @@ -127,7 +125,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, Exchanger), + (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), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time, RoomPermissions) ); diff --git a/src/systems/interact.rs b/src/systems/interact.rs index 35c30b6..1eb4589 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -28,7 +28,7 @@ use crate::{ Minable }, controls::{Control}, - resources::{Ground, Emigration}, + resources::{Ground, Emigration, NewEntities}, hashmap, playerstate::RoomPos, PlayerId, @@ -51,10 +51,11 @@ impl <'a> System<'a> for Interact { ReadStorage<'a, Player>, Write<'a, Emigration>, WriteStorage<'a, Whitelist>, - WriteStorage<'a, Minable> + WriteStorage<'a, Minable>, + Read<'a, NewEntities> ); - 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) { + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables, new): Self::SystemData) { for (actor, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; let ear = ears.get_mut(actor); @@ -136,6 +137,27 @@ impl <'a> System<'a> for Interact { } } } + 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() + }) + } + } } cooldowns.insert(actor, ControlCooldown{amount: cooldown}).unwrap(); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 963cfa6..7f93bb9 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -24,7 +24,6 @@ mod building; mod deduplicate; mod spawntrigger; mod replace; -mod exchange; pub use self::{ controlinput::ControlInput, @@ -52,5 +51,4 @@ pub use self::{ deduplicate::Deduplicate, spawntrigger::SpawnTrigger, replace::Replace, - exchange::Exchange }; |
