2019-09-17 12:43:19 +00:00
|
|
|
use crate::{
|
2020-03-27 16:07:19 +00:00
|
|
|
comp::{
|
|
|
|
projectile, Energy, EnergySource, HealthSource, Ori, PhysicsState, Pos, Projectile, Vel,
|
|
|
|
},
|
2020-03-26 21:52:44 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2019-10-06 17:30:06 +00:00
|
|
|
state::DeltaTime,
|
2020-03-26 21:52:44 +00:00
|
|
|
sync::UidAllocator,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2019-09-17 12:43:19 +00:00
|
|
|
};
|
2020-03-26 21:52:44 +00:00
|
|
|
use specs::{saveload::MarkerAllocator, Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-10-06 17:30:06 +00:00
|
|
|
use std::time::Duration;
|
2020-03-26 21:52:44 +00:00
|
|
|
use vek::*;
|
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>,
|
2020-03-26 21:52:44 +00:00
|
|
|
Read<'a, UidAllocator>,
|
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2019-09-17 12:43:19 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2020-03-22 19:39:50 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
2019-09-17 12:43:19 +00:00
|
|
|
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>,
|
2020-03-27 16:07:19 +00:00
|
|
|
WriteStorage<'a, Energy>,
|
2019-09-17 12:43:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-10-06 17:30:06 +00:00
|
|
|
dt,
|
2020-03-26 21:52:44 +00:00
|
|
|
uid_allocator,
|
|
|
|
local_bus,
|
2019-09-17 12:43:19 +00:00
|
|
|
server_bus,
|
2020-03-22 19:39:50 +00:00
|
|
|
positions,
|
2019-09-17 12:43:19 +00:00
|
|
|
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,
|
2020-03-27 16:07:19 +00:00
|
|
|
mut energies,
|
2019-09-17 12:43:19 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-03-26 21:52:44 +00:00
|
|
|
let mut local_emitter = local_bus.emitter();
|
2019-09-17 12:43:19 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
|
|
|
|
// Attacks
|
2020-03-22 19:39:50 +00:00
|
|
|
for (entity, pos, physics, ori, projectile) in (
|
2019-09-17 12:43:19 +00:00
|
|
|
&entities,
|
2020-03-22 19:39:50 +00:00
|
|
|
&positions,
|
2019-09-17 12:43:19 +00:00
|
|
|
&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 {
|
2020-03-22 19:39:50 +00:00
|
|
|
projectile::Effect::Explode { power } => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
|
|
|
owner: projectile.owner,
|
|
|
|
})
|
|
|
|
},
|
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 {
|
2020-03-22 19:39:50 +00:00
|
|
|
projectile::Effect::Explode { power } => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
|
|
|
owner: projectile.owner,
|
|
|
|
})
|
|
|
|
},
|
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) => {
|
2020-04-06 18:35:29 +00:00
|
|
|
if other != projectile.owner.unwrap() {
|
|
|
|
server_emitter.emit(ServerEvent::Damage { uid: other, change });
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-03-26 21:52:44 +00:00
|
|
|
projectile::Effect::Knockback(knockback) => {
|
|
|
|
if let Some(entity) =
|
|
|
|
uid_allocator.retrieve_entity_internal(other.into())
|
|
|
|
{
|
|
|
|
local_emitter.emit(LocalEvent::ApplyForce {
|
|
|
|
entity,
|
2020-03-28 02:19:23 +00:00
|
|
|
force: knockback
|
|
|
|
* *Dir::slerp(ori.0, Dir::new(Vec3::unit_z()), 0.5),
|
2020-03-26 21:52:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-03-27 16:07:19 +00:00
|
|
|
projectile::Effect::RewardEnergy(energy) => {
|
|
|
|
if let Some(energy_mut) = projectile
|
|
|
|
.owner
|
|
|
|
.and_then(|o| uid_allocator.retrieve_entity_internal(o.into()))
|
|
|
|
.and_then(|o| energies.get_mut(o))
|
|
|
|
{
|
|
|
|
energy_mut.change_by(energy as i32, EnergySource::HitEnemy);
|
|
|
|
}
|
|
|
|
},
|
2020-03-22 19:39:50 +00:00
|
|
|
projectile::Effect::Explode { power } => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
|
|
|
owner: projectile.owner,
|
|
|
|
})
|
|
|
|
},
|
2019-09-17 12:43:19 +00:00
|
|
|
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
}),
|
2020-03-16 11:32:57 +00:00
|
|
|
projectile::Effect::Possess => {
|
2020-04-06 18:35:29 +00:00
|
|
|
if other != projectile.owner.unwrap() {
|
|
|
|
if let Some(owner) = projectile.owner {
|
|
|
|
server_emitter.emit(ServerEvent::Possess(owner.into(), other));
|
|
|
|
}
|
2020-03-16 11:32:57 +00:00
|
|
|
}
|
|
|
|
},
|
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 {
|
2020-03-25 22:40:07 +00:00
|
|
|
if let Some(dir) = velocities
|
|
|
|
.get(entity)
|
|
|
|
.and_then(|vel| vel.0.try_normalized())
|
|
|
|
{
|
2020-03-28 01:31:22 +00:00
|
|
|
ori.0 = dir.into();
|
2019-10-02 19:28:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|