From fc97c27b65fef26fa15f860af1aaab5847e1a0e9 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sat, 28 Sep 2019 21:35:28 +0200 Subject: [PATCH] feat(bow): sticky arrows --- common/src/comp/projectile.rs | 1 + common/src/event.rs | 6 +++++- common/src/sys/controller.rs | 17 +++++++++++++---- common/src/sys/projectile.rs | 19 ++++++++++--------- server/src/lib.rs | 14 ++++++-------- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/common/src/comp/projectile.rs b/common/src/comp/projectile.rs index 99d49c1550..2c39007d10 100644 --- a/common/src/comp/projectile.rs +++ b/common/src/comp/projectile.rs @@ -5,6 +5,7 @@ use specs_idvs::IDVStorage; pub enum Effect { Damage(u32), Vanish, + Stick, } #[derive(Clone, Debug, Serialize, Deserialize)] diff --git a/common/src/event.rs b/common/src/event.rs index a404426ee8..623a546057 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -36,7 +36,11 @@ pub enum ServerEvent { cause: comp::HealthSource, }, Respawn(EcsEntity), - Shoot(EcsEntity, Vec3), + Shoot { + entity: EcsEntity, + dir: Vec3, + projectile: comp::Projectile, + }, Mount(EcsEntity, EcsEntity), Unmount(EcsEntity), } diff --git a/common/src/sys/controller.rs b/common/src/sys/controller.rs index f4db622d61..b863558132 100644 --- a/common/src/sys/controller.rs +++ b/common/src/sys/controller.rs @@ -4,8 +4,8 @@ use super::{ }; use crate::{ comp::{ - item, ActionState::*, Body, CharacterState, ControlEvent, Controller, Item, - MovementState::*, PhysicsState, Stats, Vel, + item, projectile, ActionState::*, Body, CharacterState, ControlEvent, Controller, Item, + MovementState::*, PhysicsState, Projectile, Stats, Vel, }, event::{EventBus, LocalEvent, ServerEvent}, }; @@ -140,8 +140,17 @@ impl<'a> System<'a> for Sys { if time_left == Duration::default() { // Immediately end the wield character.action = Idle; - server_emitter - .emit(ServerEvent::Shoot(entity, controller.look_dir)); + server_emitter.emit(ServerEvent::Shoot { + entity, + dir: controller.look_dir, + projectile: Projectile { + hit_ground: vec![projectile::Effect::Stick], + hit_entity: vec![ + projectile::Effect::Damage(10), + projectile::Effect::Vanish, + ], + }, + }); } } } diff --git a/common/src/sys/projectile.rs b/common/src/sys/projectile.rs index da53ab2352..d1109e21ac 100644 --- a/common/src/sys/projectile.rs +++ b/common/src/sys/projectile.rs @@ -10,8 +10,8 @@ impl<'a> System<'a> for Sys { type SystemData = ( Entities<'a>, Read<'a, EventBus>, - ReadStorage<'a, Vel>, ReadStorage<'a, PhysicsState>, + WriteStorage<'a, Vel>, WriteStorage<'a, Ori>, WriteStorage<'a, Projectile>, ); @@ -21,8 +21,8 @@ impl<'a> System<'a> for Sys { ( entities, server_bus, - velocities, physics_states, + mut velocities, mut orientations, mut projectiles, ): Self::SystemData, @@ -30,25 +30,25 @@ impl<'a> System<'a> for Sys { let mut server_emitter = server_bus.emitter(); // Attacks - for (entity, vel, physics, ori, projectile) in ( + for (entity, physics, ori, projectile) in ( &entities, - &velocities, &physics_states, &mut orientations, &mut projectiles, ) .join() { - ori.0 = vel.0.normalized(); + if let Some(vel) = velocities.get(entity) { + ori.0 = vel.0.normalized(); + } // Hit ground if physics.on_ground { for effect in projectile.hit_ground.drain(..) { match effect { - projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy { - entity, - cause: HealthSource::World, - }), + projectile::Effect::Stick => { + velocities.remove(entity); + } _ => {} } } @@ -68,6 +68,7 @@ impl<'a> System<'a> for Sys { entity, cause: HealthSource::World, }), + _ => {} } } } diff --git a/server/src/lib.rs b/server/src/lib.rs index 988ebe12e7..58b970f7f0 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -290,7 +290,11 @@ impl Server { } } - ServerEvent::Shoot(entity, dir /*, projectile*/) => { + ServerEvent::Shoot { + entity, + dir, + projectile, + } => { let pos = state .ecs() .read_storage::() @@ -302,13 +306,7 @@ impl Server { comp::Pos(pos), comp::Vel(dir * 100.0), comp::Body::Object(comp::object::Body::Arrow), - comp::Projectile { - hit_ground: vec![comp::projectile::Effect::Vanish], - hit_entity: vec![ - comp::projectile::Effect::Damage(10), - comp::projectile::Effect::Vanish, - ], - }, + projectile, ) .build(); }