Allow non player entites to be removed again

This commit is contained in:
timokoesters 2019-08-23 22:50:35 +02:00
parent 90c81b4759
commit 4d6a32e00f
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
2 changed files with 90 additions and 88 deletions

View File

@ -122,7 +122,6 @@ impl<'a> System<'a> for Sys {
// Jump // Jump
if controller.jump && physics.on_ground && vel.0.z <= 0.0 { if controller.jump && physics.on_ground && vel.0.z <= 0.0 {
dbg!();
event_emitter.emit(Event::Jump(entity)); event_emitter.emit(Event::Jump(entity));
} }
} }

View File

@ -217,14 +217,20 @@ impl Server {
/// Handle events coming through via the event bus /// Handle events coming through via the event bus
fn handle_events(&mut self) { fn handle_events(&mut self) {
let terrain = self.state.ecs().read_resource::<TerrainMap>(); let clients = &mut self.clients;
let mut block_change = self.state.ecs().write_resource::<BlockChange>();
let mut stats = self.state.ecs().write_storage::<comp::Stats>(); let events = self.state.ecs().read_resource::<EventBus>().recv_all();
let mut positions = self.state.ecs().write_storage::<comp::Pos>(); for event in events {
let mut velocities = self.state.ecs().write_storage::<comp::Vel>(); let ecs = self.state.ecs_mut();
let mut force_updates = self.state.ecs().write_storage::<comp::ForceUpdate>(); let mut todo_remove = None;
{
let terrain = ecs.read_resource::<TerrainMap>();
let mut block_change = ecs.write_resource::<BlockChange>();
let mut stats = ecs.write_storage::<comp::Stats>();
let mut positions = ecs.write_storage::<comp::Pos>();
let mut velocities = ecs.write_storage::<comp::Vel>();
let mut force_updates = ecs.write_storage::<comp::ForceUpdate>();
for event in self.state.ecs().read_resource::<EventBus>().recv_all() {
match event { match event {
GameEvent::LandOnGround { entity, vel } => { GameEvent::LandOnGround { entity, vel } => {
if let Some(stats) = stats.get_mut(entity) { if let Some(stats) = stats.get_mut(entity) {
@ -260,37 +266,30 @@ impl Server {
} }
GameEvent::Die { entity, cause } => { GameEvent::Die { entity, cause } => {
// Chat message // Chat message
if let Some(player) = if let Some(player) = ecs.read_storage::<comp::Player>().get(entity) {
self.state.ecs().read_storage::<comp::Player>().get(entity)
{
let msg = if let comp::HealthSource::Attack { by } = cause { let msg = if let comp::HealthSource::Attack { by } = cause {
self.state() ecs.entity_from_uid(by.into()).and_then(|attacker| {
.ecs() ecs.read_storage::<comp::Player>().get(attacker).map(
.entity_from_uid(by.into()) |attacker_alias| {
.and_then(|attacker| {
self.state
.ecs()
.read_storage::<comp::Player>()
.get(attacker)
.map(|attacker_alias| {
format!( format!(
"{} was killed by {}", "{} was killed by {}",
&player.alias, &attacker_alias.alias &player.alias, &attacker_alias.alias
) )
}) },
)
}) })
} else { } else {
None None
} }
.unwrap_or(format!("{} died", &player.alias)); .unwrap_or(format!("{} died", &player.alias));
self.clients.notify_registered(ServerMsg::kill(msg)); clients.notify_registered(ServerMsg::kill(msg));
} }
// Give EXP to the client // Give EXP to the client
if let Some(entity_stats) = stats.get(entity).cloned() { if let Some(entity_stats) = stats.get(entity).cloned() {
if let comp::HealthSource::Attack { by } = cause { if let comp::HealthSource::Attack { by } = cause {
self.state.ecs().entity_from_uid(by.into()).map(|attacker| { ecs.entity_from_uid(by.into()).map(|attacker| {
if let Some(attacker_stats) = stats.get_mut(attacker) { if let Some(attacker_stats) = stats.get_mut(attacker) {
// TODO: Discuss whether we should give EXP by Player Killing or not. // TODO: Discuss whether we should give EXP by Player Killing or not.
attacker_stats.exp.change_by( attacker_stats.exp.change_by(
@ -302,26 +301,30 @@ impl Server {
} }
} }
if let Some(client) = self.clients.get_mut(&entity) { if let Some(client) = clients.get_mut(&entity) {
let _ = velocities.insert(entity, comp::Vel(Vec3::zero())); let _ = velocities.insert(entity, comp::Vel(Vec3::zero()));
let _ = force_updates.insert(entity, comp::ForceUpdate); let _ = force_updates.insert(entity, comp::ForceUpdate);
client.force_state(ClientState::Dead); client.force_state(ClientState::Dead);
} else { } else {
//let _ = self.state.ecs_mut().delete_entity_synced(entity); todo_remove = Some(entity.clone());
continue;
} }
} }
GameEvent::Respawn(entity) => { GameEvent::Respawn(entity) => {
// Only clients can respawn // Only clients can respawn
if let Some(client) = self.clients.get_mut(&entity) { if let Some(client) = clients.get_mut(&entity) {
client.allow_state(ClientState::Character); client.allow_state(ClientState::Character);
stats.get_mut(entity).map(|stats| stats.revive()); stats.get_mut(entity).map(|stats| stats.revive());
positions.get_mut(entity).map(|pos| pos.0.z += 20.0); positions.get_mut(entity).map(|pos| pos.0.z += 20.0);
force_updates.insert(entity, comp::ForceUpdate); let _ = force_updates.insert(entity, comp::ForceUpdate);
} }
} }
} }
} }
if let Some(entity) = todo_remove {
let _ = ecs.delete_entity_synced(entity);
}
}
} }
/// Execute a single server tick, handle input and update the game state by the given duration. /// Execute a single server tick, handle input and update the game state by the given duration.