feat(bow): sticky arrows

This commit is contained in:
timokoesters 2019-09-28 21:35:28 +02:00
parent 575f7da64d
commit fc97c27b65
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
5 changed files with 35 additions and 22 deletions

View File

@ -5,6 +5,7 @@ use specs_idvs::IDVStorage;
pub enum Effect {
Damage(u32),
Vanish,
Stick,
}
#[derive(Clone, Debug, Serialize, Deserialize)]

View File

@ -36,7 +36,11 @@ pub enum ServerEvent {
cause: comp::HealthSource,
},
Respawn(EcsEntity),
Shoot(EcsEntity, Vec3<f32>),
Shoot {
entity: EcsEntity,
dir: Vec3<f32>,
projectile: comp::Projectile,
},
Mount(EcsEntity, EcsEntity),
Unmount(EcsEntity),
}

View File

@ -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,
],
},
});
}
}
}

View File

@ -10,8 +10,8 @@ impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
Read<'a, EventBus<ServerEvent>>,
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,
}),
_ => {}
}
}
}

View File

@ -290,7 +290,11 @@ impl Server {
}
}
ServerEvent::Shoot(entity, dir /*, projectile*/) => {
ServerEvent::Shoot {
entity,
dir,
projectile,
} => {
let pos = state
.ecs()
.read_storage::<comp::Pos>()
@ -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();
}