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
2 changed files with 90 additions and 88 deletions

View File

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

View File

@ -217,14 +217,20 @@ impl Server {
/// Handle events coming through via the event bus
fn handle_events(&mut self) {
let terrain = self.state.ecs().read_resource::<TerrainMap>();
let mut block_change = self.state.ecs().write_resource::<BlockChange>();
let mut stats = self.state.ecs().write_storage::<comp::Stats>();
let mut positions = self.state.ecs().write_storage::<comp::Pos>();
let mut velocities = self.state.ecs().write_storage::<comp::Vel>();
let mut force_updates = self.state.ecs().write_storage::<comp::ForceUpdate>();
let clients = &mut self.clients;
let events = self.state.ecs().read_resource::<EventBus>().recv_all();
for event in events {
let ecs = self.state.ecs_mut();
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 {
GameEvent::LandOnGround { entity, vel } => {
if let Some(stats) = stats.get_mut(entity) {
@ -260,37 +266,30 @@ impl Server {
}
GameEvent::Die { entity, cause } => {
// Chat message
if let Some(player) =
self.state.ecs().read_storage::<comp::Player>().get(entity)
{
if let Some(player) = ecs.read_storage::<comp::Player>().get(entity) {
let msg = if let comp::HealthSource::Attack { by } = cause {
self.state()
.ecs()
.entity_from_uid(by.into())
.and_then(|attacker| {
self.state
.ecs()
.read_storage::<comp::Player>()
.get(attacker)
.map(|attacker_alias| {
ecs.entity_from_uid(by.into()).and_then(|attacker| {
ecs.read_storage::<comp::Player>().get(attacker).map(
|attacker_alias| {
format!(
"{} was killed by {}",
&player.alias, &attacker_alias.alias
)
})
},
)
})
} else {
None
}
.unwrap_or(format!("{} died", &player.alias));
self.clients.notify_registered(ServerMsg::kill(msg));
clients.notify_registered(ServerMsg::kill(msg));
}
// Give EXP to the client
if let Some(entity_stats) = stats.get(entity).cloned() {
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) {
// TODO: Discuss whether we should give EXP by Player Killing or not.
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 _ = force_updates.insert(entity, comp::ForceUpdate);
client.force_state(ClientState::Dead);
} else {
//let _ = self.state.ecs_mut().delete_entity_synced(entity);
continue;
todo_remove = Some(entity.clone());
}
}
GameEvent::Respawn(entity) => {
// 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);
stats.get_mut(entity).map(|stats| stats.revive());
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.