2019-09-17 12:43:19 +00:00
|
|
|
use crate::{
|
2020-03-27 16:07:19 +00:00
|
|
|
comp::{
|
2020-07-07 00:11:37 +00:00
|
|
|
projectile, Alignment, Energy, EnergySource, HealthSource, Ori, PhysicsState, Pos,
|
|
|
|
Projectile, Vel,
|
2020-03-27 16:07:19 +00:00
|
|
|
},
|
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 {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-09-17 12:43:19 +00:00
|
|
|
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>,
|
2020-07-07 00:01:39 +00:00
|
|
|
ReadStorage<'a, Alignment>,
|
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,
|
2020-07-07 00:01:39 +00:00
|
|
|
alignments,
|
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()
|
|
|
|
{
|
2020-04-26 15:16:35 +00:00
|
|
|
// Hit something solid
|
|
|
|
if physics.on_wall.is_some() || physics.on_ground || physics.on_ceiling {
|
|
|
|
for effect in projectile.hit_solid.drain(..) {
|
2019-10-06 17:30:06 +00:00
|
|
|
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-07-07 00:01:39 +00:00
|
|
|
let owner_uid = projectile.owner.unwrap();
|
|
|
|
// Hacky: remove this when groups get implemented
|
|
|
|
let passive = uid_allocator
|
|
|
|
.retrieve_entity_internal(other.into())
|
2020-07-07 00:11:37 +00:00
|
|
|
.and_then(|other| {
|
2020-07-07 00:01:39 +00:00
|
|
|
alignments
|
2020-07-07 00:11:37 +00:00
|
|
|
.get(other)
|
|
|
|
.map(|a| Alignment::Owned(owner_uid).passive_towards(*a))
|
|
|
|
})
|
2020-07-07 00:01:39 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
if other != projectile.owner.unwrap() && !passive {
|
2020-04-06 18:35:29 +00:00
|
|
|
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 {
|
2020-06-08 18:37:41 +00:00
|
|
|
server_emitter.emit(ServerEvent::Possess(owner, other));
|
2020-04-06 18:35:29 +00:00
|
|
|
}
|
2020-03-16 11:32:57 +00:00
|
|
|
}
|
|
|
|
},
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 18:37:41 +00:00
|
|
|
} else if let Some(dir) = velocities
|
|
|
|
.get(entity)
|
|
|
|
.and_then(|vel| vel.0.try_normalized())
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|