implement player respawn with "z" key

This commit is contained in:
yuni 2024-04-05 02:58:02 +02:00
parent cea7289460
commit 23bceebc79
6 changed files with 60 additions and 16 deletions

View file

@ -1,7 +1,8 @@
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
use bevy::scene::SceneInstance;
use bevy::math::DVec3;
use crate::{actor, audio, chat, nature, settings};
use crate::{actor, audio, chat, commands, nature, settings, world};
pub const ENGINE_SPEED_FACTOR: f32 = 30.0;
const MIN_INTERACT_DISTANCE: f32 = 30.0;
@ -10,6 +11,9 @@ const NO_RIDE: u32 = 0;
pub struct ActorPlugin;
impl Plugin for ActorPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, (
handle_player_death,
));
app.add_systems(FixedUpdate, (
update_physics_lifeforms,
handle_wants_maxrotation,
@ -23,9 +27,11 @@ impl Plugin for ActorPlugin {
handle_vehicle_enter_exit,
));
app.add_event::<VehicleEnterExitEvent>();
app.add_event::<PlayerDiesEvent>();
}
}
#[derive(Event)] pub struct PlayerDiesEvent;
#[derive(Event)]
pub struct VehicleEnterExitEvent {
vehicle: Entity,
@ -186,9 +192,13 @@ pub fn handle_input(
mut q_vehicles: Query<(Entity, &mut Visibility, &Transform), (With<actor::Vehicle>, Without<actor::Player>)>,
mut ew_conv: EventWriter<chat::StartConversationEvent>,
mut ew_vehicle: EventWriter<VehicleEnterExitEvent>,
mut ew_playerdies: EventWriter<PlayerDiesEvent>,
q_player_drives: Query<Entity, With<PlayerDrivesThis>>,
) {
let mindist = MIN_INTERACT_DISTANCE * MIN_INTERACT_DISTANCE;
if keyboard_input.just_pressed(settings.key_cheat_die) {
ew_playerdies.send(PlayerDiesEvent);
}
if keyboard_input.just_pressed(settings.key_interact) {
// Talking to people
if let Ok((_player_entity, _player_actor, player)) = player.get_single() {
@ -345,3 +355,26 @@ fn handle_wants_maxvelocity(
}
}
}
fn handle_player_death(
mut cmd: Commands,
mut er_playerdies: EventReader<PlayerDiesEvent>,
q_scenes: Query<(Entity, &SceneInstance), With<world::DespawnOnPlayerDeath>>,
q_noscenes: Query<Entity, (With<world::DespawnOnPlayerDeath>, Without<SceneInstance>)>,
ew_spawn: EventWriter<commands::SpawnEvent>,
mut scene_spawner: ResMut<SceneSpawner>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
) {
for _ in er_playerdies.read() {
for entity in &q_noscenes {
cmd.entity(entity).despawn();
}
for (entity, sceneinstance) in &q_scenes {
cmd.entity(entity).despawn();
scene_spawner.despawn_instance(**sceneinstance);
}
//cmd.run_system(commands::load_defs); // why is it so complicated to get SystemId?
commands::load_defs(ew_spawn);
return;
}
}

View file

@ -69,7 +69,7 @@ pub fn setup_camera(
pub fn sync_camera_to_player(
settings: Res<settings::Settings>,
mut q_camera: Query<&mut Transform, With<Camera>>,
mut q_camera: Query<&mut Transform, (With<Camera>, Without<actor::PlayerCamera>)>,
q_playercam: Query<(&actor::Actor, &Transform), (With<actor::PlayerCamera>, Without<Camera>)>,
) {
if q_camera.is_empty() || q_playercam.is_empty() {

View file

@ -1,5 +1,5 @@
use bevy::prelude::*;
use crate::{actor, audio, hud, settings};
use crate::{actor, audio, hud, settings, world};
pub struct ChatPlugin;
impl Plugin for ChatPlugin {
@ -193,12 +193,15 @@ pub fn handle_send_messages(
.collect();
for choice in choices {
if choice.choice.as_str() != hud::CHOICE_NONE {
commands.spawn(ChoiceAvailable {
commands.spawn((
ChoiceAvailable {
conv_id: choice.id.clone(),
conv_label: choice.label.clone(),
recipient: choice.name.clone(),
text: choice.choice.clone(),
});
},
world::DespawnOnPlayerDeath,
));
}
}
if !branch.script.is_empty() {
@ -269,12 +272,15 @@ pub fn handle_conversations(
.collect();
for choice in choices {
if choice.choice.as_str() != hud::CHOICE_NONE {
commands.spawn(ChoiceAvailable {
commands.spawn((
ChoiceAvailable {
conv_id: choice.id.clone(),
conv_label: choice.label.clone(),
recipient: choice.name.clone(),
text: choice.choice.clone(),
});
},
world::DespawnOnPlayerDeath,
));
}
}

View file

@ -571,7 +571,8 @@ fn spawn_entities(
let state = &state_wrapper.0;
if state.class == DefClass::Chat {
if state.stores_item {
commands.spawn(state.as_chatbranch());
let mut chat = commands.spawn(state.as_chatbranch());
chat.insert(world::DespawnOnPlayerDeath);
}
}
else if state.class == DefClass::Actor {
@ -581,6 +582,7 @@ fn spawn_entities(
camdistance: state.camdistance,
..default()
});
actor.insert(world::DespawnOnPlayerDeath);
actor.insert(Position::from(state.pos));
actor.insert(Rotation::from(state.rotation));
if state.is_sphere {

View file

@ -54,6 +54,7 @@ pub struct Settings {
pub key_cheat_adrenaline_zero: KeyCode,
pub key_cheat_adrenaline_mid: KeyCode,
pub key_cheat_adrenaline_max: KeyCode,
pub key_cheat_die: KeyCode,
}
impl Default for Settings {
@ -126,6 +127,7 @@ impl Default for Settings {
key_cheat_adrenaline_zero: KeyCode::F5,
key_cheat_adrenaline_mid: KeyCode::F6,
key_cheat_adrenaline_max: KeyCode::F7,
key_cheat_die: KeyCode::KeyZ,
}
}
}

View file

@ -71,6 +71,7 @@ impl Plugin for WorldPlugin {
#[derive(Resource)] struct ActiveAsteroids(HashMap<I64Vec3, AsteroidData>);
#[derive(Component)] struct Asteroid;
#[derive(Component)] pub struct DespawnOnPlayerDeath;
struct AsteroidData {
entity: Entity,