diff options
| -rw-r--r-- | src/attack.rs | 17 | ||||
| -rw-r--r-- | src/components/messages.rs | 52 | ||||
| -rw-r--r-- | src/components/mod.rs | 30 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 23 | ||||
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/playerstate.rs | 5 | ||||
| -rw-r--r-- | src/systems/attacking.rs | 12 | ||||
| -rw-r--r-- | src/systems/fight.rs | 6 | ||||
| -rw-r--r-- | src/systems/trapping.rs | 6 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 8 |
10 files changed, 97 insertions, 63 deletions
diff --git a/src/attack.rs b/src/attack.rs deleted file mode 100644 index a0b8b99..0000000 --- a/src/attack.rs +++ /dev/null @@ -1,17 +0,0 @@ - -use specs::Entity; - -#[derive(Debug, Clone, Default)] -pub struct Attack { - pub damage: i64, - pub attacker: Option<Entity> -} - -impl Attack { - pub fn new(damage: i64) -> Self { - Self { - damage, - attacker: None - } - } -} diff --git a/src/components/messages.rs b/src/components/messages.rs new file mode 100644 index 0000000..a2ecd2f --- /dev/null +++ b/src/components/messages.rs @@ -0,0 +1,52 @@ + +use std::any::Any; +use specs::{ + Component, + DenseVecStorage, + Entity, + WriteStorage +}; + + + +pub trait Message: Send + Sync + Any {} + +#[derive(Debug, Clone, Default)] +pub struct Inbox<M: Message> { + pub messages: Vec<M> +} + +impl <M: Message> Component for Inbox<M> { + type Storage = DenseVecStorage<Self>; +} + +impl <M: Message> Inbox<M> { + + pub fn add_message(messages: &mut WriteStorage<Self>, ent: Entity, message: M){ + messages + .entry(ent) + .unwrap() + .or_insert_with(|| Self{messages: Vec::new()}) + .messages + .push(message); + } +} + +#[derive(Debug, Clone)] +pub struct AttackMessage { + pub damage: i64, + pub attacker: Option<Entity> +} + +impl AttackMessage { + pub fn new(damage: i64) -> Self { + Self { + damage, + attacker: None + } + } +} + +impl Message for AttackMessage {} + +pub type AttackInbox = Inbox<AttackMessage>; diff --git a/src/components/mod.rs b/src/components/mod.rs index 6fae490..3925d85 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,7 +1,12 @@ pub mod item; +pub mod messages; pub use item::Item; +pub use messages::{ + AttackMessage, + AttackInbox +}; use specs::{ DenseVecStorage, @@ -9,9 +14,7 @@ use specs::{ HashMapStorage, FlaggedStorage, NullStorage, - Component, - Entity, - WriteStorage + Component }; use crate::{ @@ -22,7 +25,6 @@ use crate::{ controls::Control, Template, playerstate::RoomPos, - attack::Attack, Timestamp }; @@ -126,22 +128,6 @@ pub struct RoomExit { pub dest_pos: RoomPos } -#[derive(Component, Debug, Clone, Default)] -pub struct Attacked { - pub attacks: Vec<Attack> -} - -impl Attacked { - pub fn add_attack(attacked: &mut WriteStorage<Attacked> , ent: Entity, attack: Attack) { - attacked - .entry(ent) - .unwrap() - .or_insert_with(Attacked::default) - .attacks - .push(attack); - } -} - #[derive(Default, Component, Debug, Clone)] #[storage(NullStorage)] pub struct Entered; @@ -153,13 +139,13 @@ pub struct Dying; #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Trap { - pub attack: Attack + pub attack: AttackMessage } #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Fighter { - pub attack: Attack, + pub attack: AttackMessage, pub cooldown: i64 } diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 2fe114a..08164e4 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -7,8 +7,23 @@ use crate::{ RoomId, Sprite, playerstate::RoomPos, - attack::Attack, - components::{Visible, Movable, Blocking, Player, Floor, Item, Inventory, Health, Serialise, RoomExit, Trap, Fighter, Healing, Volatile}, + components::{ + Visible, + Movable, + Blocking, + Player, + Floor, + Item, + Inventory, + Health, + Serialise, + RoomExit, + Trap, + Fighter, + Healing, + Volatile, + AttackMessage + }, parameter::{Parameter, ParameterType} }; @@ -115,8 +130,8 @@ components!( } } }; - Trap (damage: Int) {Trap{attack: Attack::new(damage)}}; - Fighter (damage: Int, cooldown: Int) {Fighter{attack: Attack::new(damage), cooldown}}; + Trap (damage: Int) {Trap{attack: AttackMessage::new(damage)}}; + Fighter (damage: Int, cooldown: Int) {Fighter{attack: AttackMessage::new(damage), cooldown}}; Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}}; Volatile (delay: Int) {Volatile{delay, end_time: None}}; ); diff --git a/src/main.rs b/src/main.rs index ed7de08..480fc64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,6 @@ mod persistence; mod worldloader; mod world; mod sprite; -mod attack; mod timestamp; pub use self::{ diff --git a/src/playerstate.rs b/src/playerstate.rs index 01e85fe..79c7b31 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -6,8 +6,7 @@ use crate::{ componentwrapper::{ComponentWrapper, PreEntity}, PlayerId, RoomId, - components::{Visible, Player, Inventory, Health, Fighter, Healing, Movable}, - attack::Attack, + components::{Visible, Player, Inventory, Health, Fighter, Healing, Movable, AttackMessage}, Result, aerr, Sprite, @@ -116,7 +115,7 @@ impl PlayerState { capacity: self.inventory_capacity }), ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health}), - ComponentWrapper::Fighter(Fighter{attack: Attack::new(5), cooldown: 8}), + ComponentWrapper::Fighter(Fighter{attack: AttackMessage::new(5), cooldown: 8}), ComponentWrapper::Healing(Healing{delay: 50, health: 1, next_heal: None}), ComponentWrapper::Movable(Movable{cooldown: 2}) ] diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs index 4b47f11..d28e301 100644 --- a/src/systems/attacking.rs +++ b/src/systems/attacking.rs @@ -9,7 +9,7 @@ use specs::{ }; use crate::{ - components::{Health, Attacked, Dying, Removed, Position}, + components::{Health, AttackInbox, Dying, Removed, Position}, resources::NewEntities, Template, util @@ -20,17 +20,17 @@ pub struct Attacking; impl <'a> System<'a> for Attacking { type SystemData = ( Entities<'a>, - WriteStorage<'a, Attacked>, + WriteStorage<'a, AttackInbox>, WriteStorage<'a, Health>, WriteStorage<'a, Dying>, WriteStorage<'a, Removed>, ReadStorage<'a, Position>, Write<'a, NewEntities> ); - fn run(&mut self, (entities, mut victims, mut healths, mut deads, mut removals, positions, mut new): Self::SystemData) { - for (ent, health, attacked) in (&entities, &mut healths, &mut victims).join() { + fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, mut removals, positions, mut new): Self::SystemData) { + for (ent, health, attacked) in (&entities, &mut healths, &mut attackeds).join() { let mut wounded = false; - for attack in attacked.attacks.drain(..) { + for attack in attacked.messages.drain(..) { health.health -= attack.damage; if attack.damage > 0 { wounded = true; @@ -47,7 +47,7 @@ impl <'a> System<'a> for Attacking { } } } - victims.clear(); + attackeds.clear(); } } diff --git a/src/systems/fight.rs b/src/systems/fight.rs index 547f5fa..42a7543 100644 --- a/src/systems/fight.rs +++ b/src/systems/fight.rs @@ -12,7 +12,7 @@ use specs::{ use crate::components::{ Controller, Position, - Attacked, + AttackInbox, Fighter, Health, ControlCooldown @@ -30,7 +30,7 @@ impl <'a> System<'a> for Fight { ReadStorage<'a, Controller>, WriteStorage<'a, Position>, Read<'a, Ground>, - WriteStorage<'a, Attacked>, + WriteStorage<'a, AttackInbox>, ReadStorage<'a, Fighter>, ReadStorage<'a, Health>, WriteStorage<'a, ControlCooldown> @@ -43,7 +43,7 @@ impl <'a> System<'a> for Fight { for direction in directions { for ent in ground.cells.get(&(position.pos + direction.to_position())).unwrap_or(&HashSet::new()) { if healths.contains(*ent) && *ent != entity { - Attacked::add_attack(&mut attacked, *ent, fighter.attack.clone()); + AttackInbox::add_message(&mut attacked, *ent, fighter.attack.clone()); cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap(); break; } diff --git a/src/systems/trapping.rs b/src/systems/trapping.rs index d6902a8..55b8bde 100644 --- a/src/systems/trapping.rs +++ b/src/systems/trapping.rs @@ -9,7 +9,7 @@ use specs::{ }; use crate::{ - components::{Health, Attacked, Moved, Entered, Trap, Position}, + components::{Health, AttackInbox, Moved, Entered, Trap, Position}, resources::Ground }; @@ -18,7 +18,7 @@ pub struct Trapping; impl <'a> System<'a> for Trapping { type SystemData = ( Entities<'a>, - WriteStorage<'a, Attacked>, + WriteStorage<'a, AttackInbox>, ReadStorage<'a, Health>, ReadStorage<'a, Moved>, ReadStorage<'a, Entered>, @@ -31,7 +31,7 @@ impl <'a> System<'a> for Trapping { for (entity, _entered, trap, position) in (&entities, &entereds, &traps, &positions).join() { for ent in ground.cells.get(&position.pos).unwrap(){ if ent != &entity && moves.contains(*ent) && healths.contains(*ent) { - Attacked::add_attack(&mut victims, *ent, trap.attack.clone()); + AttackInbox::add_message(&mut victims, *ent, trap.attack.clone()); } } } diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index 183bd79..1a50865 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -14,12 +14,12 @@ use crate::{ Controller, Position, Inventory, - Attacked + AttackInbox, + AttackMessage }, resources::{NewEntities}, components::item::ItemAction::{None, Build, Eat}, controls::Control, - attack::Attack }; @@ -31,7 +31,7 @@ impl <'a> System<'a> for Use { WriteStorage<'a, Position>, WriteStorage<'a, Inventory>, Write<'a, NewEntities>, - WriteStorage<'a, Attacked> + WriteStorage<'a, AttackInbox> ); fn run(&mut self, (entities, controllers, positions, mut inventories, mut new, mut attacked): Self::SystemData) { @@ -45,7 +45,7 @@ impl <'a> System<'a> for Use { inventory.items.remove(*rank); } Eat(health_diff) => { - Attacked::add_attack(&mut attacked, ent, Attack::new(-*health_diff)); + AttackInbox::add_message(&mut attacked, ent, AttackMessage::new(-*health_diff)); inventory.items.remove(*rank); } None => {} |
