From e34f5f09e99e313e0c478bdd33cfd24c3f91a915 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 1 Nov 2020 11:15:46 -0600 Subject: [PATCH] Addressed comments. --- common/src/comp/health.rs | 4 +-- common/src/explosion.rs | 3 ++- server/src/cmd.rs | 17 +++++------- server/src/events/entity_manipulation.rs | 21 +++++++++------ server/src/state_ext.rs | 4 +-- server/src/sys/object.rs | 33 ++++++++---------------- voxygen/src/hud/mod.rs | 2 +- voxygen/src/hud/overhead.rs | 6 ++--- 8 files changed, 40 insertions(+), 50 deletions(-) diff --git a/common/src/comp/health.rs b/common/src/comp/health.rs index 7dd3ff3385..a9309be24b 100644 --- a/common/src/comp/health.rs +++ b/common/src/comp/health.rs @@ -120,10 +120,10 @@ impl Component for Health { } #[derive(Copy, Clone, Debug, Serialize, Deserialize)] -pub struct Dying { +pub struct Dead { pub cause: HealthSource, } -impl Component for Dying { +impl Component for Dead { type Storage = IdvStorage; } diff --git a/common/src/explosion.rs b/common/src/explosion.rs index 941d534c59..c0a96caab1 100644 --- a/common/src/explosion.rs +++ b/common/src/explosion.rs @@ -1,4 +1,4 @@ -use crate::combat::Damages; +use crate::{combat::Damages, effect::Effect}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -12,4 +12,5 @@ pub struct Explosion { pub enum RadiusEffect { Damages(Damages), TerrainDestruction(f32), + EntityEffect(Effect), } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 683915b11c..0c3c317fee 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -10,6 +10,7 @@ use chrono::{NaiveTime, Timelike}; use common::{ cmd::{ChatCommand, CHAT_COMMANDS, CHAT_SHORTCUTS}, comp::{self, ChatType, Item, LightEmitter, WaypointArea}, + effect::Effect, event::{EventBus, ServerEvent}, msg::{DisconnectReason, Notification, PlayerListUpdate, ServerGeneral}, npc::{self, get_npc_name}, @@ -18,7 +19,7 @@ use common::{ terrain::{Block, BlockKind, SpriteKind, TerrainChunkSize}, util::Dir, vol::RectVolSize, - Damage, DamageSource, Damages, Explosion, LoadoutBuilder, RadiusEffect, + Explosion, LoadoutBuilder, RadiusEffect, }; use rand::Rng; use specs::{Builder, Entity as EcsEntity, Join, WorldExt}; @@ -1157,16 +1158,10 @@ fn handle_explosion( pos: pos.0, explosion: Explosion { effects: vec![ - RadiusEffect::Damages(Damages::new( - Some(Damage { - source: DamageSource::Explosion, - value: 100.0 * power, - }), - Some(Damage { - source: DamageSource::Explosion, - value: 100.0 * power, - }), - )), + RadiusEffect::EntityEffect(Effect::Health(comp::HealthChange { + amount: (-100.0 * power) as i32, + cause: comp::HealthSource::Explosion { owner: None }, + })), RadiusEffect::TerrainDestruction(power), ], radius: 3.0 * power, diff --git a/server/src/events/entity_manipulation.rs b/server/src/events/entity_manipulation.rs index e38107577f..348d6d7a9c 100644 --- a/server/src/events/entity_manipulation.rs +++ b/server/src/events/entity_manipulation.rs @@ -516,15 +516,10 @@ pub fn handle_explosion( // Add an outcome // Uses radius as outcome power, makes negative if explosion has healing effect - #[allow(clippy::blocks_in_if_conditions)] let outcome_power = explosion.radius - * if explosion.effects.iter().any(|e| { - if let RadiusEffect::Damages(d) = e { - d.contains_damage(DamageSource::Healing) - } else { - false - } - }) { + * if explosion.effects.iter().any( + |e| matches!(e, RadiusEffect::Damages(d) if d.contains_damage(DamageSource::Healing)), + ) { -1.0 } else { 1.0 @@ -672,6 +667,16 @@ pub fn handle_explosion( .cast(); } }, + RadiusEffect::EntityEffect(effect) => { + for (entity, pos_entity) in + (&ecs.entities(), &ecs.read_storage::()).join() + { + let distance_squared = pos.distance_squared(pos_entity.0); + if distance_squared < explosion.radius.powi(2) { + server.state().apply_effect(entity, effect); + } + } + }, } } } diff --git a/server/src/state_ext.rs b/server/src/state_ext.rs index cd806f88f6..3ffa32252d 100644 --- a/server/src/state_ext.rs +++ b/server/src/state_ext.rs @@ -20,7 +20,7 @@ use vek::*; pub trait StateExt { /// Updates a component associated with the entity based on the `Effect` - fn apply_effect(&mut self, entity: EcsEntity, effect: Effect); + fn apply_effect(&self, entity: EcsEntity, effect: Effect); /// Build a non-player character fn create_npc( &mut self, @@ -71,7 +71,7 @@ pub trait StateExt { } impl StateExt for State { - fn apply_effect(&mut self, entity: EcsEntity, effect: Effect) { + fn apply_effect(&self, entity: EcsEntity, effect: Effect) { match effect { Effect::Health(change) => { self.ecs() diff --git a/server/src/sys/object.rs b/server/src/sys/object.rs index 110895e009..7b86eb73b5 100644 --- a/server/src/sys/object.rs +++ b/server/src/sys/object.rs @@ -1,9 +1,10 @@ use common::{ - comp::{HealthSource, Object, PhysicsState, Pos, Vel}, + comp::{HealthChange, HealthSource, Object, PhysicsState, Pos, Vel}, + effect::Effect, event::{EventBus, ServerEvent}, span, state::DeltaTime, - Damage, DamageSource, Damages, Explosion, RadiusEffect, + Explosion, RadiusEffect, }; use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage}; @@ -49,16 +50,10 @@ impl<'a> System<'a> for Sys { pos: pos.0, explosion: Explosion { effects: vec![ - RadiusEffect::Damages(Damages::new( - Some(Damage { - source: DamageSource::Explosion, - value: 500.0, - }), - Some(Damage { - source: DamageSource::Explosion, - value: 500.0, - }), - )), + RadiusEffect::EntityEffect(Effect::Health(HealthChange { + amount: -500, + cause: HealthSource::Explosion { owner: *owner }, + })), RadiusEffect::TerrainDestruction(4.0), ], radius: 12.0, @@ -79,16 +74,10 @@ impl<'a> System<'a> for Sys { pos: pos.0, explosion: Explosion { effects: vec![ - RadiusEffect::Damages(Damages::new( - Some(Damage { - source: DamageSource::Explosion, - value: 50.0, - }), - Some(Damage { - source: DamageSource::Explosion, - value: 50.0, - }), - )), + RadiusEffect::EntityEffect(Effect::Health(HealthChange { + amount: -50, + cause: HealthSource::Explosion { owner: *owner }, + })), RadiusEffect::TerrainDestruction(4.0), ], radius: 12.0, diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 66a67c4bf6..38f26f98f0 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -1195,7 +1195,7 @@ impl Hud { let display_overhead_info = (info.target_entity.map_or(false, |e| e == entity) || info.selected_entity.map_or(false, |s| s.0 == entity) - || overhead::show_healthbar(health) + || overhead::should_show_healthbar(health) || in_group) && dist_sqr < (if in_group { diff --git a/voxygen/src/hud/overhead.rs b/voxygen/src/hud/overhead.rs index 04ff0e2ab8..6d2630b3ad 100644 --- a/voxygen/src/hud/overhead.rs +++ b/voxygen/src/hud/overhead.rs @@ -64,7 +64,7 @@ pub struct Info<'a> { } /// Determines whether to show the healthbar -pub fn show_healthbar(health: &Health) -> bool { health.current() != health.maximum() } +pub fn should_show_healthbar(health: &Health) -> bool { health.current() != health.maximum() } /// ui widget containing everything that goes over a character's head /// (Speech bubble, Name, Level, HP/energy bars, etc.) @@ -142,7 +142,7 @@ impl<'a> Ingameable for Overhead<'a> { } else { 0 } - + if show_healthbar(info.health) { + + if should_show_healthbar(info.health) { 5 + if info.energy.is_some() { 1 } else { 0 } } else { 0 @@ -303,7 +303,7 @@ impl<'a> Widget for Overhead<'a> { .parent(id) .set(state.ids.name, ui); - if show_healthbar(health) { + if should_show_healthbar(health) { // Show HP Bar let hp_ani = (self.pulse * 4.0/* speed factor */).cos() * 0.5 + 1.0; //Animation timer let crit_hp_color: Color = Color::Rgba(0.79, 0.19, 0.17, hp_ani);