diff options
| author | troido <troido@protonmail.com> | 2020-03-03 18:06:02 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-03 18:06:02 +0100 |
| commit | 986c82723cf9a4adada02287309999f4ebbf94e3 (patch) | |
| tree | acc26dfd3cb604c959d16f4291db2f6f21d242f9 /src | |
| parent | 9954b5cbaab27aaffcafa8723dcd5d1c99fa811f (diff) | |
randomise attack damage
Diffstat (limited to 'src')
| -rw-r--r-- | src/components/messages.rs | 25 | ||||
| -rw-r--r-- | src/components/mod.rs | 7 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 16 | ||||
| -rw-r--r-- | src/playerstate.rs | 4 | ||||
| -rw-r--r-- | src/systems/attacking.rs | 20 | ||||
| -rw-r--r-- | src/systems/fight.rs | 5 | ||||
| -rw-r--r-- | src/systems/trapping.rs | 4 | ||||
| -rw-r--r-- | src/systems/useitem.rs | 5 |
8 files changed, 47 insertions, 39 deletions
diff --git a/src/components/messages.rs b/src/components/messages.rs index a2ecd2f..ae615f1 100644 --- a/src/components/messages.rs +++ b/src/components/messages.rs @@ -32,21 +32,28 @@ impl <M: Message> Inbox<M> { } } -#[derive(Debug, Clone)] -pub struct AttackMessage { - pub damage: i64, - pub attacker: Option<Entity> + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum AttackType { + Attack(i64), + Heal(i64) } -impl AttackMessage { - pub fn new(damage: i64) -> Self { - Self { - damage, - attacker: None +impl AttackType { + pub fn is_hostile(&self) -> bool { + match self { + Self::Attack(_) => true, + Self::Heal(_) => false } } } +#[derive(Debug, Clone)] +pub struct AttackMessage { + pub attacker: Option<Entity>, + pub typ: AttackType +} + impl Message for AttackMessage {} pub type AttackInbox = Inbox<AttackMessage>; diff --git a/src/components/mod.rs b/src/components/mod.rs index 96261c7..202caf2 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -5,7 +5,8 @@ pub mod messages; pub use item::Item; pub use messages::{ AttackMessage, - AttackInbox + AttackInbox, + AttackType }; use specs::{ @@ -140,13 +141,13 @@ pub struct Dead; #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Trap { - pub attack: AttackMessage + pub attack: AttackType } #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Fighter { - pub attack: AttackMessage, + pub attack: AttackType, pub cooldown: i64, pub range: i64 } diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 919a9c2..c617d77 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -9,7 +9,7 @@ use crate::{ Sprite, playerstate::RoomPos, components::{ - AttackMessage, + AttackType, Clan }, parameter::{Parameter, ParameterType}, @@ -20,16 +20,13 @@ use crate::{ macro_rules! components { ($($comp: ident ($($paramname: ident : $paramtype: ident),*) $creation: expr);*;) => { - #[derive(Clone)] pub enum ComponentWrapper{ $( $comp(crate::components::$comp), )* } - impl ComponentWrapper { - pub fn build<A: Builder>(&self, builder: A ) -> A { match self.clone() { $( @@ -37,12 +34,9 @@ macro_rules! components { )* } } - pub fn load_component(comptype: ComponentType, mut parameters: HashMap<&str, Parameter>) -> Option<Self> { - match comptype { $( - ComponentType::$comp => Some(Self::$comp({ use crate::components::$comp; $( @@ -57,16 +51,13 @@ macro_rules! components { } } } - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum ComponentType { $( $comp, )* } - impl ComponentType { - pub fn from_str(typename: &str) -> Option<ComponentType>{ match typename { $( @@ -75,7 +66,6 @@ macro_rules! components { _ => None } } - pub fn parameters(&self) -> HashMap<&str, ParameterType> { match self { $( @@ -120,8 +110,8 @@ components!( } } }; - Trap (damage: Int) {Trap{attack: AttackMessage::new(damage)}}; - Fighter (damage: Int, cooldown: Int) {Fighter{attack: AttackMessage::new(damage), cooldown, range: 1}}; + Trap (damage: Int) {Trap{attack: AttackType::Attack(damage)}}; + Fighter (damage: Int, cooldown: Int) {Fighter{attack: AttackType::Attack(damage), cooldown, range: 1}}; Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}}; Volatile (delay: Int) {Volatile{delay, end_time: None}}; Autofight () {Autofight::default()}; diff --git a/src/playerstate.rs b/src/playerstate.rs index dc4594b..4ccd6af 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -14,7 +14,7 @@ use crate::{ Fighter, Healing, Movable, - AttackMessage, + AttackType, Autofight }, Result, @@ -131,7 +131,7 @@ impl PlayerState { capacity: self.inventory_capacity }), ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health}), - ComponentWrapper::Fighter(Fighter{attack: AttackMessage::new(5), cooldown: 8, range: 1}), + ComponentWrapper::Fighter(Fighter{attack: AttackType::Attack(5), cooldown: 8, range: 1}), ComponentWrapper::Healing(Healing{delay: 50, health: 1, next_heal: None}), ComponentWrapper::Movable(Movable{cooldown: 2}), ComponentWrapper::Autofight(Autofight::default()) diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs index 9d10fee..4318695 100644 --- a/src/systems/attacking.rs +++ b/src/systems/attacking.rs @@ -1,4 +1,6 @@ + +use rand::Rng; use specs::{ ReadStorage, WriteStorage, @@ -9,7 +11,7 @@ use specs::{ }; use crate::{ - components::{Health, AttackInbox, Dead, Position, Autofight}, + components::{Health, AttackInbox, AttackType, Dead, Position, Autofight}, resources::NewEntities, Template, util @@ -31,7 +33,7 @@ impl <'a> System<'a> for Attacking { for (entity, attacked, autofighter) in (&entities, &attackeds, &mut autofighters).join() { for attack in &attacked.messages { - if attack.damage > 0 { + if attack.typ.is_hostile() { if let Some(attacker) = attack.attacker { if healths.contains(attacker) && attacker != entity { autofighter.target = Some(attacker); @@ -43,9 +45,17 @@ impl <'a> System<'a> for Attacking { for (ent, health, attacked) in (&entities, &mut healths, &mut attackeds).join() { let mut wounded = false; for attack in attacked.messages.drain(..) { - health.health -= attack.damage; - if attack.damage > 0 { - wounded = true; + match attack.typ { + AttackType::Attack(strength) => { + let damage = rand::thread_rng().gen_range(0, strength+1); + health.health -= damage; + if damage > 0 { + wounded = true; + } + } + AttackType::Heal(healthdiff) => { + health.health += healthdiff; + } } } health.health = util::clamp(health.health, 0, health.maxhealth); diff --git a/src/systems/fight.rs b/src/systems/fight.rs index 88305e2..00aeb3e 100644 --- a/src/systems/fight.rs +++ b/src/systems/fight.rs @@ -13,6 +13,7 @@ use crate::components::{ Controller, Position, AttackInbox, + AttackMessage, Fighter, Health, ControlCooldown, @@ -63,9 +64,7 @@ impl <'a> System<'a> for Fight { _ => {} } if let Some(ent) = target { - let mut attack = fighter.attack.clone(); - attack.attacker = Some(entity); - AttackInbox::add_message(&mut attacked, ent, attack); + AttackInbox::add_message(&mut attacked, ent, AttackMessage{typ: fighter.attack.clone(), attacker: Some(entity)}); cooldowns.insert(entity, ControlCooldown{amount: fighter.cooldown}).unwrap(); if let Some(autofighter) = autofighters.get_mut(entity){ autofighter.target = Some(ent); diff --git a/src/systems/trapping.rs b/src/systems/trapping.rs index 55b8bde..35c66d0 100644 --- a/src/systems/trapping.rs +++ b/src/systems/trapping.rs @@ -9,7 +9,7 @@ use specs::{ }; use crate::{ - components::{Health, AttackInbox, Moved, Entered, Trap, Position}, + components::{Health, AttackInbox, AttackMessage, Moved, Entered, Trap, Position}, resources::Ground }; @@ -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) { - AttackInbox::add_message(&mut victims, *ent, trap.attack.clone()); + AttackInbox::add_message(&mut victims, *ent, AttackMessage{typ: trap.attack.clone(), attacker: Some(entity)}); } } } diff --git a/src/systems/useitem.rs b/src/systems/useitem.rs index 1a50865..ab147b7 100644 --- a/src/systems/useitem.rs +++ b/src/systems/useitem.rs @@ -15,7 +15,8 @@ use crate::{ Position, Inventory, AttackInbox, - AttackMessage + AttackMessage, + AttackType }, resources::{NewEntities}, components::item::ItemAction::{None, Build, Eat}, @@ -45,7 +46,7 @@ impl <'a> System<'a> for Use { inventory.items.remove(*rank); } Eat(health_diff) => { - AttackInbox::add_message(&mut attacked, ent, AttackMessage::new(-*health_diff)); + AttackInbox::add_message(&mut attacked, ent, AttackMessage{typ: AttackType::Heal(*health_diff), attacker: Option::None}); inventory.items.remove(*rank); } None => {} |
