2019-09-17 12:43:19 +00:00
|
|
|
use crate::{
|
2020-02-01 20:39:39 +00:00
|
|
|
comp::{projectile, HealthSource, Ori, PhysicsState, Projectile, Vel},
|
2019-09-17 12:43:19 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2019-10-06 17:30:06 +00:00
|
|
|
state::DeltaTime,
|
2019-09-17 12:43:19 +00:00
|
|
|
};
|
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-10-06 17:30:06 +00:00
|
|
|
use std::time::Duration;
|
2019-09-17 12:43:19 +00:00
|
|
|
|
|
|
|
/// This system is responsible for handling projectile effect triggers
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-10-06 17:30:06 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2019-09-17 12:43:19 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
ReadStorage<'a, PhysicsState>,
|
2019-10-02 19:28:35 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2019-09-21 12:43:24 +00:00
|
|
|
WriteStorage<'a, Ori>,
|
2019-09-17 12:43:19 +00:00
|
|
|
WriteStorage<'a, Projectile>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-10-06 17:30:06 +00:00
|
|
|
dt,
|
2019-09-17 12:43:19 +00:00
|
|
|
server_bus,
|
|
|
|
physics_states,
|
2019-10-02 19:28:35 +00:00
|
|
|
velocities,
|
2019-09-21 12:43:24 +00:00
|
|
|
mut orientations,
|
2019-09-17 12:43:19 +00:00
|
|
|
mut projectiles,
|
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
|
|
|
|
// Attacks
|
2019-09-28 19:35:28 +00:00
|
|
|
for (entity, physics, ori, projectile) in (
|
2019-09-17 12:43:19 +00:00
|
|
|
&entities,
|
|
|
|
&physics_states,
|
2019-09-21 12:43:24 +00:00
|
|
|
&mut orientations,
|
2019-09-17 12:43:19 +00:00
|
|
|
&mut projectiles,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-10-06 17:30:06 +00:00
|
|
|
// Hit ground
|
|
|
|
if physics.on_ground {
|
|
|
|
for effect in projectile.hit_ground.drain(..) {
|
|
|
|
match effect {
|
2019-10-11 04:30:34 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-10-06 17:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Hit wall
|
|
|
|
else if physics.on_wall.is_some() {
|
|
|
|
for effect in projectile.hit_wall.drain(..) {
|
|
|
|
match effect {
|
2019-10-11 04:30:34 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-10-06 17:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-21 12:43:24 +00:00
|
|
|
// Hit entity
|
2019-10-06 17:30:06 +00:00
|
|
|
else if let Some(other) = physics.touch_entity {
|
2019-09-17 12:43:19 +00:00
|
|
|
for effect in projectile.hit_entity.drain(..) {
|
|
|
|
match effect {
|
2019-10-17 20:59:36 +00:00
|
|
|
projectile::Effect::Damage(change) => {
|
|
|
|
server_emitter.emit(ServerEvent::Damage { uid: other, change })
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-09-17 12:43:19 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
2019-10-17 20:59:36 +00:00
|
|
|
projectile::Effect::Possess => server_emitter
|
|
|
|
.emit(ServerEvent::Possess(projectile.owner.into(), other)),
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 19:28:35 +00:00
|
|
|
} else {
|
|
|
|
if let Some(vel) = velocities.get(entity) {
|
|
|
|
ori.0 = vel.0.normalized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:30:06 +00:00
|
|
|
if projectile.time_left == Duration::default() {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
projectile.time_left = projectile
|
|
|
|
.time_left
|
|
|
|
.checked_sub(Duration::from_secs_f32(dt.0))
|
|
|
|
.unwrap_or_default();
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|