Rtsim death event

This commit is contained in:
Joshua Barretto 2023-01-05 23:15:43 +00:00
parent c4032ee024
commit 94390331e0
4 changed files with 29 additions and 9 deletions

View File

@ -1,4 +1,4 @@
use super::{RtState, Rule};
use crate::{data::NpcId, RtState, Rule};
use common::resources::{Time, TimeOfDay};
use world::{IndexRef, World};
@ -23,3 +23,9 @@ pub struct OnTick {
pub dt: f32,
}
impl Event for OnTick {}
#[derive(Clone)]
pub struct OnDeath {
pub npc_id: NpcId,
}
impl Event for OnDeath {}

View File

@ -77,10 +77,11 @@ impl Data {
// matches!(&index.sites.get(ws).kind, SiteKind::Refactor(_)))) .skip(1)
// .take(1)
{
let good_or_evil = site
let Some(good_or_evil) = site
.faction
.and_then(|f| this.factions.get(f))
.map_or(true, |f| f.good_or_evil);
.map(|f| f.good_or_evil)
else { continue };
let rand_wpos = |rng: &mut SmallRng| {
let wpos2d = site.wpos.map(|e| e + rng.gen_range(-10..10));
@ -89,7 +90,7 @@ impl Data {
.with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0))
};
if good_or_evil {
for _ in 0..250 {
for _ in 0..64 {
this.npcs.create(
Npc::new(rng.gen(), rand_wpos(&mut rng))
.with_faction(site.faction)

View File

@ -43,7 +43,7 @@ use rand_distr::Distribution;
use specs::{
join::Join, saveload::MarkerAllocator, Builder, Entity as EcsEntity, Entity, WorldExt,
};
use std::{collections::HashMap, iter, time::Duration};
use std::{collections::HashMap, iter, sync::Arc, time::Duration};
use tracing::{debug, error};
use vek::{Vec2, Vec3};
@ -528,7 +528,14 @@ pub fn handle_destroy(server: &mut Server, entity: EcsEntity, last_change: Healt
state
.ecs()
.write_resource::<rtsim2::RtSim>()
.hook_rtsim_entity_delete(rtsim_entity);
.hook_rtsim_entity_delete(
&state.ecs().read_resource::<Arc<world::World>>(),
state
.ecs()
.read_resource::<world::IndexOwned>()
.as_index_ref(),
rtsim_entity,
);
}
if let Err(e) = state.delete_entity_recorded(entity) {

View File

@ -13,7 +13,7 @@ use common_ecs::{dispatch, System};
use enum_map::EnumMap;
use rtsim2::{
data::{npc::NpcMode, Data, ReadError},
event::OnSetup,
event::{OnDeath, OnSetup},
rule::Rule,
RtState,
};
@ -154,8 +154,14 @@ impl RtSim {
}
}
pub fn hook_rtsim_entity_delete(&mut self, entity: RtSimEntity) {
// TODO: Emit event on deletion to catch death?
pub fn hook_rtsim_entity_delete(
&mut self,
world: &World,
index: IndexRef,
entity: RtSimEntity,
) {
// Should entity deletion be death? They're not exactly the same thing...
self.state.emit(OnDeath { npc_id: entity.0 }, world, index);
self.state.data_mut().npcs.remove(entity.0);
}