veloren/common/src/sys/projectile.rs

149 lines
5.5 KiB
Rust
Raw Normal View History

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,
},
event::{EventBus, LocalEvent, ServerEvent},
state::DeltaTime,
sync::UidAllocator,
util::Dir,
2019-09-17 12:43:19 +00:00
};
use specs::{saveload::MarkerAllocator, Entities, Join, Read, ReadStorage, System, WriteStorage};
use std::time::Duration;
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>,
Read<'a, DeltaTime>,
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>,
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,
dt,
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,
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,
) {
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(..) {
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,
}),
_ => {},
}
}
}
2019-09-21 12:43:24 +00:00
// Hit entity
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 {
projectile::Effect::Damage(change) => {
if other != projectile.owner.unwrap() {
server_emitter.emit(ServerEvent::Damage { uid: other, change });
}
},
projectile::Effect::Knockback(knockback) => {
if let Some(entity) =
uid_allocator.retrieve_entity_internal(other.into())
{
local_emitter.emit(LocalEvent::ApplyForce {
entity,
force: knockback
* *Dir::slerp(ori.0, Dir::new(Vec3::unit_z()), 0.5),
});
}
},
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,
}),
projectile::Effect::Possess => {
if other != projectile.owner.unwrap() {
if let Some(owner) = projectile.owner {
server_emitter.emit(ServerEvent::Possess(owner, other));
}
}
},
_ => {},
2019-09-17 12:43:19 +00:00
}
}
} else if let Some(dir) = velocities
.get(entity)
.and_then(|vel| vel.0.try_normalized())
{
ori.0 = dir.into();
}
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
}
}
}