make death/respawn system work better

master
Drake 1 year ago
parent cac85f20d7
commit e5c701c0d3

@ -23,6 +23,8 @@ use crate::PLAYER_COUNT;
static PLAYER_HEIGHT_HISTORY: Lazy<Mutex<HashMap<i32, f64>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
static PLAYER_DEATHS: Lazy<Mutex<HashMap<Uuid, bool>>> = Lazy::new(|| Mutex::new(HashMap::new()));
pub fn init_clients(
mut new_clients: Query<
(
@ -146,11 +148,28 @@ pub fn fall_damage(
}
}
pub fn death(mut healths: Query<(&mut Client, &Health, &Username), Changed<Health>>) {
for (mut client, health, username) in &mut healths {
if health.0 <= 0.0 {
pub fn death(mut clients: Query<(&mut Client, &UniqueId, &Health, &Username)>) {
let mut messages: Vec<Text> = vec![];
for (mut client, id, health, username) in &mut clients {
let mut hashmap = PLAYER_DEATHS.lock().unwrap();
let is_player_dead = hashmap.get(&id.0).unwrap_or(&false);
if health.0 <= 0.0 && !is_player_dead {
tracing::debug!("Trying to kill player {}", username);
client.kill(None, format!("{} fell from a high place.", username));
client.kill(None, format!("{} fell off.", username));
hashmap.insert(id.0, true);
messages.push(
format!("{} fell off.", username)
.italic()
.color(Color::GRAY),
);
}
}
for (mut client, _, _, _) in &mut clients {
for message in messages.iter() {
client.send_message(message.clone());
}
}
}
@ -168,12 +187,20 @@ pub fn despawn_disconnected_clients(
}
pub fn respawn(
mut clients: Query<(&mut Position, &mut Look, &mut Location)>,
mut clients: Query<(
&mut Position,
&mut Look,
&mut Location,
&UniqueId,
&mut Health,
)>,
mut events: EventReader<PerformRespawn>,
instances: Query<Entity, With<Instance>>,
) {
for event in events.iter() {
if let Ok((mut pos, mut look, mut loc)) = clients.get_mut(event.client) {
if let Ok((mut pos, mut look, mut loc, uuid, mut health)) = clients.get_mut(event.client) {
let mut hashmap = PLAYER_DEATHS.lock().unwrap();
pos.set(DVec3 {
x: -214.0,
y: 83.0,
@ -182,6 +209,9 @@ pub fn respawn(
look.yaw = 0.0;
look.pitch = 0.0;
loc.0 = instances.single();
health.0 = 20.0;
hashmap.insert(uuid.0, false);
}
}
}

Loading…
Cancel
Save