2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-02-28 20:02:03 +00:00
|
|
|
combat::{AttackerInfo, TargetInfo},
|
2021-01-30 05:17:40 +00:00
|
|
|
comp::{
|
2021-03-02 22:19:38 +00:00
|
|
|
projectile, Combo, Energy, Group, HealthSource, Inventory, Ori, PhysicsState, Pos,
|
|
|
|
Projectile, Stats, Vel,
|
2021-01-30 05:17:40 +00:00
|
|
|
},
|
2020-11-07 18:00:07 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-12-01 00:28:00 +00:00
|
|
|
resources::DeltaTime,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::UidAllocator,
|
2021-02-04 09:17:38 +00:00
|
|
|
util::Dir,
|
2020-11-02 00:26:01 +00:00
|
|
|
GroupTarget,
|
2019-09-17 12:43:19 +00:00
|
|
|
};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2020-09-14 12:56:05 +00:00
|
|
|
use specs::{
|
2021-03-08 08:36:53 +00:00
|
|
|
saveload::MarkerAllocator, shred::ResourceId, Entities, Join, Read, ReadStorage, SystemData,
|
|
|
|
World, WriteStorage,
|
2020-09-14 12:56:05 +00:00
|
|
|
};
|
2019-10-06 17:30:06 +00:00
|
|
|
use std::time::Duration;
|
2019-09-17 12:43:19 +00:00
|
|
|
|
2021-02-22 18:37:42 +00:00
|
|
|
#[derive(SystemData)]
|
2021-02-22 21:02:37 +00:00
|
|
|
pub struct ReadData<'a> {
|
2021-02-22 18:37:42 +00:00
|
|
|
entities: Entities<'a>,
|
|
|
|
dt: Read<'a, DeltaTime>,
|
|
|
|
uid_allocator: Read<'a, UidAllocator>,
|
|
|
|
server_bus: Read<'a, EventBus<ServerEvent>>,
|
|
|
|
positions: ReadStorage<'a, Pos>,
|
|
|
|
physics_states: ReadStorage<'a, PhysicsState>,
|
|
|
|
velocities: ReadStorage<'a, Vel>,
|
|
|
|
inventories: ReadStorage<'a, Inventory>,
|
|
|
|
groups: ReadStorage<'a, Group>,
|
|
|
|
energies: ReadStorage<'a, Energy>,
|
2021-02-28 20:02:03 +00:00
|
|
|
stats: ReadStorage<'a, Stats>,
|
2021-03-02 22:19:38 +00:00
|
|
|
combos: ReadStorage<'a, Combo>,
|
2021-02-22 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:43:19 +00:00
|
|
|
/// This system is responsible for handling projectile effect triggers
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2019-09-17 12:43:19 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2019-09-17 12:43:19 +00:00
|
|
|
type SystemData = (
|
2021-02-22 21:02:37 +00:00
|
|
|
ReadData<'a>,
|
2019-09-21 12:43:24 +00:00
|
|
|
WriteStorage<'a, Ori>,
|
2019-09-17 12:43:19 +00:00
|
|
|
WriteStorage<'a, Projectile>,
|
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "projectile";
|
|
|
|
const ORIGIN: Origin = Origin::Common;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2021-03-08 11:13:59 +00:00
|
|
|
fn run(_job: &mut Job<Self>, (read_data, mut orientations, mut projectiles): Self::SystemData) {
|
2021-02-22 21:02:37 +00:00
|
|
|
let mut server_emitter = read_data.server_bus.emitter();
|
2019-09-17 12:43:19 +00:00
|
|
|
|
|
|
|
// Attacks
|
2021-02-01 15:41:34 +00:00
|
|
|
'projectile_loop: for (entity, pos, physics, ori, mut projectile) in (
|
2021-02-22 21:02:37 +00:00
|
|
|
&read_data.entities,
|
|
|
|
&read_data.positions,
|
|
|
|
&read_data.physics_states,
|
2019-09-21 12:43:24 +00:00
|
|
|
&mut orientations,
|
2019-09-17 12:43:19 +00:00
|
|
|
&mut projectiles,
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2021-02-01 15:41:34 +00:00
|
|
|
let mut projectile_vanished: bool = false;
|
2020-08-24 23:04:04 +00:00
|
|
|
// Hit entity
|
|
|
|
for other in physics.touch_entities.iter().copied() {
|
2020-10-17 22:42:43 +00:00
|
|
|
let same_group = projectile
|
|
|
|
.owner
|
|
|
|
// Note: somewhat inefficient since we do the lookup for every touching
|
|
|
|
// entity, but if we pull this out of the loop we would want to do it only
|
|
|
|
// if there is at least one touching entity
|
2021-02-22 21:02:37 +00:00
|
|
|
.and_then(|uid| read_data.uid_allocator.retrieve_entity_internal(uid.into()))
|
|
|
|
.and_then(|e| read_data.groups.get(e))
|
2020-10-17 22:42:43 +00:00
|
|
|
.map_or(false, |owner_group|
|
2021-02-22 21:02:37 +00:00
|
|
|
Some(owner_group) == read_data.uid_allocator
|
2020-10-17 22:42:43 +00:00
|
|
|
.retrieve_entity_internal(other.into())
|
2021-02-22 21:02:37 +00:00
|
|
|
.and_then(|e| read_data.groups.get(e))
|
2020-10-17 22:42:43 +00:00
|
|
|
);
|
2020-11-02 00:26:01 +00:00
|
|
|
|
|
|
|
let target_group = if same_group {
|
|
|
|
GroupTarget::InGroup
|
|
|
|
} else {
|
|
|
|
GroupTarget::OutOfGroup
|
|
|
|
};
|
|
|
|
|
2020-09-19 23:35:14 +00:00
|
|
|
if projectile.ignore_group
|
|
|
|
// Skip if in the same group
|
2020-10-17 22:42:43 +00:00
|
|
|
&& same_group
|
2020-09-19 23:35:14 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:04:04 +00:00
|
|
|
if projectile.owner == Some(other) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:25:12 +00:00
|
|
|
let projectile = &mut *projectile;
|
2020-09-16 01:06:45 +00:00
|
|
|
for effect in projectile.hit_entity.drain(..) {
|
2019-10-06 17:30:06 +00:00
|
|
|
match effect {
|
2021-01-29 00:04:44 +00:00
|
|
|
projectile::Effect::Attack(attack) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(target) = read_data
|
2021-02-22 18:37:42 +00:00
|
|
|
.uid_allocator
|
|
|
|
.retrieve_entity_internal(other.into())
|
2021-02-01 02:43:26 +00:00
|
|
|
{
|
2021-02-22 18:37:42 +00:00
|
|
|
let owner_entity = projectile.owner.and_then(|u| {
|
2021-02-22 21:02:37 +00:00
|
|
|
read_data.uid_allocator.retrieve_entity_internal(u.into())
|
2021-02-22 18:37:42 +00:00
|
|
|
});
|
2021-02-02 18:02:40 +00:00
|
|
|
|
|
|
|
let attacker_info =
|
|
|
|
owner_entity.zip(projectile.owner).map(|(entity, uid)| {
|
|
|
|
AttackerInfo {
|
|
|
|
entity,
|
|
|
|
uid,
|
2021-02-22 21:02:37 +00:00
|
|
|
energy: read_data.energies.get(entity),
|
2021-03-02 22:19:38 +00:00
|
|
|
combo: read_data.combos.get(entity),
|
2021-02-02 18:02:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-28 20:02:03 +00:00
|
|
|
let target_info = TargetInfo {
|
|
|
|
entity: target,
|
|
|
|
inventory: read_data.inventories.get(target),
|
|
|
|
stats: read_data.stats.get(target),
|
|
|
|
};
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
attack.apply_attack(
|
2021-02-01 02:43:26 +00:00
|
|
|
target_group,
|
2021-02-02 18:02:40 +00:00
|
|
|
attacker_info,
|
2021-02-28 20:02:03 +00:00
|
|
|
target_info,
|
2021-02-04 09:17:38 +00:00
|
|
|
ori.look_dir(),
|
2021-02-01 02:43:26 +00:00
|
|
|
false,
|
|
|
|
1.0,
|
2021-02-02 18:02:40 +00:00
|
|
|
|e| server_emitter.emit(e),
|
2021-02-01 02:43:26 +00:00
|
|
|
);
|
2020-08-24 23:04:04 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-06 02:19:41 +00:00
|
|
|
projectile::Effect::Explode(e) => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
explosion: e,
|
|
|
|
owner: projectile.owner,
|
|
|
|
})
|
|
|
|
},
|
2021-02-01 15:41:34 +00:00
|
|
|
projectile::Effect::Vanish => {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
projectile_vanished = true;
|
|
|
|
},
|
2020-08-24 23:04:04 +00:00
|
|
|
projectile::Effect::Possess => {
|
|
|
|
if other != projectile.owner.unwrap() {
|
|
|
|
if let Some(owner) = projectile.owner {
|
|
|
|
server_emitter.emit(ServerEvent::Possess(owner, other));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-02-01 20:39:39 +00:00
|
|
|
_ => {},
|
2019-10-06 17:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 15:41:34 +00:00
|
|
|
|
|
|
|
if projectile_vanished {
|
|
|
|
continue 'projectile_loop;
|
|
|
|
}
|
2020-08-24 23:04:04 +00:00
|
|
|
}
|
2020-08-24 17:24:44 +00:00
|
|
|
|
2020-08-24 23:04:04 +00:00
|
|
|
// Hit something solid
|
|
|
|
if physics.on_wall.is_some() || physics.on_ground || physics.on_ceiling {
|
2021-01-07 20:25:12 +00:00
|
|
|
let projectile = &mut *projectile;
|
2020-08-24 23:04:04 +00:00
|
|
|
for effect in projectile.hit_solid.drain(..) {
|
|
|
|
match effect {
|
2020-10-06 02:19:41 +00:00
|
|
|
projectile::Effect::Explode(e) => {
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
explosion: e,
|
|
|
|
owner: projectile.owner,
|
|
|
|
})
|
|
|
|
},
|
2021-02-01 15:41:34 +00:00
|
|
|
projectile::Effect::Vanish => {
|
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::World,
|
|
|
|
});
|
|
|
|
projectile_vanished = true;
|
|
|
|
},
|
2020-08-24 23:04:04 +00:00
|
|
|
_ => {},
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 15:41:34 +00:00
|
|
|
|
|
|
|
if projectile_vanished {
|
|
|
|
continue 'projectile_loop;
|
|
|
|
}
|
2021-02-22 21:02:37 +00:00
|
|
|
} else if let Some(dir) = read_data
|
2021-02-22 18:37:42 +00:00
|
|
|
.velocities
|
2020-08-24 23:04:04 +00:00
|
|
|
.get(entity)
|
2021-02-04 09:17:38 +00:00
|
|
|
.and_then(|vel| Dir::from_unnormalized(vel.0))
|
2020-08-24 23:04:04 +00:00
|
|
|
{
|
2021-02-04 09:17:38 +00:00
|
|
|
*ori = 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
|
2021-02-22 21:02:37 +00:00
|
|
|
.checked_sub(Duration::from_secs_f32(read_data.dt.0))
|
2019-10-06 17:30:06 +00:00
|
|
|
.unwrap_or_default();
|
2019-09-17 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|