diff options
| author | troido <troido@protonmail.com> | 2020-04-04 14:01:17 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-04 14:01:17 +0200 |
| commit | 2cc5b468cfd4c28bf1ad17ef1b3600c3d42f8b83 (patch) | |
| tree | 835e67143e56fafef795e320ce8fc5d50cb9cc6c /src | |
| parent | 0b17829846adf9482b460e4cc616382ede1df6dd (diff) | |
listen to sounds in the world
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/ear.rs | 24 | ||||
| -rw-r--r-- | src/components/interactable.rs | 4 | ||||
| -rw-r--r-- | src/components/mod.rs | 5 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/playerstate.rs | 6 | ||||
| -rw-r--r-- | src/room.rs | 2 | ||||
| -rw-r--r-- | src/systems/interact.rs | 14 | ||||
| -rw-r--r-- | src/systems/view.rs | 13 | ||||
| -rw-r--r-- | src/worldmessages.rs | 18 |
10 files changed, 70 insertions, 18 deletions
diff --git a/src/components/ear.rs b/src/components/ear.rs new file mode 100644 index 0000000..d032122 --- /dev/null +++ b/src/components/ear.rs @@ -0,0 +1,24 @@ + +use specs::{ + HashMapStorage, + Component, +}; + + +#[derive(Debug, Clone)] +pub struct Sound { + pub source: Option<String>, + pub text: String +} + +impl Sound { + pub fn as_message(self) -> (Option<String>, String) { + return (None, format!("{}: {}", self.source.unwrap_or("".to_string()), self.text)); + } +} + +#[derive(Component, Debug, Clone, Default)] +#[storage(HashMapStorage)] +pub struct Ear{ + pub sounds: Vec<Sound> +} diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 2dca6ea..3c8f918 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -12,7 +12,8 @@ use crate::{ #[storage(HashMapStorage)] pub enum Interactable { Harvest, - Change(Template) + Change(Template), + Say(String) } impl Interactable { @@ -22,6 +23,7 @@ impl Interactable { match typ.as_str()? { "harvest" => Some(Interactable::Harvest), "change" => Some(Interactable::Change(Template::from_json(arg).ok()?)), + "say" => Some(Interactable::Say(arg.as_str()?.to_string())), _ => None } } diff --git a/src/components/mod.rs b/src/components/mod.rs index efdf8f7..b7e3875 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -7,6 +7,7 @@ pub mod equipment; pub mod inventory; pub mod serialise; pub mod flags; +pub mod ear; pub use item::Item; pub use messages::{ @@ -23,6 +24,10 @@ pub use flags::{ Flag, Flags }; +pub use ear::{ + Sound, + Ear +}; use specs::{ DenseVecStorage, diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 88a29f3..360a546 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -235,6 +235,7 @@ components!( .collect::<Option<HashSet<Flag>>>().ok_or(aerr!("invalid flag name"))? ) }; + Ear () {Ear::default()}; ); diff --git a/src/main.rs b/src/main.rs index 2e6042f..2f42ac3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,6 +141,7 @@ fn main() -> Result<()>{ if message.is_empty(){ continue; } +// println!("m {}", message.to_json()); let _ = gameserver.send(&player, message.to_json()); } diff --git a/src/playerstate.rs b/src/playerstate.rs index 6d953fb..f146cd2 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -18,7 +18,8 @@ use crate::{ Autofight, Faction, Equipment, - equipment::Slot + equipment::Slot, + Ear }, Result, aerr, @@ -151,7 +152,8 @@ impl PlayerState { ComponentWrapper::Movable(Movable{cooldown: 2}), ComponentWrapper::Autofight(Autofight::default()), ComponentWrapper::Faction(Faction::Good), - ComponentWrapper::Equipment(Equipment{slots: vec!(Slot::Hand, Slot::Body)}) + ComponentWrapper::Equipment(Equipment{slots: vec!(Slot::Hand, Slot::Body)}), + ComponentWrapper::Ear(Ear::default()) ] } } diff --git a/src/room.rs b/src/room.rs index 5b125c4..fa46d47 100644 --- a/src/room.rs +++ b/src/room.rs @@ -122,7 +122,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, /*Blocking, Floor,*/ New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, Dead, Trap, Fighter, Healing, Volatile, ControlCooldown, Autofight, MonsterAI, Home, Mortal, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Grow, Equipment, CreationTime, Flags), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, Dead, Trap, Fighter, Healing, Volatile, ControlCooldown, Autofight, MonsterAI, Home, Mortal, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Grow, Equipment, CreationTime, Flags, Ear), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time) ); diff --git a/src/systems/interact.rs b/src/systems/interact.rs index e3b8358..b932fd5 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -16,7 +16,9 @@ use crate::components::{ ControlCooldown, Interactable, Dead, - Removed + Removed, + Sound, + Ear }; use crate::controls::{Control}; @@ -35,10 +37,11 @@ impl <'a> System<'a> for Interact { ReadStorage<'a, Interactable>, WriteStorage<'a, Dead>, WriteStorage<'a, Removed>, - Write<'a, NewEntities> + Write<'a, NewEntities>, + WriteStorage<'a, Ear> ); - fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads, mut removeds, mut new): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads, mut removeds, mut new, mut ears): Self::SystemData) { for (entity, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; match &controller.control { @@ -64,6 +67,11 @@ impl <'a> System<'a> for Interact { new.create(pos, into).unwrap(); removeds.insert(ent, Removed).unwrap(); } + Interactable::Say(text) => { + if let Some(ear) = ears.get_mut(entity) { + ear.sounds.push(Sound{source: None, text: text.clone()}); + } + } } cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap(); } diff --git a/src/systems/view.rs b/src/systems/view.rs index 0ed1cf7..49338ac 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -3,6 +3,7 @@ use std::collections::{HashSet}; use specs::{ ReadStorage, + WriteStorage, Read, Write, System, @@ -13,7 +14,7 @@ use specs::{ use crate::{ Pos, Sprite, - components::{Visible, Player, Position, Inventory, New, Moved, Removed, Health}, + components::{Visible, Player, Position, Inventory, New, Moved, Removed, Health, Ear}, resources::{Size, Output, Ground}, worldmessages::{WorldMessage, FieldMessage} }; @@ -34,9 +35,10 @@ impl <'a> System<'a> for View { ReadStorage<'a, New>, ReadStorage<'a, Moved>, ReadStorage<'a, Removed>, - Read<'a, Ground> + Read<'a, Ground>, + WriteStorage<'a, Ear> ); - fn run(&mut self, (entities, positions, inventories, healths, visible, size, players, mut output, new, moved, removed, ground): Self::SystemData) { + fn run(&mut self, (entities, positions, inventories, healths, visible, size, players, mut output, new, moved, removed, ground, mut ears): Self::SystemData) { let mut changed = HashSet::new(); for (pos, _new) in (&positions, &new).join() { @@ -78,6 +80,11 @@ impl <'a> System<'a> for View { if let Some(health) = healths.get(ent){ updates.health = Some((health.health, health.maxhealth)); } + if let Some(ear) = ears.get_mut(ent){ + if !ear.sounds.is_empty(){ + updates.sounds = Some(ear.sounds.drain(..).map(|s| s.as_message()).collect()); + } + } updates.ground = Some( ground .by_height(&pos.pos, &visible, &ent) diff --git a/src/worldmessages.rs b/src/worldmessages.rs index 75d8b33..0ca72fa 100644 --- a/src/worldmessages.rs +++ b/src/worldmessages.rs @@ -9,7 +9,7 @@ use crate::{ }; macro_rules! worldmessages { - ($($name: ident, $typ: ident, $strname: expr);*;) => { + ($($name: ident, $typ: ident, $strname: expr, $filter: expr);*;) => { #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct WorldMessage { @@ -22,7 +22,7 @@ macro_rules! worldmessages { pub fn remove_old(&mut self, previous: &WorldMessage){ $( - if self.$name == previous.$name { + if $filter && self.$name == previous.$name { self.$name = None; } )* @@ -54,12 +54,13 @@ macro_rules! worldmessages { } worldmessages!( - field, FieldMessage, "field"; - pos, Pos, "playerpos"; - change, ChangeMessage, "changecells"; - inventory, InventoryMessage, "inventory"; - health, HealthMessage, "health"; - ground, GroundMessage, "ground"; + field, FieldMessage, "field", true; + pos, Pos, "playerpos", true; + change, ChangeMessage, "changecells", true; + inventory, InventoryMessage, "inventory", true; + health, HealthMessage, "health", true; + ground, GroundMessage, "ground", true; + sounds, SoundMessage, "messages", false; ); @@ -67,6 +68,7 @@ pub type ChangeMessage = Vec<(Pos, Vec<Sprite>)>; pub type HealthMessage = (i64, i64); pub type InventoryMessage = Vec<String>; pub type GroundMessage = Vec<String>; +pub type SoundMessage = Vec<(Option<String>, String)>; #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)] pub struct FieldMessage { |
