From 2ecb976b143a3206ed9947f701d8776ca0938dec Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 23 Apr 2024 17:39:07 +0200 Subject: [PATCH] move around code to better match the module's purposes --- src/actor.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++++- src/camera.rs | 5 +- src/hud.rs | 6 +-- src/world.rs | 123 +------------------------------------------------- 4 files changed, 129 insertions(+), 128 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index f18838c..8dfcd31 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -10,15 +10,17 @@ // // This module manages the internal states of individual characters, // such as their resources, the damage they receive, and interactions -// between characters and with vehicles. +// between characters and with vehicles. It also handles cheats. use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; +use bevy_xpbd_3d::plugins::sync; use bevy::scene::SceneInstance; use bevy::math::DVec3; use crate::{actor, audio, camera, chat, commands, effects, hud, nature, var, world}; use std::collections::HashMap; +const CENTER_WORLD_ON_PLAYER: bool = true; pub const ENGINE_SPEED_FACTOR: f32 = 30.0; const MAX_TRANSMISSION_DISTANCE: f32 = 100.0; const MAX_INTERACT_DISTANCE: f32 = 50.0; @@ -39,6 +41,7 @@ impl Plugin for ActorPlugin { handle_input, handle_collisions, handle_damage, + handle_cheats, )); app.add_systems(PostUpdate, ( handle_vehicle_enter_exit, @@ -47,6 +50,18 @@ impl Plugin for ActorPlugin { app.add_event::(); app.add_event::(); app.insert_resource(Id2Pos(HashMap::new())); + + if CENTER_WORLD_ON_PLAYER { + // Disable bevy_xpbd's position->transform sync function + app.insert_resource(sync::SyncConfig { + position_to_transform: true, + transform_to_position: false, + }); + // Add own position->transform sync function + app.add_systems(PostUpdate, position_to_transform + .after(sync::position_to_transform) + .in_set(sync::SyncSet::PositionToTransform)); + } } } @@ -552,3 +567,109 @@ fn update_id2pos( id2pos.0.insert(id.0.clone(), pos.0); } } + +fn handle_cheats( + key_input: Res>, + mut q_player: Query<(&Transform, &mut Position, &mut LinearVelocity), With>, + mut q_life: Query<(&mut actor::LifeForm, &mut actor::ExperiencesGForce), With>, + q_target: Query<(&Transform, &Position, &LinearVelocity), (With, Without)>, + mut ew_playerdies: EventWriter, + mut settings: ResMut, + id2pos: Res, + mut ew_sfx: EventWriter, +) { + if q_player.is_empty() || q_life.is_empty() { + return; + } + let (trans, mut pos, mut v) = q_player.get_single_mut().unwrap(); + let (mut lifeform, mut gforce) = q_life.get_single_mut().unwrap(); + let boost = if key_input.pressed(KeyCode::ShiftLeft) { + 1e6 + } else { + 1e3 + }; + if key_input.just_pressed(settings.key_cheat_god_mode) { + settings.god_mode ^= true; + if settings.god_mode { + ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle)); + } + } + if !settings.god_mode && !settings.dev_mode { + return; + } + + if key_input.just_pressed(settings.key_cheat_stop) { + gforce.ignore_gforce_seconds = 1.0; + v.0 = DVec3::ZERO; + } + if key_input.pressed(settings.key_cheat_speed) { + gforce.ignore_gforce_seconds = 1.0; + v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, boost)); + } + if key_input.pressed(settings.key_cheat_speed_backward) { + gforce.ignore_gforce_seconds = 1.0; + v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, -boost)); + } + if key_input.just_pressed(settings.key_cheat_teleport) { + if let Ok((transform, target_pos, target_v)) = q_target.get_single() { + let offset: DVec3 = 4.0 * (**pos - **target_pos).normalize() * transform.scale.as_dvec3(); + pos.0 = **target_pos + offset; + *v = target_v.clone(); + } + } + + if !settings.dev_mode { + return; + } + + if key_input.just_pressed(settings.key_cheat_pizza) { + if let Some(target) = id2pos.0.get(&"pizzeria".to_string()) { + pos.0 = *target + DVec3::new(-60.0, 0.0, 0.0); + gforce.ignore_gforce_seconds = 1.0; + } + } + if key_input.just_pressed(settings.key_cheat_farview1) { + if let Some(target) = id2pos.0.get(&"busstop2".to_string()) { + pos.0 = *target + DVec3::new(0.0, -1000.0, 0.0); + gforce.ignore_gforce_seconds = 1.0; + } + } + if key_input.just_pressed(settings.key_cheat_farview2) { + if let Some(target) = id2pos.0.get(&"busstop3".to_string()) { + pos.0 = *target + DVec3::new(0.0, -1000.0, 0.0); + gforce.ignore_gforce_seconds = 1.0; + } + } + if key_input.pressed(settings.key_cheat_adrenaline_zero) { + lifeform.adrenaline = 0.0; + } + if key_input.pressed(settings.key_cheat_adrenaline_mid) { + lifeform.adrenaline = 0.5; + } + if key_input.pressed(settings.key_cheat_adrenaline_max) { + lifeform.adrenaline = 1.0; + } + if key_input.just_pressed(settings.key_cheat_die) { + settings.god_mode = false; + ew_playerdies.send(actor::PlayerDiesEvent(actor::DamageType::Trauma)); + } +} + +// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts +// the rendering position to center entities at the player camera. +// This avoids rendering glitches when very far away from the origin. +pub fn position_to_transform( + q_player: Query<&Position, With>, + mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without>, +) { + if let Ok(player_pos) = q_player.get_single() { + for (mut transform, pos, rot) in &mut q_trans { + transform.translation = Vec3::new( + (pos.x - player_pos.x) as f32, + (pos.y - player_pos.y) as f32, + (pos.z - player_pos.z) as f32, + ); + transform.rotation = rot.as_quat(); + } + } +} diff --git a/src/camera.rs b/src/camera.rs index 7d7526e..aceab5c 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -8,8 +8,9 @@ // + + + ███ // + ▀████████████████████████████████████████████████████▀ // -// This module manages the game's viewport and handles camera- and -// movement-related keyboard input. +// This module manages the game's viewport, handles camera- and +// movement-related keyboard input, and provides some camera- +// related computation functions. use bevy::prelude::*; use bevy::input::mouse::{MouseMotion, MouseWheel}; diff --git a/src/hud.rs b/src/hud.rs index 6dfee7a..bfa9370 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -10,7 +10,7 @@ // // This module manages the heads-up display and augmented reality overlays. -use crate::{actor, audio, camera, chat, nature, skeleton, var, world}; +use crate::{actor, audio, camera, chat, nature, skeleton, var}; use bevy::prelude::*; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::transform::TransformSystem; @@ -43,10 +43,10 @@ impl Plugin for HudPlugin { app.add_systems(PostUpdate, ( update_overlay_visibility, update_ar_overlays - .after(world::position_to_transform) + .after(actor::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_poi_overlays - .after(world::position_to_transform) + .after(actor::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_target_selectagon .after(PhysicsSet::Sync) diff --git a/src/world.rs b/src/world.rs index 9936fca..cf1720b 100644 --- a/src/world.rs +++ b/src/world.rs @@ -10,13 +10,12 @@ // // This module populates the world with stars and asteroids. -use crate::{actor, audio, hud, nature, shading, skeleton, var}; +use crate::{actor, hud, nature, shading, skeleton}; use bevy::prelude::*; use bevy::math::{DVec3, I64Vec3}; use bevy::scene::{InstanceId, SceneInstance}; use bevy::render::mesh::Indices; use bevy_xpbd_3d::prelude::*; -use bevy_xpbd_3d::plugins::sync; use std::collections::HashMap; use std::f32::consts::PI; use fastrand; @@ -26,7 +25,6 @@ const ASTEROID_SIZE_FACTOR: f32 = 10.0; const RING_THICKNESS: f64 = 8.0e6; const STARS_MAX_MAGNITUDE: f32 = 5.5; // max 7.0, see generate_starchart.py -const CENTER_WORLD_ON_PLAYER: bool = true; const SKYBOX: bool = false; const ASTEROID_SPAWN_STEP: f64 = 500.0; @@ -39,7 +37,6 @@ pub struct WorldPlugin; impl Plugin for WorldPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, handle_cheats); app.add_systems(PostUpdate, handle_despawn); app.add_systems(Update, spawn_despawn_asteroids); app.add_plugins(PhysicsPlugins::default()); @@ -49,18 +46,6 @@ impl Plugin for WorldPlugin { Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating))); app.insert_resource(ActiveAsteroids(HashMap::new())); app.add_event::(); - - if CENTER_WORLD_ON_PLAYER { - // Disable bevy_xpbd's position->transform sync function - app.insert_resource(sync::SyncConfig { - position_to_transform: true, - transform_to_position: false, - }); - // Add own position->transform sync function - app.add_systems(PostUpdate, position_to_transform - .after(sync::position_to_transform) - .in_set(sync::SyncSet::PositionToTransform)); - } } } @@ -359,109 +344,3 @@ fn handle_despawn( db.0.remove(&despawn.origin); } } - -fn handle_cheats( - key_input: Res>, - mut q_player: Query<(&Transform, &mut Position, &mut LinearVelocity), With>, - mut q_life: Query<(&mut actor::LifeForm, &mut actor::ExperiencesGForce), With>, - q_target: Query<(&Transform, &Position, &LinearVelocity), (With, Without)>, - mut ew_playerdies: EventWriter, - mut settings: ResMut, - id2pos: Res, - mut ew_sfx: EventWriter, -) { - if q_player.is_empty() || q_life.is_empty() { - return; - } - let (trans, mut pos, mut v) = q_player.get_single_mut().unwrap(); - let (mut lifeform, mut gforce) = q_life.get_single_mut().unwrap(); - let boost = if key_input.pressed(KeyCode::ShiftLeft) { - 1e6 - } else { - 1e3 - }; - if key_input.just_pressed(settings.key_cheat_god_mode) { - settings.god_mode ^= true; - if settings.god_mode { - ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle)); - } - } - if !settings.god_mode && !settings.dev_mode { - return; - } - - if key_input.just_pressed(settings.key_cheat_stop) { - gforce.ignore_gforce_seconds = 1.0; - v.0 = DVec3::ZERO; - } - if key_input.pressed(settings.key_cheat_speed) { - gforce.ignore_gforce_seconds = 1.0; - v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, boost)); - } - if key_input.pressed(settings.key_cheat_speed_backward) { - gforce.ignore_gforce_seconds = 1.0; - v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, -boost)); - } - if key_input.just_pressed(settings.key_cheat_teleport) { - if let Ok((transform, target_pos, target_v)) = q_target.get_single() { - let offset: DVec3 = 4.0 * (**pos - **target_pos).normalize() * transform.scale.as_dvec3(); - pos.0 = **target_pos + offset; - *v = target_v.clone(); - } - } - - if !settings.dev_mode { - return; - } - - if key_input.just_pressed(settings.key_cheat_pizza) { - if let Some(target) = id2pos.0.get(&"pizzeria".to_string()) { - pos.0 = *target + DVec3::new(-60.0, 0.0, 0.0); - gforce.ignore_gforce_seconds = 1.0; - } - } - if key_input.just_pressed(settings.key_cheat_farview1) { - if let Some(target) = id2pos.0.get(&"busstop2".to_string()) { - pos.0 = *target + DVec3::new(0.0, -1000.0, 0.0); - gforce.ignore_gforce_seconds = 1.0; - } - } - if key_input.just_pressed(settings.key_cheat_farview2) { - if let Some(target) = id2pos.0.get(&"busstop3".to_string()) { - pos.0 = *target + DVec3::new(0.0, -1000.0, 0.0); - gforce.ignore_gforce_seconds = 1.0; - } - } - if key_input.pressed(settings.key_cheat_adrenaline_zero) { - lifeform.adrenaline = 0.0; - } - if key_input.pressed(settings.key_cheat_adrenaline_mid) { - lifeform.adrenaline = 0.5; - } - if key_input.pressed(settings.key_cheat_adrenaline_max) { - lifeform.adrenaline = 1.0; - } - if key_input.just_pressed(settings.key_cheat_die) { - settings.god_mode = false; - ew_playerdies.send(actor::PlayerDiesEvent(actor::DamageType::Trauma)); - } -} - -// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts -// the rendering position to center entities at the player camera. -// This avoids rendering glitches when very far away from the origin. -pub fn position_to_transform( - q_player: Query<&Position, With>, - mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without>, -) { - if let Ok(player_pos) = q_player.get_single() { - for (mut transform, pos, rot) in &mut q_trans { - transform.translation = Vec3::new( - (pos.x - player_pos.x) as f32, - (pos.y - player_pos.y) as f32, - (pos.z - player_pos.z) as f32, - ); - transform.rotation = rot.as_quat(); - } - } -}