veloren/common/src/sys/projectile.rs
Monty Marz fdfab6a807 refactor: create_projectile doesn't take a light
It returns a builder so the caller can just do it
2019-10-17 20:59:36 +00:00

104 lines
3.4 KiB
Rust

use crate::{
comp::{projectile, /*HealthChange,*/ HealthSource, Ori, PhysicsState, Projectile, Vel},
event::{EventBus, ServerEvent},
state::DeltaTime,
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
use std::time::Duration;
/// 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, EventBus<ServerEvent>>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, Vel>,
WriteStorage<'a, Ori>,
WriteStorage<'a, Projectile>,
);
fn run(
&mut self,
(
entities,
dt,
server_bus,
physics_states,
velocities,
mut orientations,
mut projectiles,
): Self::SystemData,
) {
let mut server_emitter = server_bus.emitter();
// Attacks
for (entity, physics, ori, projectile) in (
&entities,
&physics_states,
&mut orientations,
&mut projectiles,
)
.join()
{
// 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,
}),
_ => {}
}
}
}
// Hit wall
else if physics.on_wall.is_some() {
for effect in projectile.hit_wall.drain(..) {
match effect {
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
entity,
cause: HealthSource::World,
}),
_ => {}
}
}
}
// Hit entity
else if let Some(other) = physics.touch_entity {
for effect in projectile.hit_entity.drain(..) {
match effect {
projectile::Effect::Damage(change) => {
server_emitter.emit(ServerEvent::Damage { uid: other, change })
}
projectile::Effect::Vanish => server_emitter.emit(ServerEvent::Destroy {
entity,
cause: HealthSource::World,
}),
projectile::Effect::Possess => server_emitter
.emit(ServerEvent::Possess(projectile.owner.into(), other)),
_ => {}
}
}
} else {
if let Some(vel) = velocities.get(entity) {
ori.0 = vel.0.normalized();
}
}
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();
}
}
}