simlify player handling in handle_chat_scripts()

This commit is contained in:
yuni 2024-11-26 15:14:56 +01:00
parent a29b7c8436
commit 03e96e6ecd

View file

@ -91,7 +91,9 @@ impl Plugin for ChatPlugin {
handle_chat_events
.run_if(game_running)
.before(handle_chat_scripts),
handle_chat_scripts.run_if(game_running),
handle_chat_scripts
.run_if(game_running)
.run_if(on_event::<ChatScriptEvent>()),
update_chat_variables.run_if(game_running),
),
);
@ -913,6 +915,14 @@ pub fn handle_chat_scripts(
id2pos: Res<game::Id2Pos>,
id2v: Res<game::Id2V>,
) {
let (player_entity, _, mut suit, mut gforce) =
if let Ok(player_data) = q_player.get_single_mut() {
player_data
} else {
error!("Can't access player in handle_chat_scripts");
return;
};
for script in er_chatscript.read() {
// Parse the script string
let mut parts = script.0.split_whitespace();
@ -929,32 +939,29 @@ pub fn handle_chat_scripts(
match name {
"refilloxygen" => {
if let Ok(mut amount) = param1.to_string().parse::<f32>() {
for (_, _, mut suit, _) in q_player.iter_mut() {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Refill));
if param2.is_empty() {
suit.oxygen = (suit.oxygen + amount).clamp(0.0, suit.oxygen_max);
} else {
let mut found_other = false;
info!("param2={}", param2);
for (other_actor, mut other_suit) in q_actor.iter_mut() {
if !other_actor.id.is_empty() {
info!("ID={}", other_actor.id);
}
if other_actor.id == param2 {
found_other = true;
amount = amount
.clamp(0.0, other_suit.oxygen)
.clamp(0.0, suit.oxygen_max - suit.oxygen);
other_suit.oxygen = other_suit.oxygen - amount;
suit.oxygen =
(suit.oxygen + amount).clamp(0.0, suit.oxygen_max);
break;
}
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Refill));
if param2.is_empty() {
suit.oxygen = (suit.oxygen + amount).clamp(0.0, suit.oxygen_max);
} else {
let mut found_other = false;
info!("param2={}", param2);
for (other_actor, mut other_suit) in q_actor.iter_mut() {
if !other_actor.id.is_empty() {
info!("ID={}", other_actor.id);
}
if !found_other {
error!("Script error: could not find actor with ID `{}`", param2);
if other_actor.id == param2 {
found_other = true;
amount = amount
.clamp(0.0, other_suit.oxygen)
.clamp(0.0, suit.oxygen_max - suit.oxygen);
other_suit.oxygen = other_suit.oxygen - amount;
suit.oxygen = (suit.oxygen + amount).clamp(0.0, suit.oxygen_max);
break;
}
}
if !found_other {
error!("Script error: could not find actor with ID `{}`", param2);
}
}
} else {
error!("Invalid parameter for command `{}`: `{}`", name, param1);
@ -962,9 +969,7 @@ pub fn handle_chat_scripts(
}
"repairsuit" => {
ew_achievement.send(game::AchievementEvent::RepairSuit);
for (_, _, mut suit, _) in q_player.iter_mut() {
suit.integrity = 1.0;
}
suit.integrity = 1.0;
}
"cryotrip" => {
if param1.is_empty() {
@ -1011,10 +1016,8 @@ pub fn handle_chat_scripts(
}
}
}
if let Ok((_, _, mut suit, mut gforce)) = q_player.get_single_mut() {
suit.oxygen = suit.oxygen_max;
gforce.ignore_gforce_seconds = 1.0;
}
suit.oxygen = suit.oxygen_max;
gforce.ignore_gforce_seconds = 1.0;
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::WakeUp));
ew_effect.send(visual::SpawnEffectEvent {
class: visual::Effects::FadeIn(css::AQUA.into()),
@ -1053,20 +1056,18 @@ pub fn handle_chat_scripts(
ew_playerdies.send(game::PlayerDiesEvent(actor::DamageType::Bullet));
}
"addcatears" => {
if let Ok((player_entity, _, _, _)) = q_player.get_single_mut() {
let mut entitycmd = commands.spawn((
hud::AugmentedRealityOverlay {
owner: player_entity,
scale: 1.0,
always_visible: true,
},
world::DespawnOnPlayerDeath,
SpatialBundle::default(),
));
load_asset("suit_ar_nekomimi", &mut entitycmd, &*asset_server);
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Crash));
vars.set_in_scope("$", "catears", "1".to_string());
}
let mut entitycmd = commands.spawn((
hud::AugmentedRealityOverlay {
owner: player_entity,
scale: 1.0,
always_visible: true,
},
world::DespawnOnPlayerDeath,
SpatialBundle::default(),
));
load_asset("suit_ar_nekomimi", &mut entitycmd, &*asset_server);
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Crash));
vars.set_in_scope("$", "catears", "1".to_string());
}
_ => {
error!("Error, undefined chat script {name}");