2024-04-21 16:23:40 +00:00
|
|
|
// ▄████████▄ + ███ + ▄█████████ ███ +
|
|
|
|
// ███▀ ▀███ + + ███ ███▀ + ███ + +
|
|
|
|
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
|
2024-04-21 17:34:00 +00:00
|
|
|
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
|
2024-04-21 16:23:40 +00:00
|
|
|
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
|
|
|
|
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
|
|
|
|
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
|
|
|
|
// + + + ███
|
|
|
|
// + ▀████████████████████████████████████████████████████▀
|
2024-04-23 15:33:36 +00:00
|
|
|
//
|
|
|
|
// This module manages the internal states of individual characters,
|
|
|
|
// such as their resources, the damage they receive, and interactions
|
2024-05-12 21:57:21 +00:00
|
|
|
// between characters and with vehicles.
|
2024-04-23 15:45:40 +00:00
|
|
|
//
|
|
|
|
// This module should never handle any visual aspects directly.
|
2024-04-21 16:23:40 +00:00
|
|
|
|
2024-05-22 02:57:20 +00:00
|
|
|
use crate::prelude::*;
|
2024-03-17 22:49:50 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-29 15:58:42 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-11-16 20:49:44 +00:00
|
|
|
use std::collections::HashMap;
|
2024-03-19 00:24:27 +00:00
|
|
|
|
2024-03-29 01:40:55 +00:00
|
|
|
pub const ENGINE_SPEED_FACTOR: f32 = 30.0;
|
2024-04-14 19:51:59 +00:00
|
|
|
const MAX_TRANSMISSION_DISTANCE: f32 = 100.0;
|
2024-04-17 13:24:00 +00:00
|
|
|
const MAX_INTERACT_DISTANCE: f32 = 50.0;
|
2024-09-22 15:12:23 +00:00
|
|
|
pub const POWER_DRAIN_THRUSTER: [f32; 3] = [3000.0, 3000.0, 0.0];
|
2024-09-22 04:12:15 +00:00
|
|
|
pub const THRUSTER_BOOST_FACTOR: [f64; 3] = [3.0, 3.0, 0.0];
|
2024-09-22 15:12:23 +00:00
|
|
|
pub const POWER_DRAIN_FLASHLIGHT: [f32; 4] = [200.0, 1500.0, 2500.0, 10000.0];
|
2024-09-22 04:39:21 +00:00
|
|
|
pub const FLASHLIGHT_INTENSITY: [f32; 4] = [10e6, 400e6, 2e9, 100e9]; // in lumens
|
2024-09-22 15:12:23 +00:00
|
|
|
pub const POWER_DRAIN_LIGHTAMP: [f32; 4] = [0.0, 200.0, 400.0, 1400.0];
|
|
|
|
pub const POWER_DRAIN_AR: f32 = 300.0;
|
|
|
|
pub const POWER_GAIN_REACTOR: [f32; 3] = [0.0, 2000.0, 10000.0];
|
2024-03-17 22:49:50 +00:00
|
|
|
|
|
|
|
pub struct ActorPlugin;
|
|
|
|
impl Plugin for ActorPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2024-05-22 02:57:20 +00:00
|
|
|
app.add_systems(
|
|
|
|
FixedUpdate,
|
|
|
|
(
|
2024-10-09 22:49:01 +00:00
|
|
|
update_physics_lifeforms.run_if(game_running),
|
|
|
|
update_power.run_if(game_running),
|
|
|
|
handle_gravity.run_if(game_running),
|
2024-11-16 23:02:00 +00:00
|
|
|
handle_wants_rotation_change.run_if(game_running),
|
2024-11-16 21:29:19 +00:00
|
|
|
handle_wants_acceleration.run_if(game_running).run_if(alive),
|
2024-10-09 22:49:01 +00:00
|
|
|
handle_wants_maxrotation.run_if(game_running),
|
|
|
|
handle_wants_maxvelocity
|
|
|
|
.run_if(game_running)
|
|
|
|
.run_if(any_with_component::<WantsMaxVelocity>),
|
|
|
|
handle_wants_lookat.run_if(game_running).run_if(alive),
|
2024-05-22 02:57:20 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
|
|
|
handle_gforce
|
2024-10-09 22:49:01 +00:00
|
|
|
.run_if(game_running)
|
2024-05-22 02:57:20 +00:00
|
|
|
.after(PhysicsSet::Sync)
|
|
|
|
.after(sync::position_to_transform),
|
|
|
|
);
|
|
|
|
app.add_systems(
|
|
|
|
Update,
|
|
|
|
(
|
2024-10-09 22:49:01 +00:00
|
|
|
handle_input.run_if(in_control).run_if(game_running),
|
|
|
|
handle_collisions.run_if(game_running),
|
|
|
|
handle_damage.run_if(game_running),
|
2024-05-22 02:57:20 +00:00
|
|
|
),
|
|
|
|
);
|
2024-10-09 22:49:01 +00:00
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
|
|
|
(handle_vehicle_enter_exit.run_if(game_running),),
|
|
|
|
);
|
2024-03-28 15:02:36 +00:00
|
|
|
app.add_event::<VehicleEnterExitEvent>();
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 18:46:52 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum DamageType {
|
|
|
|
Unknown,
|
|
|
|
Mental,
|
|
|
|
Trauma,
|
2024-05-12 20:30:53 +00:00
|
|
|
GForce,
|
2024-04-11 18:46:52 +00:00
|
|
|
Asphyxiation,
|
2024-05-14 17:12:36 +00:00
|
|
|
Depressurization,
|
2024-04-11 18:46:52 +00:00
|
|
|
//Poison,
|
2024-09-22 05:15:29 +00:00
|
|
|
Radiation,
|
2024-04-11 18:46:52 +00:00
|
|
|
//Freeze,
|
|
|
|
//Burn,
|
|
|
|
}
|
|
|
|
|
2024-03-28 15:02:36 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct VehicleEnterExitEvent {
|
|
|
|
vehicle: Entity,
|
|
|
|
driver: Entity,
|
2024-05-13 23:24:57 +00:00
|
|
|
name: Option<String>,
|
2024-03-28 15:02:36 +00:00
|
|
|
is_entering: bool,
|
2024-05-22 02:57:20 +00:00
|
|
|
is_player: bool,
|
2024-03-28 15:02:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Actor {
|
2024-03-23 20:26:56 +00:00
|
|
|
pub id: String,
|
2024-04-05 17:03:50 +00:00
|
|
|
pub name: Option<String>,
|
2024-03-29 18:41:46 +00:00
|
|
|
pub camdistance: f32,
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
impl Default for Actor {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-03-23 20:26:56 +00:00
|
|
|
id: "".to_string(),
|
2024-04-05 17:03:50 +00:00
|
|
|
name: None,
|
2024-04-29 23:26:59 +00:00
|
|
|
camdistance: 7.5,
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 01:31:52 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct HitPoints {
|
|
|
|
pub current: f32,
|
|
|
|
pub max: f32,
|
|
|
|
pub damage: f32,
|
2024-04-11 18:46:52 +00:00
|
|
|
pub damagetype: DamageType,
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
|
|
|
impl Default for HitPoints {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
current: 100.0,
|
|
|
|
max: 100.0,
|
|
|
|
damage: 0.0,
|
2024-04-11 18:46:52 +00:00
|
|
|
damagetype: DamageType::Unknown,
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 23:11:11 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ExperiencesGForce {
|
|
|
|
pub gforce: f32,
|
|
|
|
pub damage_threshold: f32,
|
2024-04-08 00:36:47 +00:00
|
|
|
pub visual_effect_threshold: f32,
|
|
|
|
pub visual_effect: f32,
|
2024-04-05 23:11:11 +00:00
|
|
|
pub last_linear_velocity: DVec3,
|
2024-06-11 01:29:26 +00:00
|
|
|
pub gravitational_component: DVec3,
|
2024-04-05 23:36:14 +00:00
|
|
|
pub ignore_gforce_seconds: f32,
|
2024-04-05 23:11:11 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
impl Default for ExperiencesGForce {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
gforce: 0.0,
|
|
|
|
damage_threshold: 100.0,
|
|
|
|
visual_effect_threshold: 20.0,
|
|
|
|
visual_effect: 0.0,
|
2024-06-11 01:29:26 +00:00
|
|
|
last_linear_velocity: DVec3::ZERO,
|
|
|
|
gravitational_component: DVec3::ZERO,
|
2024-06-08 02:13:53 +00:00
|
|
|
ignore_gforce_seconds: 0.01,
|
2024-05-22 02:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 23:11:11 +00:00
|
|
|
|
2024-11-16 20:49:44 +00:00
|
|
|
#[derive(Component, Default)]
|
|
|
|
pub struct WantsAcceleration {
|
|
|
|
pub direction: DVec3,
|
|
|
|
pub brake: bool,
|
|
|
|
}
|
|
|
|
|
2024-05-22 02:57:20 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Player; // Attached to the suit of the player
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct PlayerCollider; // Attached to the collider of the suit of the player
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct PlayerDrivesThis; // Attached to the entered vehicle
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct PlayerCamera; // Attached to the actor to use as point of view
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct JustNowEnteredVehicle;
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ActorEnteringVehicle;
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ActorVehicleBeingEntered;
|
|
|
|
#[derive(Component)]
|
2024-10-03 23:00:47 +00:00
|
|
|
pub struct MessageOnVehicleEntry(pub String);
|
|
|
|
#[derive(Component)]
|
2024-05-22 02:57:20 +00:00
|
|
|
pub struct PlayersFlashLight;
|
|
|
|
#[derive(Component)]
|
2024-11-11 22:20:29 +00:00
|
|
|
pub struct MirrorLight;
|
|
|
|
#[derive(Component)]
|
2024-05-22 02:57:20 +00:00
|
|
|
pub struct WantsMaxRotation(pub f64);
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct WantsMaxVelocity(pub f64);
|
|
|
|
#[derive(Component)]
|
2024-05-22 22:31:31 +00:00
|
|
|
pub struct WantsToLookAt(pub String);
|
|
|
|
#[derive(Component)]
|
2024-11-16 23:02:00 +00:00
|
|
|
pub struct WantsRotationChange(pub Vec3); // Vec3 = (pitch, yaw, rot)
|
2024-11-16 18:55:42 +00:00
|
|
|
#[derive(Component)]
|
2024-06-11 00:46:31 +00:00
|
|
|
pub struct WantsMatchVelocityWith(pub String);
|
|
|
|
#[derive(Component)]
|
2024-05-22 02:57:20 +00:00
|
|
|
pub struct Identifier(pub String);
|
2024-06-08 02:17:28 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct OrbitsJupiter;
|
2024-03-19 00:24:27 +00:00
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct LifeForm {
|
2024-03-20 17:37:42 +00:00
|
|
|
pub is_alive: bool,
|
2024-09-22 05:15:29 +00:00
|
|
|
pub is_radioactively_damaged: bool,
|
2024-03-17 22:49:50 +00:00
|
|
|
pub adrenaline: f32,
|
|
|
|
pub adrenaline_baseline: f32,
|
|
|
|
pub adrenaline_jolt: f32,
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
impl Default for LifeForm {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
is_alive: true,
|
2024-09-22 05:15:29 +00:00
|
|
|
is_radioactively_damaged: false,
|
2024-05-22 02:57:20 +00:00
|
|
|
adrenaline: 0.3,
|
|
|
|
adrenaline_baseline: 0.3,
|
|
|
|
adrenaline_jolt: 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 22:49:50 +00:00
|
|
|
|
2024-03-28 12:26:14 +00:00
|
|
|
#[derive(Component)]
|
2024-03-30 21:31:07 +00:00
|
|
|
pub struct Vehicle {
|
|
|
|
stored_drivers_collider: Option<Collider>,
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
impl Default for Vehicle {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
stored_drivers_collider: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-28 12:26:14 +00:00
|
|
|
|
2024-03-28 13:10:10 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub enum EngineType {
|
|
|
|
Monopropellant,
|
2024-03-29 03:36:20 +00:00
|
|
|
Ion,
|
2024-03-28 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 21:29:19 +00:00
|
|
|
#[derive(Component, Copy, Clone)]
|
2024-03-28 12:26:14 +00:00
|
|
|
pub struct Engine {
|
|
|
|
pub thrust_forward: f32,
|
|
|
|
pub thrust_back: f32,
|
|
|
|
pub thrust_sideways: f32,
|
|
|
|
pub reaction_wheels: f32,
|
2024-03-28 13:10:10 +00:00
|
|
|
pub engine_type: EngineType,
|
2024-03-28 23:01:17 +00:00
|
|
|
pub warmup_seconds: f32,
|
2024-04-15 19:55:46 +00:00
|
|
|
pub current_warmup: f32, // between 0.0 and 1.0
|
2024-09-22 02:34:36 +00:00
|
|
|
pub current_boost_factor: f64,
|
|
|
|
pub currently_firing: bool,
|
|
|
|
pub currently_matching_velocity: bool,
|
2024-03-28 12:26:14 +00:00
|
|
|
}
|
|
|
|
impl Default for Engine {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
thrust_forward: 1.0,
|
|
|
|
thrust_back: 1.0,
|
|
|
|
thrust_sideways: 1.0,
|
|
|
|
reaction_wheels: 1.0,
|
2024-03-28 13:10:10 +00:00
|
|
|
engine_type: EngineType::Monopropellant,
|
2024-03-28 23:01:17 +00:00
|
|
|
warmup_seconds: 1.5,
|
|
|
|
current_warmup: 0.0,
|
2024-09-22 02:34:36 +00:00
|
|
|
current_boost_factor: 1.0,
|
|
|
|
currently_firing: false,
|
|
|
|
currently_matching_velocity: false,
|
2024-03-28 12:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 22:49:50 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Suit {
|
|
|
|
pub oxygen: f32,
|
|
|
|
pub oxygen_max: f32,
|
2024-03-20 17:37:10 +00:00
|
|
|
pub integrity: f32, // [0.0 - 1.0]
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
impl Default for Suit {
|
|
|
|
fn default() -> Self {
|
|
|
|
SUIT_SIMPLE
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 22:49:50 +00:00
|
|
|
|
|
|
|
const SUIT_SIMPLE: Suit = Suit {
|
2024-03-18 22:53:52 +00:00
|
|
|
oxygen: nature::OXY_D,
|
|
|
|
oxygen_max: nature::OXY_D,
|
2024-03-30 18:39:53 +00:00
|
|
|
integrity: 1.0,
|
2024-03-17 22:49:50 +00:00
|
|
|
};
|
|
|
|
|
2024-05-08 00:35:36 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Battery {
|
2024-05-22 02:57:20 +00:00
|
|
|
pub power: f32, // Watt-seconds
|
2024-05-08 00:35:36 +00:00
|
|
|
pub capacity: f32, // Watt-seconds
|
2024-09-22 03:50:01 +00:00
|
|
|
pub overloaded_recovering: bool,
|
2024-05-08 00:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Battery {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-09-22 15:12:23 +00:00
|
|
|
power: 10.0 * 3600.0,
|
|
|
|
capacity: 10.0 * 3600.0, // 10Wh
|
2024-09-22 03:50:01 +00:00
|
|
|
overloaded_recovering: false,
|
2024-05-08 00:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_power(
|
|
|
|
time: Res<Time>,
|
2024-05-12 20:17:17 +00:00
|
|
|
mut settings: ResMut<Settings>,
|
2024-09-22 01:10:11 +00:00
|
|
|
prefs: Res<Preferences>,
|
2024-09-22 02:34:36 +00:00
|
|
|
mut q_battery: Query<(&mut Battery, &mut Engine), With<Player>>,
|
2024-05-08 00:35:36 +00:00
|
|
|
mut q_flashlight: Query<&mut Visibility, With<PlayersFlashLight>>,
|
2024-10-03 04:26:06 +00:00
|
|
|
q_bike: Query<&PlayerDrivesThis>,
|
2024-05-08 00:35:36 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-09-22 01:10:11 +00:00
|
|
|
mut ew_game: EventWriter<game::GameEvent>,
|
2024-05-08 00:35:36 +00:00
|
|
|
) {
|
2024-09-22 03:50:01 +00:00
|
|
|
let mut power_down = false;
|
2024-05-08 00:35:36 +00:00
|
|
|
let d = time.delta_seconds();
|
2024-10-03 04:26:06 +00:00
|
|
|
let inside_vehicle = !q_bike.is_empty();
|
2024-09-22 02:34:36 +00:00
|
|
|
for (mut battery, mut engine) in &mut q_battery {
|
2024-10-03 04:26:06 +00:00
|
|
|
if !inside_vehicle && !settings.god_mode {
|
2024-10-03 03:59:50 +00:00
|
|
|
if settings.flashlight_active {
|
|
|
|
battery.power -= POWER_DRAIN_FLASHLIGHT[prefs.flashlight_power] * d; // 2.4MW
|
|
|
|
if battery.power <= 0.0 {
|
|
|
|
power_down = true;
|
|
|
|
settings.flashlight_active = false;
|
|
|
|
for mut flashlight_vis in &mut q_flashlight {
|
|
|
|
*flashlight_vis = Visibility::Hidden;
|
|
|
|
}
|
2024-05-08 00:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-03 03:59:50 +00:00
|
|
|
if settings.hud_active {
|
|
|
|
let mut hud_drain = POWER_DRAIN_AR;
|
|
|
|
hud_drain += POWER_DRAIN_LIGHTAMP[prefs.light_amp];
|
|
|
|
battery.power -= hud_drain * d;
|
|
|
|
if battery.power <= 0.0 {
|
|
|
|
power_down = true;
|
|
|
|
ew_game.send(GameEvent::SetAR(Turn::Off));
|
|
|
|
for mut flashlight_vis in &mut q_flashlight {
|
|
|
|
*flashlight_vis = Visibility::Hidden;
|
|
|
|
}
|
2024-09-22 01:10:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-03 03:59:50 +00:00
|
|
|
let drain = POWER_DRAIN_THRUSTER[prefs.thruster_boost];
|
|
|
|
let boosting = !battery.overloaded_recovering
|
|
|
|
&& prefs.thruster_boost != 2
|
|
|
|
&& (prefs.thruster_boost == 1 || engine.currently_matching_velocity);
|
|
|
|
if boosting {
|
|
|
|
if battery.power > drain * d * 100.0 {
|
|
|
|
engine.current_boost_factor = THRUSTER_BOOST_FACTOR[prefs.thruster_boost];
|
|
|
|
if engine.currently_firing {
|
|
|
|
battery.power -= drain * d;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
power_down = true;
|
|
|
|
battery.overloaded_recovering = true;
|
|
|
|
engine.current_boost_factor = 1.0;
|
2024-09-22 03:50:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
engine.current_boost_factor = 1.0;
|
2024-09-22 02:34:36 +00:00
|
|
|
}
|
2024-10-03 03:59:50 +00:00
|
|
|
if power_down {
|
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::PowerDown));
|
|
|
|
}
|
2024-09-22 03:50:01 +00:00
|
|
|
}
|
2024-09-22 04:38:44 +00:00
|
|
|
let reactor = POWER_GAIN_REACTOR[settings.reactor_state];
|
|
|
|
battery.power = (battery.power + reactor * d).clamp(0.0, battery.capacity);
|
2024-09-22 03:50:01 +00:00
|
|
|
if battery.overloaded_recovering && battery.power > battery.capacity * 0.5 {
|
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::PowerUp));
|
|
|
|
battery.overloaded_recovering = false;
|
|
|
|
}
|
2024-05-08 00:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:14:12 +00:00
|
|
|
pub fn update_physics_lifeforms(
|
2024-03-17 22:49:50 +00:00
|
|
|
time: Res<Time>,
|
2024-09-22 05:15:29 +00:00
|
|
|
settings: Res<Settings>,
|
|
|
|
id2pos: Res<game::Id2Pos>,
|
2024-10-03 04:08:51 +00:00
|
|
|
mut query: Query<(
|
|
|
|
&mut LifeForm,
|
|
|
|
&mut HitPoints,
|
|
|
|
&mut Suit,
|
|
|
|
&LinearVelocity,
|
|
|
|
&Position,
|
|
|
|
Option<&Player>,
|
|
|
|
)>,
|
2024-10-03 04:48:03 +00:00
|
|
|
q_bike: Query<&PlayerDrivesThis>,
|
2024-03-17 22:49:50 +00:00
|
|
|
) {
|
|
|
|
let d = time.delta_seconds();
|
2024-10-03 04:48:03 +00:00
|
|
|
let inside_vehicle = !q_bike.is_empty();
|
2024-09-22 05:15:29 +00:00
|
|
|
for (mut lifeform, mut hp, mut suit, velocity, pos, player) in query.iter_mut() {
|
2024-03-17 22:49:50 +00:00
|
|
|
if lifeform.adrenaline_jolt.abs() > 1e-3 {
|
|
|
|
lifeform.adrenaline_jolt *= 0.99;
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-03-17 22:49:50 +00:00
|
|
|
lifeform.adrenaline_jolt = 0.0
|
|
|
|
}
|
2024-03-30 21:31:07 +00:00
|
|
|
let speed = velocity.length();
|
2024-03-28 22:13:59 +00:00
|
|
|
if speed > 1000.0 {
|
|
|
|
lifeform.adrenaline += 0.001;
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
lifeform.adrenaline =
|
|
|
|
(lifeform.adrenaline - 0.0001 + lifeform.adrenaline_jolt * 0.01).clamp(0.0, 1.0);
|
2024-03-20 17:37:10 +00:00
|
|
|
|
2024-09-22 05:15:29 +00:00
|
|
|
if player.is_some() {
|
2024-10-03 04:48:03 +00:00
|
|
|
lifeform.is_radioactively_damaged = if !inside_vehicle && settings.reactor_state == 2 {
|
2024-09-22 05:15:29 +00:00
|
|
|
true
|
|
|
|
} else if let Some(pos_jupiter) = id2pos.0.get(cmd::ID_JUPITER) {
|
|
|
|
pos_jupiter.distance(pos.0) < 140_000_000.0
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if lifeform.is_radioactively_damaged {
|
|
|
|
hp.damage += 0.3 * d;
|
|
|
|
hp.damagetype = DamageType::Radiation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 17:37:10 +00:00
|
|
|
let mut oxygen_drain = nature::OXY_S;
|
|
|
|
let integr_threshold = 0.5;
|
|
|
|
if suit.integrity < integr_threshold {
|
|
|
|
// The oxygen drain from suit integrity scales with (2 - 2x)^4,
|
|
|
|
// which is a function with ~16 at x=0 and 0 at x=1.
|
|
|
|
// Furthermore, x is divided by the integrity threshold (e.g. 0.5)
|
|
|
|
// to squeeze the function horizontally, and change the range for
|
|
|
|
// the x parameter from [0-1] to [0-integritythreshold]
|
|
|
|
//
|
|
|
|
// 16 |.
|
|
|
|
// |.
|
|
|
|
// |'.
|
|
|
|
// | '.
|
|
|
|
// | '..
|
|
|
|
// |______''....
|
|
|
|
// x=0 x=1
|
|
|
|
let drain_scaling = (2.0 - 2.0 * suit.integrity / integr_threshold).powf(4.0);
|
|
|
|
oxygen_drain += suit.oxygen * 0.01 * drain_scaling;
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
suit.oxygen = (suit.oxygen - oxygen_drain * d).clamp(0.0, suit.oxygen_max);
|
2024-04-05 01:31:52 +00:00
|
|
|
if suit.oxygen <= 0.0 {
|
|
|
|
hp.damage += 1.0 * d;
|
2024-04-11 18:46:52 +00:00
|
|
|
hp.damagetype = DamageType::Asphyxiation;
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
2024-03-17 22:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:24:27 +00:00
|
|
|
pub fn handle_input(
|
2024-03-28 12:26:14 +00:00
|
|
|
mut commands: Commands,
|
2024-03-19 00:24:27 +00:00
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
2024-05-12 20:17:17 +00:00
|
|
|
mut settings: ResMut<Settings>,
|
2024-04-05 17:57:55 +00:00
|
|
|
q_talker: Query<(&chat::Talker, &Transform), (Without<actor::Player>, Without<Camera>)>,
|
2024-04-05 18:05:58 +00:00
|
|
|
player: Query<Entity, With<actor::Player>>,
|
2024-04-05 17:57:55 +00:00
|
|
|
q_camera: Query<&Transform, With<Camera>>,
|
2024-05-07 15:11:18 +00:00
|
|
|
mut q_flashlight: Query<&mut Visibility, With<PlayersFlashLight>>,
|
2024-05-22 02:57:20 +00:00
|
|
|
q_vehicles: Query<
|
2024-10-03 23:00:47 +00:00
|
|
|
(Entity, &Actor, &Transform, Option<&MessageOnVehicleEntry>),
|
2024-05-22 02:57:20 +00:00
|
|
|
(
|
|
|
|
With<actor::Vehicle>,
|
|
|
|
Without<actor::Player>,
|
|
|
|
Without<Camera>,
|
|
|
|
),
|
|
|
|
>,
|
2024-04-04 11:33:54 +00:00
|
|
|
mut ew_conv: EventWriter<chat::StartConversationEvent>,
|
2024-03-28 15:02:36 +00:00
|
|
|
mut ew_vehicle: EventWriter<VehicleEnterExitEvent>,
|
2024-05-07 15:11:18 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-10-03 23:00:47 +00:00
|
|
|
mut log: ResMut<hud::Log>,
|
2024-03-28 15:02:36 +00:00
|
|
|
q_player_drives: Query<Entity, With<PlayerDrivesThis>>,
|
|
|
|
) {
|
2024-04-05 18:01:44 +00:00
|
|
|
if q_camera.is_empty() || player.is_empty() {
|
2024-04-05 17:57:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let camtrans = q_camera.get_single().unwrap();
|
2024-04-05 18:05:58 +00:00
|
|
|
let player_entity = player.get_single().unwrap();
|
2024-04-05 17:57:55 +00:00
|
|
|
|
2024-03-19 00:24:27 +00:00
|
|
|
if keyboard_input.just_pressed(settings.key_interact) {
|
2024-04-03 12:21:47 +00:00
|
|
|
// Talking to people
|
2024-04-05 18:01:44 +00:00
|
|
|
let objects: Vec<(chat::Talker, &Transform)> = q_talker
|
|
|
|
.iter()
|
|
|
|
.map(|(talker, transform)| (talker.clone(), transform))
|
|
|
|
.collect();
|
|
|
|
// TODO: replace Transform.translation with Position
|
2024-05-22 02:57:20 +00:00
|
|
|
if let (Some(talker), dist) = camera::find_closest_target::<chat::Talker>(objects, camtrans)
|
|
|
|
{
|
2024-04-05 18:01:44 +00:00
|
|
|
if dist <= MAX_TRANSMISSION_DISTANCE {
|
2024-05-22 02:57:20 +00:00
|
|
|
ew_conv.send(chat::StartConversationEvent {
|
|
|
|
talker: talker.clone(),
|
|
|
|
});
|
2024-03-19 00:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 15:02:36 +00:00
|
|
|
// Entering Vehicles
|
|
|
|
if q_player_drives.is_empty() {
|
2024-10-03 23:00:47 +00:00
|
|
|
// Sort vehicles by their distance to the player
|
|
|
|
let objects: Vec<((Entity, &Actor, Option<&MessageOnVehicleEntry>), &Transform)> =
|
|
|
|
q_vehicles
|
|
|
|
.iter()
|
|
|
|
.map(|(entity, actor, transform, msg)| ((entity, actor, msg), transform))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Get the vehicle with shortest distance
|
|
|
|
if let (Some((entity, actor, msg)), dist) =
|
|
|
|
camera::find_closest_target::<(Entity, &Actor, Option<&MessageOnVehicleEntry>)>(
|
|
|
|
objects, camtrans,
|
|
|
|
)
|
2024-05-13 23:24:57 +00:00
|
|
|
{
|
2024-04-05 18:05:58 +00:00
|
|
|
if dist <= MAX_INTERACT_DISTANCE {
|
|
|
|
commands.entity(entity).insert(ActorVehicleBeingEntered);
|
2024-04-05 18:01:44 +00:00
|
|
|
commands.entity(player_entity).insert(ActorEnteringVehicle);
|
2024-05-22 02:57:20 +00:00
|
|
|
ew_vehicle.send(VehicleEnterExitEvent {
|
2024-04-05 18:05:58 +00:00
|
|
|
vehicle: entity,
|
2024-04-05 18:01:44 +00:00
|
|
|
driver: player_entity,
|
2024-05-13 23:24:57 +00:00
|
|
|
name: actor.name.clone(),
|
2024-04-05 18:01:44 +00:00
|
|
|
is_entering: q_player_drives.is_empty(),
|
|
|
|
is_player: true,
|
|
|
|
});
|
2024-10-03 23:00:47 +00:00
|
|
|
if let Some(msg) = msg {
|
|
|
|
log.warning(msg.0.clone());
|
|
|
|
}
|
2024-03-28 15:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if keyboard_input.just_pressed(settings.key_vehicle) {
|
2024-03-28 15:02:36 +00:00
|
|
|
// Exiting Vehicles
|
2024-04-05 18:01:44 +00:00
|
|
|
for vehicle_entity in &q_player_drives {
|
2024-05-22 02:57:20 +00:00
|
|
|
commands
|
|
|
|
.entity(vehicle_entity)
|
|
|
|
.insert(ActorVehicleBeingEntered);
|
2024-04-05 18:01:44 +00:00
|
|
|
commands.entity(player_entity).insert(ActorEnteringVehicle);
|
2024-05-22 02:57:20 +00:00
|
|
|
ew_vehicle.send(VehicleEnterExitEvent {
|
2024-04-05 18:01:44 +00:00
|
|
|
vehicle: vehicle_entity,
|
|
|
|
driver: player_entity,
|
2024-05-13 23:24:57 +00:00
|
|
|
name: None,
|
2024-04-05 18:01:44 +00:00
|
|
|
is_entering: false,
|
|
|
|
is_player: true,
|
|
|
|
});
|
|
|
|
break;
|
2024-03-28 12:26:14 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if keyboard_input.just_pressed(settings.key_flashlight) {
|
2024-05-07 15:11:18 +00:00
|
|
|
for mut flashlight_vis in &mut q_flashlight {
|
2024-05-08 04:42:35 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
|
2024-05-07 15:11:18 +00:00
|
|
|
if *flashlight_vis == Visibility::Hidden {
|
|
|
|
*flashlight_vis = Visibility::Visible;
|
2024-05-07 18:54:24 +00:00
|
|
|
settings.flashlight_active = true;
|
2024-05-07 15:11:18 +00:00
|
|
|
} else {
|
|
|
|
*flashlight_vis = Visibility::Hidden;
|
2024-05-07 18:54:24 +00:00
|
|
|
settings.flashlight_active = false;
|
2024-05-07 15:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if keyboard_input.just_pressed(settings.key_cruise_control) {
|
2024-05-15 02:58:55 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
|
|
|
|
settings.cruise_control_active ^= true;
|
|
|
|
}
|
2024-03-19 00:24:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 15:02:36 +00:00
|
|
|
pub fn handle_vehicle_enter_exit(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut er_vehicle: EventReader<VehicleEnterExitEvent>,
|
2024-05-13 23:24:57 +00:00
|
|
|
mut ew_achievement: EventWriter<game::AchievementEvent>,
|
2024-05-22 02:57:20 +00:00
|
|
|
mut q_playerflashlight: Query<
|
2024-07-14 19:07:00 +00:00
|
|
|
&mut Transform,
|
2024-05-22 02:57:20 +00:00
|
|
|
(
|
|
|
|
With<PlayersFlashLight>,
|
|
|
|
Without<ActorVehicleBeingEntered>,
|
|
|
|
Without<ActorEnteringVehicle>,
|
|
|
|
),
|
|
|
|
>,
|
|
|
|
mut q_drivers: Query<
|
|
|
|
(Entity, &mut Visibility, Option<&Collider>),
|
|
|
|
(
|
|
|
|
Without<ActorVehicleBeingEntered>,
|
|
|
|
With<ActorEnteringVehicle>,
|
|
|
|
),
|
|
|
|
>,
|
|
|
|
mut q_vehicles: Query<
|
|
|
|
(Entity, &mut Vehicle, &mut Visibility),
|
|
|
|
(
|
|
|
|
With<ActorVehicleBeingEntered>,
|
|
|
|
Without<ActorEnteringVehicle>,
|
|
|
|
),
|
|
|
|
>,
|
2024-03-28 15:02:36 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
|
|
|
) {
|
|
|
|
for event in er_vehicle.read() {
|
2024-03-30 21:31:07 +00:00
|
|
|
for (driver, mut driver_vis, driver_collider) in q_drivers.iter_mut() {
|
2024-03-28 15:02:36 +00:00
|
|
|
if driver == event.driver {
|
2024-03-30 21:31:07 +00:00
|
|
|
for (vehicle, mut vehicle_component, mut vehicle_vis) in q_vehicles.iter_mut() {
|
|
|
|
if !event.is_player {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-28 15:02:36 +00:00
|
|
|
if vehicle == event.vehicle {
|
|
|
|
if event.is_entering {
|
|
|
|
// Entering Vehicle
|
2024-03-30 21:31:07 +00:00
|
|
|
if let Some(collider) = driver_collider {
|
|
|
|
vehicle_component.stored_drivers_collider = Some(collider.clone());
|
|
|
|
}
|
2024-03-30 17:48:19 +00:00
|
|
|
commands.entity(driver).remove::<RigidBody>();
|
2024-03-30 17:56:24 +00:00
|
|
|
*driver_vis = Visibility::Hidden; //seems to have no effect...
|
2024-03-30 21:31:07 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle));
|
|
|
|
commands.entity(driver).remove::<PlayerCamera>();
|
2024-11-16 23:02:00 +00:00
|
|
|
commands.entity(driver).remove::<WantsRotationChange>();
|
2024-03-30 21:31:07 +00:00
|
|
|
commands.entity(driver).remove::<Collider>();
|
2024-04-17 11:55:39 +00:00
|
|
|
commands.entity(driver).insert(JustNowEnteredVehicle);
|
2024-11-16 21:29:19 +00:00
|
|
|
commands
|
|
|
|
.entity(vehicle)
|
|
|
|
.insert(WantsAcceleration::default());
|
2024-06-16 21:58:41 +00:00
|
|
|
commands.entity(vehicle).remove::<hud::IsTargeted>();
|
2024-03-30 21:31:07 +00:00
|
|
|
commands.entity(vehicle).insert(PlayerCamera);
|
|
|
|
commands.entity(vehicle).insert(PlayerDrivesThis);
|
2024-07-14 19:07:00 +00:00
|
|
|
if let Ok(mut flashlight_trans) = q_playerflashlight.get_single_mut() {
|
|
|
|
flashlight_trans.rotation = Quat::from_rotation_y(0f32);
|
|
|
|
flashlight_trans.translation = Vec3::new(0.0, 0.0, 0.0);
|
2024-05-07 17:13:49 +00:00
|
|
|
}
|
2024-05-13 23:24:57 +00:00
|
|
|
if let Some(vehicle_name) = &event.name {
|
2024-05-22 02:57:20 +00:00
|
|
|
ew_achievement.send(game::AchievementEvent::RideVehicle(
|
|
|
|
vehicle_name.clone(),
|
|
|
|
));
|
2024-05-13 23:24:57 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-03-28 15:02:36 +00:00
|
|
|
// Exiting Vehicle
|
2024-03-30 21:31:07 +00:00
|
|
|
if let Some(collider) = &vehicle_component.stored_drivers_collider {
|
|
|
|
commands.entity(driver).insert(collider.clone());
|
2024-03-28 15:02:36 +00:00
|
|
|
}
|
2024-07-14 19:07:00 +00:00
|
|
|
if let Ok(mut flashlight_trans) = q_playerflashlight.get_single_mut() {
|
2024-07-14 19:16:19 +00:00
|
|
|
flashlight_trans.rotation =
|
|
|
|
Quat::from_rotation_y(180f32.to_radians());
|
2024-07-14 19:07:00 +00:00
|
|
|
flashlight_trans.translation = Vec3::new(0.0, 0.0, 1.0);
|
|
|
|
}
|
2024-03-30 21:31:07 +00:00
|
|
|
commands.entity(driver).insert(RigidBody::Dynamic);
|
2024-09-15 02:30:09 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::ExitVehicle));
|
2024-03-30 21:31:07 +00:00
|
|
|
commands.entity(vehicle).remove::<PlayerCamera>();
|
|
|
|
commands.entity(driver).insert(PlayerCamera);
|
|
|
|
commands.entity(vehicle).remove::<PlayerDrivesThis>();
|
2024-04-03 10:27:43 +00:00
|
|
|
*vehicle_vis = Visibility::Visible;
|
2024-03-28 15:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 15:58:42 +00:00
|
|
|
fn handle_collisions(
|
2024-03-31 03:13:21 +00:00
|
|
|
mut collision_event_reader: EventReader<CollisionStarted>,
|
2024-03-29 15:58:42 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-05-07 17:07:28 +00:00
|
|
|
q_player: Query<Entity, With<PlayerCollider>>,
|
2024-04-05 23:11:11 +00:00
|
|
|
mut q_player_lifeform: Query<(&mut LifeForm, &mut Suit), With<Player>>,
|
2024-03-29 15:58:42 +00:00
|
|
|
) {
|
2024-05-22 02:57:20 +00:00
|
|
|
if let (Ok(player), Ok((mut lifeform, mut suit))) =
|
|
|
|
(q_player.get_single(), q_player_lifeform.get_single_mut())
|
|
|
|
{
|
2024-03-31 03:13:21 +00:00
|
|
|
for CollisionStarted(entity1, entity2) in collision_event_reader.read() {
|
|
|
|
if *entity1 == player || *entity2 == player {
|
2024-03-29 15:58:42 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Crash));
|
2024-04-01 04:24:29 +00:00
|
|
|
lifeform.adrenaline_jolt += 0.1;
|
2024-05-07 22:32:20 +00:00
|
|
|
suit.integrity = (suit.integrity - 0.03).max(0.0);
|
2024-03-29 15:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 22:54:58 +00:00
|
|
|
|
2024-04-04 23:42:50 +00:00
|
|
|
fn handle_wants_maxrotation(
|
2024-04-04 23:19:46 +00:00
|
|
|
//time: Res<Time>,
|
2024-04-04 23:42:50 +00:00
|
|
|
mut query: Query<(&mut AngularVelocity, &Engine, &WantsMaxRotation)>,
|
2024-04-04 22:54:58 +00:00
|
|
|
) {
|
2024-04-04 23:19:46 +00:00
|
|
|
//let d = time.delta_seconds();
|
2024-04-04 23:42:50 +00:00
|
|
|
for (mut v_ang, engine, maxrot) in &mut query {
|
2024-04-04 22:54:58 +00:00
|
|
|
let total = v_ang.0.length();
|
2024-05-12 22:48:41 +00:00
|
|
|
if total <= maxrot.0 + EPSILON {
|
2024-04-04 23:42:50 +00:00
|
|
|
if total > maxrot.0 {
|
2024-04-04 22:54:58 +00:00
|
|
|
v_ang.0 = DVec3::splat(0.0);
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
|
|
|
let angular_slowdown: f64 =
|
2024-05-22 21:29:54 +00:00
|
|
|
(2.0 - engine.reaction_wheels.powf(0.05).clamp(1.001, 1.1)) as f64;
|
2024-04-04 22:54:58 +00:00
|
|
|
v_ang.0 *= angular_slowdown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 23:19:46 +00:00
|
|
|
|
2024-06-10 22:05:03 +00:00
|
|
|
/// Slows down NPC's movement until they reach their target velocity.
|
2024-04-04 23:42:50 +00:00
|
|
|
fn handle_wants_maxvelocity(
|
2024-04-04 23:19:46 +00:00
|
|
|
time: Res<Time>,
|
2024-06-10 22:50:25 +00:00
|
|
|
mut query: Query<(
|
|
|
|
&Position,
|
|
|
|
&mut LinearVelocity,
|
|
|
|
&Engine,
|
|
|
|
&WantsMaxVelocity,
|
|
|
|
Option<&OrbitsJupiter>,
|
2024-06-11 00:46:31 +00:00
|
|
|
Option<&WantsMatchVelocityWith>,
|
2024-06-10 22:50:25 +00:00
|
|
|
)>,
|
2024-06-11 00:46:31 +00:00
|
|
|
id2v: Res<game::Id2V>,
|
2024-06-10 22:58:56 +00:00
|
|
|
jupiter_pos: Res<game::JupiterPos>,
|
2024-04-04 23:19:46 +00:00
|
|
|
) {
|
|
|
|
let dt = time.delta_seconds();
|
2024-06-11 00:46:31 +00:00
|
|
|
for (pos, mut v, engine, maxv, orbits_jupiter, matchwith) in &mut query {
|
|
|
|
let target_velocity = if let Some(matchwith) = matchwith {
|
|
|
|
if let Some(target_v) = id2v.0.get(&matchwith.0) {
|
|
|
|
*target_v
|
|
|
|
} else {
|
|
|
|
warn!("Can't match velocity with nonexisting ID {}", matchwith.0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if orbits_jupiter.is_some() {
|
2024-06-10 22:58:56 +00:00
|
|
|
let relative_pos = pos.0 - jupiter_pos.0;
|
2024-06-10 22:50:25 +00:00
|
|
|
nature::orbital_velocity(relative_pos, nature::JUPITER_MASS)
|
|
|
|
} else {
|
|
|
|
DVec3::ZERO
|
|
|
|
};
|
2024-06-10 22:05:03 +00:00
|
|
|
let relative_velocity = v.0 - target_velocity;
|
|
|
|
let relative_speed = relative_velocity.length();
|
|
|
|
|
|
|
|
if relative_speed <= maxv.0 + EPSILON {
|
|
|
|
// it's already pretty close to the target
|
|
|
|
if relative_speed > maxv.0 {
|
|
|
|
// but not quite the target, so let's set it to the target
|
|
|
|
v.0 = target_velocity;
|
2024-04-04 23:19:46 +00:00
|
|
|
}
|
2024-05-22 21:54:11 +00:00
|
|
|
} else {
|
2024-06-10 22:05:03 +00:00
|
|
|
// slow it down a little bit
|
2024-05-22 21:54:11 +00:00
|
|
|
// TODO: respect engine parameters for different thrusts for different directions
|
|
|
|
let avg_thrust =
|
|
|
|
(engine.thrust_forward + engine.thrust_back + engine.thrust_sideways) / 3.0;
|
2024-06-10 22:05:03 +00:00
|
|
|
let acceleration = (avg_thrust * dt) as f64 * -relative_velocity;
|
2024-05-22 21:54:11 +00:00
|
|
|
v.0 += acceleration;
|
|
|
|
if v.0.length() + EPSILON < acceleration.length() {
|
2024-06-10 22:05:03 +00:00
|
|
|
v.0 = target_velocity;
|
2024-05-22 21:54:11 +00:00
|
|
|
}
|
2024-04-04 23:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-05 00:58:02 +00:00
|
|
|
|
2024-11-16 23:02:00 +00:00
|
|
|
fn handle_wants_rotation_change(mut q_actor: Query<(&mut Rotation, &mut WantsRotationChange)>) {
|
|
|
|
for (mut rot, mut change_rot) in &mut q_actor {
|
|
|
|
if change_rot.0 == Vec3::ZERO {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let actual_change_rot = change_rot.0.clamp_length_max(0.10);
|
|
|
|
for axis in 0..3 {
|
|
|
|
let original = change_rot.0[axis];
|
|
|
|
change_rot.0[axis] = 0.90 * change_rot.0[axis] - actual_change_rot[axis];
|
|
|
|
if original.signum() != change_rot.0[axis].signum() {
|
|
|
|
change_rot.0[axis] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let change = DQuat::from_euler(
|
|
|
|
EulerRot::XYZ,
|
|
|
|
actual_change_rot[0] as f64,
|
|
|
|
actual_change_rot[1] as f64,
|
|
|
|
actual_change_rot[2] as f64,
|
|
|
|
);
|
|
|
|
**rot = **rot * change;
|
2024-11-16 18:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 20:49:44 +00:00
|
|
|
fn handle_wants_acceleration(
|
2024-11-16 21:29:19 +00:00
|
|
|
time: Res<Time>,
|
|
|
|
settings: Res<var::Settings>,
|
2024-11-16 20:49:44 +00:00
|
|
|
jupiter_pos: Res<game::JupiterPos>,
|
2024-11-16 21:29:19 +00:00
|
|
|
q_audiosinks: Query<(&audio::Sfx, &AudioSink)>,
|
2024-11-16 22:12:47 +00:00
|
|
|
mut q_actor: Query<
|
|
|
|
(
|
|
|
|
Entity,
|
|
|
|
&Transform,
|
|
|
|
&Position,
|
|
|
|
&mut LinearVelocity,
|
|
|
|
Option<&mut Engine>,
|
|
|
|
Option<&WantsAcceleration>,
|
|
|
|
Option<&hud::IsTargeted>,
|
|
|
|
Option<&PlayerCamera>,
|
|
|
|
),
|
|
|
|
Without<visual::IsEffect>,
|
|
|
|
>,
|
2024-11-16 21:59:01 +00:00
|
|
|
mut ew_effect: EventWriter<visual::SpawnEffectEvent>,
|
2024-11-16 20:49:44 +00:00
|
|
|
) {
|
2024-11-16 21:29:19 +00:00
|
|
|
let dt = time.delta_seconds();
|
|
|
|
|
2024-11-16 20:49:44 +00:00
|
|
|
// Vector elements: (Entity, is_player, pos)
|
|
|
|
let mut request_closest: Vec<(Entity, bool, DVec3)> = vec![];
|
|
|
|
let mut closest_map: HashMap<Entity, DVec3> = HashMap::new();
|
|
|
|
|
|
|
|
// First, determine whether any actor wants to brake (=match velocity)
|
2024-11-16 21:29:19 +00:00
|
|
|
for (entity, _, pos, _, _, accel, _, is_player) in &mut q_actor {
|
2024-11-16 20:49:44 +00:00
|
|
|
if accel.is_some() && accel.unwrap().brake {
|
|
|
|
request_closest.push((entity, is_player.is_some(), pos.0.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an actor is braking, find out relative to what it wants to brake
|
|
|
|
for (entity, is_player, pos) in &request_closest {
|
|
|
|
let mut target_v: Option<DVec3> = None;
|
|
|
|
|
|
|
|
// First, if this is the player, check whether they targeted anything
|
|
|
|
// so we can match velocity to the target.
|
|
|
|
if *is_player {
|
2024-11-16 21:29:19 +00:00
|
|
|
for (_, _, _, v, _, _, is_target, _) in &q_actor {
|
2024-11-16 20:49:44 +00:00
|
|
|
if is_target.is_some() {
|
|
|
|
target_v = Some(v.0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, simply look for the closest object and match velocity to that.
|
|
|
|
if target_v.is_none() {
|
|
|
|
let mut closest_distance = camera::MAX_DIST_FOR_MATCH_VELOCITY;
|
2024-11-16 21:29:19 +00:00
|
|
|
for (testentity, _, testpos, v, _, _, _, _) in &q_actor {
|
2024-11-16 20:49:44 +00:00
|
|
|
if *entity != testentity {
|
|
|
|
let distance = (*pos - testpos.0).length();
|
|
|
|
if distance < closest_distance {
|
|
|
|
target_v = Some(v.0);
|
|
|
|
closest_distance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last resort: Match velocity to the orbital velocity around Jupiter
|
|
|
|
if target_v.is_none() {
|
|
|
|
let relative_pos = *pos - jupiter_pos.0;
|
|
|
|
target_v = Some(nature::orbital_velocity(relative_pos, nature::JUPITER_MASS));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(target_v) = target_v {
|
|
|
|
closest_map.insert(*entity, target_v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 21:29:19 +00:00
|
|
|
// Finally, apply the requested acceleration to the actor's velocity
|
|
|
|
let mut play_thruster_sound = false;
|
|
|
|
let mut players_engine: Option<Engine> = None;
|
2024-11-16 21:59:01 +00:00
|
|
|
for (entity, trans, pos, mut v, engine, accel, _, is_player) in &mut q_actor {
|
2024-11-16 21:29:19 +00:00
|
|
|
let mut thruster_on = false;
|
2024-11-16 21:59:01 +00:00
|
|
|
if let (Some(mut engine), Some(accel)) = (engine, accel) {
|
|
|
|
let mut delta_v = DVec3::ZERO;
|
|
|
|
let boost = engine.current_boost_factor;
|
2024-11-16 23:14:45 +00:00
|
|
|
engine.currently_matching_velocity = false;
|
2024-11-16 21:59:01 +00:00
|
|
|
|
2024-11-16 20:49:44 +00:00
|
|
|
if accel.brake {
|
|
|
|
if let Some(target_v) = closest_map.get(&entity) {
|
2024-11-16 23:14:45 +00:00
|
|
|
delta_v = *target_v - v.0;
|
|
|
|
if delta_v.length_squared() > 0.003 {
|
|
|
|
engine.currently_matching_velocity = true;
|
|
|
|
thruster_on = true;
|
|
|
|
}
|
2024-11-16 20:49:44 +00:00
|
|
|
}
|
2024-11-16 23:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if accel.direction != DVec3::ZERO {
|
|
|
|
delta_v += (trans.rotation * accel.direction.as_vec3()).as_dvec3();
|
2024-11-16 21:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if delta_v.length_squared() > 0.003 {
|
|
|
|
// Engine is firing!
|
2024-11-16 21:29:19 +00:00
|
|
|
thruster_on = true;
|
2024-11-16 21:59:01 +00:00
|
|
|
engine.current_warmup =
|
|
|
|
(engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
|
|
|
|
|
|
|
// Adjust acceleration to what the engine can actually provide
|
2024-11-16 22:12:47 +00:00
|
|
|
let factor_forward = if accel.direction.z > 0.0 {
|
|
|
|
engine.thrust_forward
|
|
|
|
} else {
|
|
|
|
engine.thrust_back
|
|
|
|
};
|
|
|
|
let factor_right = engine.thrust_sideways;
|
|
|
|
let factor_up = engine.thrust_sideways;
|
|
|
|
let engine_factor = Vec3::new(factor_forward, factor_up, factor_right).as_dvec3()
|
|
|
|
* engine.current_warmup as f64
|
|
|
|
* ENGINE_SPEED_FACTOR as f64
|
|
|
|
* engine.current_boost_factor;
|
2024-11-16 21:59:01 +00:00
|
|
|
|
|
|
|
// Apply acceleration to velocity
|
2024-11-16 23:14:45 +00:00
|
|
|
**v += delta_v.normalize() * engine_factor * dt as f64;
|
2024-11-16 21:59:01 +00:00
|
|
|
|
|
|
|
// Visual effect
|
|
|
|
if engine.engine_type == EngineType::Monopropellant {
|
|
|
|
let thruster_direction = delta_v.normalize();
|
|
|
|
let thruster_pos = pos.0 - 0.3 * thruster_direction;
|
|
|
|
let thruster_v = v.0 - boost * 5.0 * thruster_direction;
|
|
|
|
ew_effect.send(visual::SpawnEffectEvent {
|
|
|
|
duration: 2.0,
|
|
|
|
class: visual::Effects::ThrusterParticle(
|
|
|
|
Position::from(thruster_pos),
|
|
|
|
LinearVelocity::from(thruster_v),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Engine is not firing
|
|
|
|
engine.current_warmup =
|
|
|
|
(engine.current_warmup - dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
2024-11-16 21:29:19 +00:00
|
|
|
}
|
2024-11-16 21:59:01 +00:00
|
|
|
|
2024-11-16 21:29:19 +00:00
|
|
|
if is_player.is_some() {
|
|
|
|
play_thruster_sound = thruster_on;
|
|
|
|
players_engine = Some((*engine).clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play sound effects for player acceleration
|
|
|
|
let engine = if let Some(engine) = players_engine {
|
|
|
|
engine
|
|
|
|
} else {
|
|
|
|
warn!("Failed to retrieve player's engine type for playing SFX");
|
|
|
|
Engine::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut sinks: HashMap<audio::Sfx, &AudioSink> = HashMap::new();
|
|
|
|
for (sfx, sink) in &q_audiosinks {
|
|
|
|
sinks.insert(*sfx, sink);
|
|
|
|
}
|
|
|
|
let sinks = vec![
|
|
|
|
(
|
|
|
|
1.0,
|
2024-11-16 21:59:01 +00:00
|
|
|
engine.current_boost_factor as f32,
|
2024-11-16 21:29:19 +00:00
|
|
|
actor::EngineType::Monopropellant,
|
|
|
|
sinks.get(&audio::Sfx::Thruster),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
1.0,
|
|
|
|
1.0,
|
|
|
|
actor::EngineType::Ion,
|
|
|
|
sinks.get(&audio::Sfx::Ion),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
let seconds_to_max_vol = 0.05;
|
|
|
|
let seconds_to_min_vol = 0.05;
|
|
|
|
for sink_data in sinks {
|
|
|
|
if let (vol_boost, speed, engine_type, Some(sink)) = sink_data {
|
|
|
|
if settings.mute_sfx {
|
|
|
|
sink.pause();
|
|
|
|
} else {
|
|
|
|
let volume = sink.volume();
|
|
|
|
let maxvol = settings.volume_sfx * vol_boost;
|
|
|
|
if engine.engine_type == engine_type {
|
|
|
|
if play_thruster_sound {
|
|
|
|
sink.set_speed(speed);
|
|
|
|
sink.play();
|
|
|
|
if volume < maxvol {
|
2024-11-16 21:59:01 +00:00
|
|
|
sink.set_volume((volume + dt / seconds_to_max_vol).clamp(0.0, maxvol));
|
2024-11-16 21:29:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, maxvol));
|
|
|
|
}
|
|
|
|
} else if volume > 0.0 {
|
|
|
|
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, maxvol));
|
|
|
|
}
|
|
|
|
if volume < 0.0001 {
|
|
|
|
sink.pause();
|
|
|
|
}
|
2024-11-16 20:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 22:31:31 +00:00
|
|
|
fn handle_wants_lookat(
|
2024-10-05 02:26:44 +00:00
|
|
|
mut query: Query<
|
|
|
|
(
|
|
|
|
&Position,
|
|
|
|
&mut Rotation,
|
|
|
|
&Transform,
|
|
|
|
&WantsToLookAt,
|
|
|
|
Option<&visual::IsEffect>,
|
|
|
|
),
|
|
|
|
Without<Camera>,
|
|
|
|
>,
|
2024-05-22 22:56:12 +00:00
|
|
|
q_playercam: Query<&Position, With<PlayerCamera>>,
|
2024-09-15 21:51:03 +00:00
|
|
|
q_cam: Query<&Transform, With<Camera>>,
|
2024-05-22 22:31:31 +00:00
|
|
|
id2pos: Res<game::Id2Pos>,
|
|
|
|
) {
|
2024-05-22 22:56:12 +00:00
|
|
|
let player_pos = if let Ok(player_pos) = q_playercam.get_single() {
|
|
|
|
player_pos
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2024-09-15 21:51:03 +00:00
|
|
|
let cam_pos = if let Ok(cam_trans) = q_cam.get_single() {
|
|
|
|
cam_trans.translation.as_dvec3() + player_pos.0
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2024-05-22 22:56:12 +00:00
|
|
|
|
2024-05-22 23:26:24 +00:00
|
|
|
// TODO: use ExternalTorque rather than hard-resetting the rotation
|
2024-10-05 02:26:44 +00:00
|
|
|
for (pos, mut rot, trans, target_id, is_effect) in &mut query {
|
2024-09-15 21:51:03 +00:00
|
|
|
let target_pos: DVec3 = if target_id.0 == cmd::ID_SPECIAL_PLAYERCAM {
|
|
|
|
cam_pos
|
2024-05-22 22:56:12 +00:00
|
|
|
} else if let Some(target_pos) = id2pos.0.get(&target_id.0) {
|
2024-09-15 21:51:03 +00:00
|
|
|
*target_pos
|
2024-05-22 22:56:12 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
2024-10-05 02:26:44 +00:00
|
|
|
let up = if is_effect.is_some() {
|
|
|
|
// trans.up() sometimes crashes with thruster particle effects
|
|
|
|
Dir3::Y
|
|
|
|
} else {
|
|
|
|
if trans.translation.length_squared() > 1e-6 {
|
|
|
|
trans.up()
|
|
|
|
} else {
|
|
|
|
Dir3::Y
|
|
|
|
}
|
|
|
|
};
|
2024-09-16 02:07:36 +00:00
|
|
|
rot.0 = look_at_quat(**pos, target_pos, up.as_dvec3());
|
2024-05-22 22:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 01:31:52 +00:00
|
|
|
fn handle_damage(
|
2024-05-12 21:57:21 +00:00
|
|
|
mut ew_playerdies: EventWriter<game::PlayerDiesEvent>,
|
2024-04-05 01:31:52 +00:00
|
|
|
mut q_hp: Query<(&mut HitPoints, Option<&Player>), Changed<HitPoints>>,
|
2024-05-12 20:17:17 +00:00
|
|
|
settings: Res<Settings>,
|
2024-04-05 01:31:52 +00:00
|
|
|
) {
|
|
|
|
for (mut hp, player_maybe) in &mut q_hp {
|
|
|
|
if player_maybe.is_some() {
|
2024-04-07 23:44:36 +00:00
|
|
|
if !settings.god_mode {
|
|
|
|
hp.current -= hp.damage;
|
|
|
|
}
|
2024-04-05 01:31:52 +00:00
|
|
|
if hp.current <= 0.0 {
|
2024-05-12 21:57:21 +00:00
|
|
|
ew_playerdies.send(game::PlayerDiesEvent(hp.damagetype));
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-04-07 23:44:36 +00:00
|
|
|
hp.current -= hp.damage;
|
|
|
|
}
|
|
|
|
hp.damage = 0.0;
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 23:11:11 +00:00
|
|
|
|
|
|
|
fn handle_gforce(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut q_actor: Query<(&LinearVelocity, &mut HitPoints, &mut ExperiencesGForce)>,
|
|
|
|
) {
|
|
|
|
let dt = time.delta_seconds();
|
2024-04-07 23:53:56 +00:00
|
|
|
let factor = 1.0 / dt / nature::EARTH_GRAVITY;
|
2024-04-05 23:11:11 +00:00
|
|
|
for (v, mut hp, mut gforce) in &mut q_actor {
|
2024-06-11 01:29:26 +00:00
|
|
|
gforce.gforce = factor
|
|
|
|
* (v.0 - gforce.last_linear_velocity - gforce.gravitational_component).length() as f32;
|
2024-04-07 23:53:56 +00:00
|
|
|
gforce.last_linear_velocity = v.0;
|
2024-06-11 01:29:26 +00:00
|
|
|
gforce.gravitational_component = DVec3::ZERO;
|
2024-04-05 23:36:14 +00:00
|
|
|
if gforce.ignore_gforce_seconds > 0.0 {
|
|
|
|
gforce.ignore_gforce_seconds -= dt;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-05 23:11:11 +00:00
|
|
|
if gforce.gforce > gforce.damage_threshold {
|
|
|
|
hp.damage += (gforce.gforce - gforce.damage_threshold).powf(2.0) / 3000.0;
|
2024-05-12 20:30:53 +00:00
|
|
|
hp.damagetype = DamageType::GForce;
|
2024-04-05 23:11:11 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 00:36:47 +00:00
|
|
|
if gforce.visual_effect > 0.0001 {
|
|
|
|
gforce.visual_effect *= 0.984;
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if gforce.visual_effect > 0.0 {
|
2024-04-08 00:36:47 +00:00
|
|
|
gforce.visual_effect = 0.0;
|
|
|
|
}
|
|
|
|
if gforce.gforce > gforce.visual_effect_threshold {
|
2024-05-22 02:57:20 +00:00
|
|
|
gforce.visual_effect +=
|
|
|
|
(gforce.gforce - gforce.visual_effect_threshold).powf(2.0) / 300000.0
|
2024-04-08 00:36:47 +00:00
|
|
|
}
|
2024-04-05 23:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-08 02:17:28 +00:00
|
|
|
|
|
|
|
fn handle_gravity(
|
|
|
|
time: Res<Time>,
|
2024-06-11 01:29:26 +00:00
|
|
|
mut q_pos: Query<
|
|
|
|
(
|
|
|
|
&Position,
|
|
|
|
&mut LinearVelocity,
|
|
|
|
Option<&mut ExperiencesGForce>,
|
|
|
|
),
|
|
|
|
With<OrbitsJupiter>,
|
|
|
|
>,
|
2024-06-10 22:58:56 +00:00
|
|
|
jupiter_pos: Res<game::JupiterPos>,
|
2024-06-08 02:17:28 +00:00
|
|
|
) {
|
2024-06-11 01:29:26 +00:00
|
|
|
let dt = time.delta_seconds() as f64;
|
2024-06-08 02:17:28 +00:00
|
|
|
|
|
|
|
// this assumes prograde orbits for every object
|
2024-06-11 01:29:26 +00:00
|
|
|
for (pos, mut v, gforce_maybe) in &mut q_pos {
|
2024-06-10 22:58:56 +00:00
|
|
|
let relative_pos = pos.0 - jupiter_pos.0;
|
2024-06-11 01:29:26 +00:00
|
|
|
let accel = dt * nature::gravitational_acceleration(relative_pos, nature::JUPITER_MASS);
|
|
|
|
if let Some(mut gforce) = gforce_maybe {
|
|
|
|
gforce.gravitational_component += accel;
|
|
|
|
}
|
2024-06-08 02:17:28 +00:00
|
|
|
v.0 += accel;
|
|
|
|
}
|
|
|
|
}
|