Projectile system now uses immutable data struct

This commit is contained in:
Sam 2021-02-22 13:37:42 -05:00
parent 91e982b3ee
commit b278cad807

View File

@ -12,57 +12,46 @@ use common::{
GroupTarget, GroupTarget,
}; };
use specs::{ use specs::{
saveload::MarkerAllocator, Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage, saveload::MarkerAllocator, shred::ResourceId, Entities, Join, Read, ReadExpect, ReadStorage,
System, SystemData, World, WriteStorage,
}; };
use std::time::Duration; use std::time::Duration;
#[derive(SystemData)]
pub struct ImmutableData<'a> {
entities: Entities<'a>,
dt: Read<'a, DeltaTime>,
uid_allocator: Read<'a, UidAllocator>,
server_bus: Read<'a, EventBus<ServerEvent>>,
metrics: ReadExpect<'a, SysMetrics>,
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>,
}
/// This system is responsible for handling projectile effect triggers /// This system is responsible for handling projectile effect triggers
pub struct Sys; pub struct Sys;
impl<'a> System<'a> for Sys { impl<'a> System<'a> for Sys {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
type SystemData = ( type SystemData = (
Entities<'a>, ImmutableData<'a>,
Read<'a, DeltaTime>,
Read<'a, UidAllocator>,
Read<'a, EventBus<ServerEvent>>,
ReadExpect<'a, SysMetrics>,
ReadStorage<'a, Pos>,
ReadStorage<'a, PhysicsState>,
ReadStorage<'a, Vel>,
WriteStorage<'a, Ori>, WriteStorage<'a, Ori>,
WriteStorage<'a, Projectile>, WriteStorage<'a, Projectile>,
ReadStorage<'a, Inventory>,
ReadStorage<'a, Group>,
ReadStorage<'a, Energy>,
); );
fn run( fn run(&mut self, (immutable_data, mut orientations, mut projectiles): Self::SystemData) {
&mut self,
(
entities,
dt,
uid_allocator,
server_bus,
sys_metrics,
positions,
physics_states,
velocities,
mut orientations,
mut projectiles,
inventories,
groups,
energies,
): Self::SystemData,
) {
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
span!(_guard, "run", "projectile::Sys::run"); span!(_guard, "run", "projectile::Sys::run");
let mut server_emitter = server_bus.emitter(); let mut server_emitter = immutable_data.server_bus.emitter();
// Attacks // Attacks
'projectile_loop: for (entity, pos, physics, ori, mut projectile) in ( 'projectile_loop: for (entity, pos, physics, ori, mut projectile) in (
&entities, &immutable_data.entities,
&positions, &immutable_data.positions,
&physics_states, &immutable_data.physics_states,
&mut orientations, &mut orientations,
&mut projectiles, &mut projectiles,
) )
@ -76,12 +65,12 @@ impl<'a> System<'a> for Sys {
// Note: somewhat inefficient since we do the lookup for every touching // 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 // 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 // if there is at least one touching entity
.and_then(|uid| uid_allocator.retrieve_entity_internal(uid.into())) .and_then(|uid| immutable_data.uid_allocator.retrieve_entity_internal(uid.into()))
.and_then(|e| groups.get(e)) .and_then(|e| immutable_data.groups.get(e))
.map_or(false, |owner_group| .map_or(false, |owner_group|
Some(owner_group) == uid_allocator Some(owner_group) == immutable_data.uid_allocator
.retrieve_entity_internal(other.into()) .retrieve_entity_internal(other.into())
.and_then(|e| groups.get(e)) .and_then(|e| immutable_data.groups.get(e))
); );
let target_group = if same_group { let target_group = if same_group {
@ -105,19 +94,22 @@ impl<'a> System<'a> for Sys {
for effect in projectile.hit_entity.drain(..) { for effect in projectile.hit_entity.drain(..) {
match effect { match effect {
projectile::Effect::Attack(attack) => { projectile::Effect::Attack(attack) => {
if let Some(target_entity) = if let Some(target_entity) = immutable_data
uid_allocator.retrieve_entity_internal(other.into()) .uid_allocator
.retrieve_entity_internal(other.into())
{ {
let owner_entity = projectile let owner_entity = projectile.owner.and_then(|u| {
.owner immutable_data
.and_then(|u| uid_allocator.retrieve_entity_internal(u.into())); .uid_allocator
.retrieve_entity_internal(u.into())
});
let attacker_info = let attacker_info =
owner_entity.zip(projectile.owner).map(|(entity, uid)| { owner_entity.zip(projectile.owner).map(|(entity, uid)| {
AttackerInfo { AttackerInfo {
entity, entity,
uid, uid,
energy: energies.get(entity), energy: immutable_data.energies.get(entity),
} }
}); });
@ -125,7 +117,7 @@ impl<'a> System<'a> for Sys {
target_group, target_group,
attacker_info, attacker_info,
target_entity, target_entity,
inventories.get(target_entity), immutable_data.inventories.get(target_entity),
ori.look_dir(), ori.look_dir(),
false, false,
1.0, 1.0,
@ -191,7 +183,8 @@ impl<'a> System<'a> for Sys {
if projectile_vanished { if projectile_vanished {
continue 'projectile_loop; continue 'projectile_loop;
} }
} else if let Some(dir) = velocities } else if let Some(dir) = immutable_data
.velocities
.get(entity) .get(entity)
.and_then(|vel| Dir::from_unnormalized(vel.0)) .and_then(|vel| Dir::from_unnormalized(vel.0))
{ {
@ -206,10 +199,10 @@ impl<'a> System<'a> for Sys {
} }
projectile.time_left = projectile projectile.time_left = projectile
.time_left .time_left
.checked_sub(Duration::from_secs_f32(dt.0)) .checked_sub(Duration::from_secs_f32(immutable_data.dt.0))
.unwrap_or_default(); .unwrap_or_default();
} }
sys_metrics.projectile_ns.store( immutable_data.metrics.projectile_ns.store(
start_time.elapsed().as_nanos() as u64, start_time.elapsed().as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed, std::sync::atomic::Ordering::Relaxed,
); );