2024-04-01 18:52:57 +00:00
|
|
|
use crate::{actor, nature, settings, hud};
|
2024-03-16 20:44:51 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-31 22:47:03 +00:00
|
|
|
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
|
2024-04-01 14:29:14 +00:00
|
|
|
use bevy::math::DVec3;
|
2024-03-29 15:33:12 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-04-01 15:19:43 +00:00
|
|
|
use bevy_xpbd_3d::plugins::sync::SyncConfig;
|
2024-03-16 22:37:18 +00:00
|
|
|
use std::f32::consts::PI;
|
2024-04-01 23:07:22 +00:00
|
|
|
use std::f64::consts::PI as PI64;
|
2024-03-16 20:44:51 +00:00
|
|
|
|
2024-04-01 18:52:57 +00:00
|
|
|
const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds
|
2024-03-18 03:39:26 +00:00
|
|
|
const ASTEROID_SIZE: f32 = 100.0;
|
2024-03-27 15:47:05 +00:00
|
|
|
const STARS_MAX_MAGNITUDE: f32 = 5.5;
|
2024-03-18 03:39:26 +00:00
|
|
|
|
2024-04-01 17:20:31 +00:00
|
|
|
const CENTER_WORLD_ON_PLAYER: bool = true;
|
2024-04-01 21:54:56 +00:00
|
|
|
const ASTEROIDS_ARE_SPHERES: bool = false;
|
2024-04-01 17:20:31 +00:00
|
|
|
|
2024-03-19 20:09:20 +00:00
|
|
|
const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0";
|
|
|
|
const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0";
|
2024-03-31 20:00:34 +00:00
|
|
|
pub fn asset_name_to_path(name: &str) -> &'static str {
|
2024-03-20 19:37:35 +00:00
|
|
|
match name {
|
2024-03-21 01:51:07 +00:00
|
|
|
"suit" => "models/suit.glb#Scene0",
|
2024-03-20 20:03:22 +00:00
|
|
|
"asteroid1" => ASSET_ASTEROID1,
|
|
|
|
"asteroid2" => ASSET_ASTEROID2,
|
2024-03-21 02:15:00 +00:00
|
|
|
"moonlet" => "models/moonlet.glb#Scene0",
|
2024-03-29 13:19:42 +00:00
|
|
|
"monolith" => "models/monolith_neon.glb#Scene0",
|
2024-03-31 02:10:54 +00:00
|
|
|
"lightorb" => "models/lightorb.glb#Scene0",
|
2024-03-28 16:25:35 +00:00
|
|
|
"MeteorAceGT" => "models/MeteorAceGT.glb#Scene0",
|
2024-03-21 03:34:09 +00:00
|
|
|
"pizzeria" => "models/pizzeria2.glb#Scene0",
|
|
|
|
"pizzasign" => "models/pizzasign.glb#Scene0",
|
2024-03-20 20:25:47 +00:00
|
|
|
_ => "models/error.glb#Scene0",
|
2024-03-20 19:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
|
2024-03-17 23:04:23 +00:00
|
|
|
pub struct WorldPlugin;
|
|
|
|
impl Plugin for WorldPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2024-03-31 20:00:34 +00:00
|
|
|
app.add_systems(Startup, setup);
|
2024-04-01 16:33:31 +00:00
|
|
|
app.add_systems(Startup, generate_asteroids);
|
2024-04-01 03:25:35 +00:00
|
|
|
app.add_systems(Update, handle_cheats);
|
2024-04-01 21:54:56 +00:00
|
|
|
app.add_systems(PostUpdate, handle_despawn);
|
2024-04-01 16:33:31 +00:00
|
|
|
app.add_systems(Update, spawn_despawn_asteroids);
|
2024-03-29 15:33:12 +00:00
|
|
|
app.add_plugins(PhysicsPlugins::default());
|
2024-04-01 00:05:38 +00:00
|
|
|
app.add_plugins(MaterialPlugin::<RingMaterial>::default());
|
2024-03-30 14:37:51 +00:00
|
|
|
//app.add_plugins(PhysicsDebugPlugin::default());
|
2024-04-01 14:29:14 +00:00
|
|
|
app.insert_resource(Gravity(DVec3::splat(0.0)));
|
2024-04-01 16:33:31 +00:00
|
|
|
app.insert_resource(AsteroidUpdateTimer(
|
|
|
|
Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating)));
|
|
|
|
app.insert_resource(AsteroidDatabase(Vec::new()));
|
2024-04-01 21:54:56 +00:00
|
|
|
app.add_event::<DespawnEvent>();
|
2024-04-01 15:19:43 +00:00
|
|
|
|
2024-04-01 17:20:31 +00:00
|
|
|
if CENTER_WORLD_ON_PLAYER {
|
|
|
|
// Disable bevy_xpbd's position->transform sync function
|
|
|
|
app.insert_resource(SyncConfig {
|
|
|
|
position_to_transform: false,
|
|
|
|
transform_to_position: false,
|
|
|
|
});
|
|
|
|
// Add own position->transform sync function
|
|
|
|
app.add_systems(PreUpdate, position_to_transform);
|
|
|
|
}
|
2024-03-17 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 16:33:31 +00:00
|
|
|
#[derive(Resource)] struct AsteroidUpdateTimer(Timer);
|
|
|
|
#[derive(Resource)] struct AsteroidDatabase(Vec<AsteroidData>);
|
|
|
|
|
2024-04-01 18:52:57 +00:00
|
|
|
#[derive(Component)] struct Asteroid;
|
|
|
|
|
2024-04-01 16:33:31 +00:00
|
|
|
struct AsteroidData {
|
2024-04-01 18:52:57 +00:00
|
|
|
entity: Option<Entity>,
|
2024-04-01 16:33:31 +00:00
|
|
|
is_spawned: bool,
|
2024-04-01 18:52:57 +00:00
|
|
|
spawned_once: bool,
|
2024-04-01 16:33:31 +00:00
|
|
|
class: u8,
|
|
|
|
size: f32,
|
2024-04-01 18:52:57 +00:00
|
|
|
pos: DVec3,
|
2024-04-01 16:33:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 21:54:56 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct DespawnEvent(Entity);
|
|
|
|
|
2024-03-31 22:47:03 +00:00
|
|
|
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
|
2024-04-01 00:05:38 +00:00
|
|
|
pub struct RingMaterial {
|
|
|
|
alpha_mode: AlphaMode,
|
|
|
|
#[uniform(0)]
|
|
|
|
ring_radius: f32,
|
|
|
|
#[uniform(1)]
|
|
|
|
jupiter_radius: f32,
|
|
|
|
}
|
2024-03-31 22:47:03 +00:00
|
|
|
|
2024-04-01 00:05:38 +00:00
|
|
|
impl Material for RingMaterial {
|
2024-03-31 22:47:03 +00:00
|
|
|
fn fragment_shader() -> ShaderRef {
|
2024-04-01 00:05:38 +00:00
|
|
|
"shaders/jupiters_rings.wgsl".into()
|
|
|
|
}
|
|
|
|
fn alpha_mode(&self) -> AlphaMode {
|
|
|
|
self.alpha_mode
|
2024-03-31 22:47:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 21:13:37 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Star;
|
|
|
|
|
2024-03-16 20:44:51 +00:00
|
|
|
pub fn setup(
|
|
|
|
mut commands: Commands,
|
2024-03-16 22:11:56 +00:00
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2024-04-01 00:05:38 +00:00
|
|
|
mut materials_custom: ResMut<Assets<RingMaterial>>,
|
2024-03-16 20:44:51 +00:00
|
|
|
) {
|
2024-03-18 21:13:37 +00:00
|
|
|
// Generate starmap
|
2024-03-20 20:03:22 +00:00
|
|
|
let sphere_handle = meshes.add(Sphere::new(1.0));
|
2024-03-21 02:20:44 +00:00
|
|
|
let mut starcount = 0;
|
2024-03-19 02:54:16 +00:00
|
|
|
for star in nature::STARS {
|
2024-03-21 01:11:07 +00:00
|
|
|
let mag = star[3];
|
2024-03-21 02:20:44 +00:00
|
|
|
if mag > STARS_MAX_MAGNITUDE {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-21 01:35:16 +00:00
|
|
|
let scale_color = {|color: f32|
|
2024-03-21 01:11:07 +00:00
|
|
|
if mag < -20.0 {
|
2024-03-21 01:35:16 +00:00
|
|
|
color * 13.0f32 // Sun
|
2024-03-21 01:11:07 +00:00
|
|
|
} else {
|
2024-03-21 01:35:16 +00:00
|
|
|
color * (0.0659663 * mag * mag - 1.09862 * mag + 4.3)
|
2024-03-21 01:11:07 +00:00
|
|
|
}
|
|
|
|
};
|
2024-03-21 01:35:16 +00:00
|
|
|
let scale_size = {|mag: f32|
|
2024-03-21 01:11:07 +00:00
|
|
|
if mag < -20.0 {
|
|
|
|
40000.0f32 // Sun
|
|
|
|
} else {
|
2024-03-21 01:35:16 +00:00
|
|
|
1000.0 * (0.230299 * mag * mag - 3.09013 * mag + 15.1782)
|
2024-04-01 23:07:22 +00:00
|
|
|
} * 100.0
|
2024-03-21 01:11:07 +00:00
|
|
|
};
|
2024-03-18 22:47:03 +00:00
|
|
|
let (r, g, b) = nature::star_color_index_to_rgb(star[4]);
|
2024-03-18 21:13:37 +00:00
|
|
|
let star_color_handle = materials.add(StandardMaterial {
|
2024-03-21 01:35:16 +00:00
|
|
|
base_color: Color::rgb(scale_color(r), scale_color(g), scale_color(b)),
|
2024-03-21 01:11:07 +00:00
|
|
|
unlit: true,
|
2024-03-18 21:13:37 +00:00
|
|
|
..default()
|
|
|
|
});
|
2024-04-01 23:07:22 +00:00
|
|
|
let dist = 1e9;
|
2024-03-18 21:13:37 +00:00
|
|
|
commands.spawn((
|
|
|
|
Star,
|
|
|
|
PbrBundle {
|
|
|
|
mesh: sphere_handle.clone(),
|
|
|
|
material: star_color_handle.clone(),
|
|
|
|
transform: Transform::from_xyz(
|
|
|
|
dist * star[0],
|
|
|
|
dist * star[1],
|
|
|
|
dist * star[2],
|
|
|
|
)
|
2024-03-21 01:35:16 +00:00
|
|
|
.with_scale(Vec3::splat(scale_size(mag))),
|
2024-03-18 21:13:37 +00:00
|
|
|
..default()
|
|
|
|
}
|
|
|
|
));
|
2024-03-21 02:20:44 +00:00
|
|
|
starcount += 1;
|
2024-03-18 21:13:37 +00:00
|
|
|
}
|
2024-03-21 02:20:44 +00:00
|
|
|
info!("Generated {starcount} stars");
|
2024-03-17 13:16:25 +00:00
|
|
|
|
2024-03-31 22:47:03 +00:00
|
|
|
// Add shaded ring
|
2024-04-01 23:07:22 +00:00
|
|
|
let ring_radius = 229_000_000.0;
|
|
|
|
let jupiter_radius = 71_492_000.0;
|
2024-04-01 15:19:43 +00:00
|
|
|
commands.spawn((
|
|
|
|
MaterialMeshBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(Cylinder::new(ring_radius, 1.0))),
|
|
|
|
material: materials_custom.add(RingMaterial {
|
|
|
|
alpha_mode: AlphaMode::Blend,
|
|
|
|
ring_radius: ring_radius,
|
|
|
|
jupiter_radius: jupiter_radius,
|
|
|
|
}),
|
|
|
|
..default()
|
|
|
|
},
|
2024-04-01 15:55:58 +00:00
|
|
|
Position::from_xyz(0.0, 0.0, 0.0),
|
2024-04-01 23:07:22 +00:00
|
|
|
//Rotation::from(Quat::IDENTITY),
|
|
|
|
Rotation::from(Quat::from_rotation_z(0.1f32.to_radians())),
|
2024-04-01 15:19:43 +00:00
|
|
|
));
|
2024-03-16 20:44:51 +00:00
|
|
|
}
|
2024-04-01 03:25:35 +00:00
|
|
|
|
2024-04-01 16:33:31 +00:00
|
|
|
fn generate_asteroids(
|
|
|
|
mut db: ResMut<AsteroidDatabase>,
|
|
|
|
) {
|
2024-04-01 19:07:28 +00:00
|
|
|
let maxdist = 30;
|
|
|
|
let maxdist_orthogonal = maxdist / 4;
|
2024-04-01 23:07:22 +00:00
|
|
|
let player_r: f64 = 150000e3; // sync this with defs.txt parameters
|
|
|
|
let player_phase: f64 = 0.66; // sync this with defs.txt parameters
|
|
|
|
let player_x: f64 = player_r * (player_phase * PI64 * 2.0).cos();
|
2024-04-01 16:33:31 +00:00
|
|
|
let player_y: f64 = 0.0;
|
2024-04-01 23:07:22 +00:00
|
|
|
let player_z: f64 = player_r * (player_phase * PI64 * 2.0).sin();
|
2024-04-01 19:07:28 +00:00
|
|
|
let offset: f64 = 500.0;
|
|
|
|
let dist: f64 = 8e3;
|
2024-04-01 16:33:31 +00:00
|
|
|
for i in -maxdist..maxdist {
|
2024-04-01 19:07:28 +00:00
|
|
|
for j in -maxdist_orthogonal..maxdist_orthogonal {
|
2024-04-01 16:33:31 +00:00
|
|
|
for k in -maxdist..maxdist {
|
|
|
|
let wobble: f64 = dist/2.0;
|
|
|
|
let (i, j, k) = (i as f64, j as f64, k as f64);
|
|
|
|
db.0.push(AsteroidData {
|
2024-04-01 18:52:57 +00:00
|
|
|
entity: None,
|
2024-04-01 16:33:31 +00:00
|
|
|
is_spawned: false,
|
2024-04-01 18:52:57 +00:00
|
|
|
spawned_once: false,
|
|
|
|
pos: DVec3::new(
|
2024-04-01 23:07:22 +00:00
|
|
|
player_x + offset + dist * i + wobble * (j+k/PI64).sin() * (k+j/PI64).cos(),
|
|
|
|
player_y + offset + dist * j + wobble * (k+i/PI64).sin() * (i+k/PI64).cos(),
|
|
|
|
player_z + offset + dist * k + wobble * (i+j/PI64).sin() * (j+i/PI64).cos(),
|
2024-04-01 18:52:57 +00:00
|
|
|
),
|
2024-04-01 16:33:31 +00:00
|
|
|
size: ASTEROID_SIZE,
|
|
|
|
class: (((i+j+k) as i32) % 2) as u8,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_despawn_asteroids(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut timer: ResMut<AsteroidUpdateTimer>,
|
|
|
|
mut db: ResMut<AsteroidDatabase>,
|
|
|
|
mut commands: Commands,
|
2024-04-01 18:52:57 +00:00
|
|
|
q_player: Query<&Position, With<actor::PlayerCamera>>,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut q_asteroid: Query<(Entity, &mut Visibility), With<Asteroid>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2024-04-01 16:33:31 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
2024-04-01 18:52:57 +00:00
|
|
|
mut log: ResMut<hud::Log>,
|
2024-04-01 21:54:56 +00:00
|
|
|
mut ew_despawn: EventWriter<DespawnEvent>,
|
2024-04-01 16:33:31 +00:00
|
|
|
) {
|
2024-04-01 18:52:57 +00:00
|
|
|
if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() {
|
2024-04-01 16:33:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-01 18:52:57 +00:00
|
|
|
let player = q_player.get_single().unwrap();
|
2024-04-01 16:33:31 +00:00
|
|
|
|
2024-04-01 18:52:57 +00:00
|
|
|
let mut spawned = 0;
|
|
|
|
let mut despawned = 0;
|
2024-04-01 16:33:31 +00:00
|
|
|
for asteroid in &mut db.0 {
|
2024-04-01 18:52:57 +00:00
|
|
|
let dist = player.distance(asteroid.pos);
|
2024-04-01 19:07:28 +00:00
|
|
|
let should_spawn = dist < 50000.0;
|
2024-04-01 18:52:57 +00:00
|
|
|
if should_spawn == asteroid.is_spawned {
|
|
|
|
continue; // Nothing to do
|
|
|
|
}
|
|
|
|
if !should_spawn {
|
|
|
|
// Despawn
|
|
|
|
if let Some(entity) = asteroid.entity {
|
|
|
|
for (ent, mut vis) in &mut q_asteroid {
|
|
|
|
if ent == entity {
|
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
asteroid.entity = None;
|
|
|
|
asteroid.is_spawned = false;
|
|
|
|
despawned += 1;
|
2024-04-01 21:54:56 +00:00
|
|
|
ew_despawn.send(DespawnEvent(entity));
|
2024-04-01 18:52:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-01 16:33:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-04-01 18:52:57 +00:00
|
|
|
|
|
|
|
// Spawn
|
|
|
|
let mut entity_commands = commands.spawn((
|
2024-04-01 16:33:31 +00:00
|
|
|
actor::Actor::default(),
|
|
|
|
RigidBody::Dynamic,
|
|
|
|
AngularVelocity(DVec3::new(0.1, 0.1, 0.03)),
|
|
|
|
LinearVelocity(DVec3::new(0.0, 0.0, 0.35)),
|
|
|
|
Collider::sphere(1.0),
|
2024-04-01 18:52:57 +00:00
|
|
|
Rotation::from(Quat::from_rotation_y(-PI / 3.)),
|
|
|
|
Position::new(asteroid.pos),
|
|
|
|
Asteroid,
|
|
|
|
));
|
|
|
|
if ASTEROIDS_ARE_SPHERES {
|
2024-04-01 19:07:28 +00:00
|
|
|
let sphere_handle = meshes.add(Sphere::default());
|
2024-04-01 18:52:57 +00:00
|
|
|
let sphere_material_handle = materials.add(StandardMaterial {
|
|
|
|
base_color: Color::rgb(0.4, 0.4, 0.4),
|
|
|
|
perceptual_roughness: 1.0,
|
|
|
|
metallic: 0.0,
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
entity_commands.insert(PbrBundle {
|
|
|
|
mesh: sphere_handle,
|
|
|
|
material: sphere_material_handle,
|
2024-04-01 16:33:31 +00:00
|
|
|
transform: Transform {
|
|
|
|
scale: Vec3::splat(asteroid.size),
|
|
|
|
..default()
|
|
|
|
},
|
2024-04-01 18:52:57 +00:00
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let asset = match asteroid.class {
|
|
|
|
0 => ASSET_ASTEROID1,
|
|
|
|
_ => ASSET_ASTEROID2,
|
|
|
|
};
|
|
|
|
entity_commands.insert(SceneBundle {
|
2024-04-01 16:33:31 +00:00
|
|
|
scene: asset_server.load(asset),
|
2024-04-01 18:52:57 +00:00
|
|
|
transform: Transform {
|
|
|
|
scale: Vec3::splat(asteroid.size),
|
|
|
|
..default()
|
|
|
|
},
|
2024-04-01 16:33:31 +00:00
|
|
|
..default()
|
2024-04-01 18:52:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
asteroid.entity = Some(entity_commands.id());
|
|
|
|
spawned += 1;
|
|
|
|
asteroid.spawned_once = true;
|
|
|
|
asteroid.is_spawned = true;
|
|
|
|
}
|
|
|
|
if spawned != 0 || despawned != 0 {
|
|
|
|
log.notice(format!("spawned: {spawned}, despawned: {despawned}"));
|
2024-04-01 16:33:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 21:54:56 +00:00
|
|
|
fn handle_despawn(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut er_despawn: EventReader<DespawnEvent>,
|
|
|
|
) {
|
|
|
|
for despawn in er_despawn.read() {
|
|
|
|
commands.entity(despawn.0).despawn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 03:25:35 +00:00
|
|
|
fn handle_cheats(
|
|
|
|
key_input: Res<ButtonInput<KeyCode>>,
|
2024-04-01 15:19:43 +00:00
|
|
|
mut q_player: Query<(&Transform, &mut Position, &mut LinearVelocity), With<actor::PlayerCamera>>,
|
2024-04-01 03:25:35 +00:00
|
|
|
mut q_life: Query<(&mut actor::LifeForm, ), With<actor::Player>>,
|
|
|
|
settings: ResMut<settings::Settings>,
|
|
|
|
) {
|
|
|
|
if !settings.dev_mode || q_player.is_empty() || q_life.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-01 15:19:43 +00:00
|
|
|
let (trans, mut pos, mut v) = q_player.get_single_mut().unwrap();
|
2024-04-01 03:25:35 +00:00
|
|
|
let (mut lifeform, ) = q_life.get_single_mut().unwrap();
|
2024-04-01 23:14:18 +00:00
|
|
|
let boost = if key_input.pressed(KeyCode::ShiftLeft) {
|
|
|
|
1e6
|
|
|
|
} else {
|
|
|
|
1e3
|
|
|
|
};
|
2024-04-01 03:25:35 +00:00
|
|
|
if key_input.just_pressed(settings.key_cheat_pizza) {
|
2024-04-01 18:01:11 +00:00
|
|
|
pos.0 = DVec3::new(-303370.0, 0.0, -500000.0);
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
|
|
|
if key_input.just_pressed(settings.key_cheat_farview1) {
|
2024-04-01 23:14:18 +00:00
|
|
|
pos.0 = DVec3::new(-1000.0e6, 1000.0e6, -500.0e6);
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
|
|
|
if key_input.just_pressed(settings.key_cheat_farview2) {
|
2024-04-01 23:14:18 +00:00
|
|
|
pos.0 = DVec3::new(1000.0e6, 1000.0e6, -500.0e6);
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
|
|
|
if key_input.just_pressed(settings.key_cheat_stop) {
|
2024-04-01 14:29:14 +00:00
|
|
|
v.0 = DVec3::ZERO;
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
|
|
|
if key_input.pressed(settings.key_cheat_speed) {
|
2024-04-01 23:14:18 +00:00
|
|
|
v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, boost));
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
2024-04-01 18:37:32 +00:00
|
|
|
if key_input.pressed(settings.key_cheat_speed_backward) {
|
2024-04-01 23:14:18 +00:00
|
|
|
v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, -boost));
|
2024-04-01 18:37:32 +00:00
|
|
|
}
|
2024-04-01 03:25:35 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-04-01 15:19:43 +00:00
|
|
|
|
|
|
|
// A variant 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<actor::PlayerCamera>>,
|
2024-04-01 18:38:28 +00:00
|
|
|
mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation, Option<&'static Parent>)>,
|
|
|
|
parents: Query<(&'static GlobalTransform, Option<&'static Position>, Option<&'static Rotation>), With<Children>>,
|
2024-04-01 15:19:43 +00:00
|
|
|
) {
|
|
|
|
if let Ok(player_pos) = q_player.get_single() {
|
2024-04-01 18:38:28 +00:00
|
|
|
for (mut transform, pos, rot, parent) in &mut q_trans {
|
|
|
|
if let Some(parent) = parent {
|
|
|
|
if let Ok((parent_transform, parent_pos, parent_rot)) = parents.get(**parent) {
|
|
|
|
// Compute the global transform of the parent using its Position and Rotation
|
|
|
|
let parent_transform = parent_transform.compute_transform();
|
|
|
|
let parent_pos = parent_pos.map_or(parent_transform.translation, |pos| {
|
|
|
|
pos.as_vec3()
|
|
|
|
// NOTE: I commented out this because it turns a vec3 to a vec4,
|
|
|
|
// and I don't understand why bevy_xpbd would do that.
|
|
|
|
//.extend(parent_transform.translation.z)
|
|
|
|
});
|
|
|
|
let parent_rot = parent_rot.map_or(parent_transform.rotation, |rot| {
|
|
|
|
rot.as_quat()
|
|
|
|
});
|
|
|
|
let parent_scale = parent_transform.scale;
|
|
|
|
let parent_transform = Transform::from_translation(parent_pos)
|
|
|
|
.with_rotation(parent_rot)
|
|
|
|
.with_scale(parent_scale);
|
|
|
|
|
|
|
|
// The new local transform of the child body,
|
|
|
|
// computed from the its global transform and its parents global transform
|
|
|
|
let new_transform = GlobalTransform::from(
|
|
|
|
Transform::from_translation(
|
|
|
|
pos.as_vec3()
|
|
|
|
// NOTE: I commented out this because it turns a vec3 to a vec4,
|
|
|
|
// and I don't understand why bevy_xpbd would do that.
|
|
|
|
//.extend(parent_pos.z + transform.translation.z * parent_scale.z),
|
|
|
|
)
|
|
|
|
.with_rotation(rot.as_quat()),
|
|
|
|
)
|
|
|
|
.reparented_to(&GlobalTransform::from(parent_transform));
|
|
|
|
|
|
|
|
transform.translation = new_transform.translation;
|
|
|
|
transform.rotation = new_transform.rotation;
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-01 15:19:43 +00:00
|
|
|
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();
|
2024-04-01 18:38:28 +00:00
|
|
|
}
|
2024-04-01 15:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|