2024-04-21 16:23:40 +00:00
|
|
|
// ▄████████▄ + ███ + ▄█████████ ███ +
|
|
|
|
// ███▀ ▀███ + + ███ ███▀ + ███ + +
|
|
|
|
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
|
|
|
|
// ███ +███ ███ ███ ███ ███ ██████ ███ ███ ███
|
|
|
|
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
|
|
|
|
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
|
|
|
|
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
|
|
|
|
// + + + ███
|
|
|
|
// + ▀████████████████████████████████████████████████████▀
|
|
|
|
|
2024-04-16 14:27:17 +00:00
|
|
|
use crate::{actor, audio, hud, nature, shading, var};
|
2024-03-16 20:44:51 +00:00
|
|
|
use bevy::prelude::*;
|
2024-04-02 03:12:53 +00:00
|
|
|
use bevy::math::{DVec3, I64Vec3};
|
2024-04-02 15:17:31 +00:00
|
|
|
use bevy::scene::{InstanceId, SceneInstance};
|
2024-04-17 02:02:40 +00:00
|
|
|
use bevy::render::mesh::Indices;
|
2024-03-29 15:33:12 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-04-16 02:04:22 +00:00
|
|
|
use bevy_xpbd_3d::plugins::sync;
|
2024-04-02 03:12:53 +00:00
|
|
|
use std::collections::HashMap;
|
2024-03-16 22:37:18 +00:00
|
|
|
use std::f32::consts::PI;
|
2024-04-02 03:51:50 +00:00
|
|
|
use fastrand;
|
2024-03-16 20:44:51 +00:00
|
|
|
|
2024-04-02 03:59:33 +00:00
|
|
|
const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds
|
2024-04-02 05:05:17 +00:00
|
|
|
const ASTEROID_SIZE_FACTOR: f32 = 10.0;
|
|
|
|
const RING_THICKNESS: f64 = 8.0e6;
|
2024-04-14 20:29:51 +00:00
|
|
|
const STARS_MAX_MAGNITUDE: f32 = 5.5; // max 7.0, see generate_starchart.py
|
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-17 02:08:44 +00:00
|
|
|
const SKYBOX: bool = false;
|
2024-04-02 03:12:53 +00:00
|
|
|
|
|
|
|
const ASTEROID_SPAWN_STEP: f64 = 500.0;
|
2024-04-02 03:41:12 +00:00
|
|
|
const ASTEROID_VIEW_RADIUS: f64 = 3000.0;
|
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-04-16 02:04:22 +00:00
|
|
|
"suit" => "models/suit_with_collider.glb#Scene0",
|
2024-04-10 20:36:19 +00:00
|
|
|
"suit_ar_chefhat" => "models/suit_ar_chefhat.glb#Scene0",
|
2024-03-20 20:03:22 +00:00
|
|
|
"asteroid1" => ASSET_ASTEROID1,
|
|
|
|
"asteroid2" => ASSET_ASTEROID2,
|
2024-04-16 14:04:53 +00:00
|
|
|
"asteroid_lum" => "models/asteroid_lum.glb#Scene0",
|
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-04-10 23:12:07 +00:00
|
|
|
"orb_busstop" => "models/orb_busstop.glb#Scene0",
|
|
|
|
"orb_busstop_dim" => "models/orb_busstop_dim.glb#Scene0",
|
2024-03-28 16:25:35 +00:00
|
|
|
"MeteorAceGT" => "models/MeteorAceGT.glb#Scene0",
|
2024-04-04 22:32:42 +00:00
|
|
|
"satellite" => "models/satellite.glb#Scene0",
|
2024-03-21 03:34:09 +00:00
|
|
|
"pizzeria" => "models/pizzeria2.glb#Scene0",
|
|
|
|
"pizzasign" => "models/pizzasign.glb#Scene0",
|
2024-04-05 20:16:01 +00:00
|
|
|
"selectagon" => "models/selectagon.glb#Scene0",
|
2024-04-20 00:09:04 +00:00
|
|
|
"orbitring" => "models/orbitring.glb#Scene0",
|
2024-04-10 15:37:06 +00:00
|
|
|
"clippy" => "models/clippy.glb#Scene0",
|
2024-04-10 19:03:30 +00:00
|
|
|
"clippy_ar" => "models/clippy_ar.glb#Scene0",
|
2024-04-16 17:44:22 +00:00
|
|
|
"whale" => "models/whale.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 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-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)));
|
2024-04-02 03:12:53 +00:00
|
|
|
app.insert_resource(ActiveAsteroids(HashMap::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
|
2024-04-16 02:04:22 +00:00
|
|
|
app.insert_resource(sync::SyncConfig {
|
2024-04-16 00:44:01 +00:00
|
|
|
position_to_transform: true,
|
2024-04-01 17:20:31 +00:00
|
|
|
transform_to_position: false,
|
|
|
|
});
|
|
|
|
// Add own position->transform sync function
|
2024-04-16 02:04:22 +00:00
|
|
|
app.add_systems(PostUpdate, position_to_transform
|
|
|
|
.after(sync::position_to_transform)
|
|
|
|
.in_set(sync::SyncSet::PositionToTransform));
|
2024-04-01 17:20:31 +00:00
|
|
|
}
|
2024-03-17 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 16:33:31 +00:00
|
|
|
#[derive(Resource)] struct AsteroidUpdateTimer(Timer);
|
2024-04-19 22:47:35 +00:00
|
|
|
#[derive(Resource)] pub struct ActiveAsteroids(pub HashMap<I64Vec3, AsteroidData>);
|
2024-04-07 16:35:19 +00:00
|
|
|
#[derive(Resource)] struct AsteroidModel1(Handle<Scene>);
|
|
|
|
#[derive(Resource)] struct AsteroidModel2(Handle<Scene>);
|
2024-04-01 16:33:31 +00:00
|
|
|
|
2024-04-01 18:52:57 +00:00
|
|
|
#[derive(Component)] struct Asteroid;
|
2024-04-05 00:58:02 +00:00
|
|
|
#[derive(Component)] pub struct DespawnOnPlayerDeath;
|
2024-04-01 18:52:57 +00:00
|
|
|
|
2024-04-19 22:47:35 +00:00
|
|
|
pub struct AsteroidData {
|
2024-04-02 03:12:53 +00:00
|
|
|
entity: Entity,
|
|
|
|
//viewdistance: f64,
|
2024-04-01 16:33:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 21:54:56 +00:00
|
|
|
#[derive(Event)]
|
2024-04-02 03:12:53 +00:00
|
|
|
pub struct DespawnEvent {
|
|
|
|
entity: Entity,
|
2024-04-02 15:17:31 +00:00
|
|
|
sceneinstance: InstanceId,
|
2024-04-02 03:12:53 +00:00
|
|
|
origin: I64Vec3,
|
|
|
|
}
|
2024-04-01 21:54:56 +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-17 02:02:40 +00:00
|
|
|
mut materials_skybox: ResMut<Assets<shading::SkyBox>>,
|
2024-04-07 16:35:19 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
2024-03-16 20:44:51 +00:00
|
|
|
) {
|
2024-04-07 16:35:19 +00:00
|
|
|
// Load assets
|
|
|
|
commands.insert_resource(AsteroidModel1(asset_server.load(ASSET_ASTEROID1)));
|
|
|
|
commands.insert_resource(AsteroidModel2(asset_server.load(ASSET_ASTEROID2)));
|
|
|
|
|
2024-03-18 21:13:37 +00:00
|
|
|
// Generate starmap
|
2024-04-18 22:42:39 +00:00
|
|
|
let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(16, 16));
|
2024-03-21 02:20:44 +00:00
|
|
|
let mut starcount = 0;
|
2024-04-19 01:42:59 +00:00
|
|
|
for (index, star) in nature::STARS.iter().enumerate() {
|
2024-04-19 01:54:17 +00:00
|
|
|
let (x, y, z, mag, _absmag, color_index, name) = *star;
|
2024-03-21 02:20:44 +00:00
|
|
|
if mag > STARS_MAX_MAGNITUDE {
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-19 01:42:59 +00:00
|
|
|
|
|
|
|
let (r, g, b) = nature::star_color_index_to_rgb(color_index);
|
|
|
|
let mut pos = DVec3::new(x as f64, z as f64, -y as f64) * nature::PARSEC2METER;
|
|
|
|
if pos.length() > 1e21 {
|
|
|
|
pos *= 0.002;
|
|
|
|
}
|
|
|
|
let pos_render = pos * 1.0;
|
|
|
|
let scale_factor = 1e-4 * pos_render.length() as f32; // from experimentation
|
|
|
|
|
2024-04-14 20:31:10 +00:00
|
|
|
let mag = mag.min(6.0);
|
2024-04-19 01:42:59 +00:00
|
|
|
let scale_size = {|mag: f32|
|
|
|
|
scale_factor * (0.230299 * mag * mag - 3.09013 * mag + 15.1782)
|
|
|
|
};
|
|
|
|
let scale = scale_size(mag);
|
|
|
|
|
2024-03-21 01:35:16 +00:00
|
|
|
let scale_color = {|color: f32|
|
2024-04-19 01:42:59 +00:00
|
|
|
1.2 * color * (0.0659663 * mag * mag - 1.09862 * mag + 4.3)
|
2024-03-21 01:11:07 +00:00
|
|
|
};
|
2024-04-19 01:42:59 +00:00
|
|
|
//let scale = translation.length().powf(0.84);
|
|
|
|
//pos_render.length().powf(0.64)
|
|
|
|
//(radius as f64 * nature::SOL_RADIUS).powf(0.02) as f32 *
|
|
|
|
|
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-19 01:42:59 +00:00
|
|
|
let name = if name.is_empty() {
|
|
|
|
format!("Uncharted Star #{index}")
|
2024-04-07 23:08:32 +00:00
|
|
|
} else {
|
2024-04-19 01:42:59 +00:00
|
|
|
name.to_string()
|
|
|
|
};
|
|
|
|
let distance = if pos.length() > 1e21 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(pos.length())
|
2024-04-07 23:08:32 +00:00
|
|
|
};
|
2024-04-18 22:42:39 +00:00
|
|
|
|
2024-03-18 21:13:37 +00:00
|
|
|
commands.spawn((
|
|
|
|
Star,
|
2024-04-07 22:39:57 +00:00
|
|
|
hud::IsClickable {
|
2024-04-07 23:08:32 +00:00
|
|
|
name: Some(name),
|
2024-04-19 01:42:59 +00:00
|
|
|
distance,
|
2024-04-20 00:48:55 +00:00
|
|
|
..default()
|
2024-04-07 22:39:57 +00:00
|
|
|
},
|
2024-03-18 21:13:37 +00:00
|
|
|
PbrBundle {
|
|
|
|
mesh: sphere_handle.clone(),
|
2024-04-07 15:59:40 +00:00
|
|
|
material: star_color_handle,
|
2024-04-14 19:52:29 +00:00
|
|
|
transform: Transform {
|
2024-04-19 01:42:59 +00:00
|
|
|
translation: pos_render.as_vec3(),
|
|
|
|
scale: Vec3::splat(scale as f32),
|
2024-04-18 22:42:39 +00:00
|
|
|
..default()
|
2024-04-14 19:52:29 +00:00
|
|
|
},
|
2024-03-18 21:13:37 +00:00
|
|
|
..default()
|
2024-04-18 22:42:39 +00:00
|
|
|
},
|
2024-03-18 21:13:37 +00:00
|
|
|
));
|
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-04-17 02:02:40 +00:00
|
|
|
// Add shaded skybox
|
2024-04-17 02:08:44 +00:00
|
|
|
if SKYBOX {
|
|
|
|
let mut mesh = Mesh::from(Sphere::new(1e10).mesh().uv(5, 5));
|
|
|
|
//let mut mesh = Mesh::from(Cuboid::from_size(Vec3::splat(2e10)));
|
|
|
|
if let Some(Indices::U32(indices)) = mesh.indices_mut() {
|
|
|
|
// Reverse the order of each triangle to avoid backface culling
|
|
|
|
for slice in indices.chunks_mut(3) {
|
|
|
|
slice.reverse();
|
|
|
|
}
|
2024-04-17 02:02:40 +00:00
|
|
|
}
|
2024-04-17 02:08:44 +00:00
|
|
|
commands.spawn(MaterialMeshBundle {
|
2024-04-17 02:02:40 +00:00
|
|
|
mesh: meshes.add(mesh),
|
|
|
|
material: materials_skybox.add(shading::SkyBox {}),
|
|
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
|
|
|
|
..default()
|
2024-04-17 02:08:44 +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 spawn_despawn_asteroids(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut timer: ResMut<AsteroidUpdateTimer>,
|
|
|
|
mut commands: Commands,
|
2024-04-01 18:52:57 +00:00
|
|
|
q_player: Query<&Position, With<actor::PlayerCamera>>,
|
2024-04-02 03:12:53 +00:00
|
|
|
mut ew_despawn: EventWriter<DespawnEvent>,
|
|
|
|
mut db: ResMut<ActiveAsteroids>,
|
2024-04-07 16:35:19 +00:00
|
|
|
asteroid1_handle: Res<AsteroidModel1>,
|
|
|
|
asteroid2_handle: Res<AsteroidModel2>,
|
2024-04-02 15:17:31 +00:00
|
|
|
mut q_asteroid: Query<(Entity, &SceneInstance), With<Asteroid>>,
|
2024-04-02 03:59:33 +00:00
|
|
|
mut last_player_cell: Local<I64Vec3>,
|
2024-04-19 20:41:16 +00:00
|
|
|
id2pos: Res<actor::Id2Pos>,
|
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-02 03:59:33 +00:00
|
|
|
//if q_player.is_empty() {
|
2024-04-01 16:33:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-19 20:41:16 +00:00
|
|
|
let jupiter_pos = if let Some(jupiter_pos) = id2pos.0.get(&"jupiter".to_string()) {
|
|
|
|
*jupiter_pos
|
|
|
|
} else {
|
|
|
|
error!("Can't spawn asteroids because Jupiter's position can not be determined");
|
|
|
|
return;
|
|
|
|
};
|
2024-04-01 18:52:57 +00:00
|
|
|
let player = q_player.get_single().unwrap();
|
2024-04-19 20:41:16 +00:00
|
|
|
let fromjupiter = player.0 - jupiter_pos;
|
2024-04-02 03:12:53 +00:00
|
|
|
let player_cell = I64Vec3::new(
|
2024-04-19 20:41:16 +00:00
|
|
|
(fromjupiter.x / ASTEROID_SPAWN_STEP).round() as i64,
|
|
|
|
(fromjupiter.y / ASTEROID_SPAWN_STEP).round() as i64,
|
|
|
|
(fromjupiter.z / ASTEROID_SPAWN_STEP).round() as i64,
|
2024-04-02 03:12:53 +00:00
|
|
|
);
|
2024-04-02 03:59:33 +00:00
|
|
|
if *last_player_cell == player_cell {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*last_player_cell = player_cell;
|
|
|
|
|
2024-04-02 03:12:53 +00:00
|
|
|
// Parameters
|
|
|
|
let view_radius: f64 = ASTEROID_VIEW_RADIUS;
|
|
|
|
let step: f64 = ASTEROID_SPAWN_STEP;
|
|
|
|
let stepmax: i64 = (view_radius / step) as i64;
|
|
|
|
|
2024-04-02 03:59:33 +00:00
|
|
|
// Despawn far asteroids
|
2024-04-02 03:12:53 +00:00
|
|
|
let x_min = player_cell.x - stepmax;
|
|
|
|
let x_max = player_cell.x + stepmax;
|
|
|
|
let y_min = player_cell.y - stepmax;
|
|
|
|
let y_max = player_cell.y + stepmax;
|
|
|
|
let z_min = player_cell.z - stepmax;
|
|
|
|
let z_max = player_cell.z + stepmax;
|
|
|
|
for (origin, asteroid) in db.0.iter() {
|
|
|
|
if origin.x < x_min || origin.x > x_max
|
|
|
|
|| origin.y < y_min || origin.y > y_max
|
|
|
|
|| origin.z < z_min || origin.z > z_max
|
|
|
|
{
|
|
|
|
let mut despawning_worked = false;
|
2024-04-02 15:17:31 +00:00
|
|
|
for (ent, sceneinstance) in &mut q_asteroid {
|
2024-04-02 03:12:53 +00:00
|
|
|
if ent == asteroid.entity {
|
|
|
|
ew_despawn.send(DespawnEvent {
|
|
|
|
entity: asteroid.entity,
|
2024-04-02 15:17:31 +00:00
|
|
|
sceneinstance: **sceneinstance,
|
2024-04-02 03:12:53 +00:00
|
|
|
origin: origin.clone(),
|
|
|
|
});
|
|
|
|
despawning_worked = true;
|
|
|
|
break;
|
2024-04-01 18:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 03:12:53 +00:00
|
|
|
if !despawning_worked {
|
|
|
|
error!("Couldn't despawn asteroid:");
|
|
|
|
dbg!(origin);
|
|
|
|
}
|
2024-04-01 16:33:31 +00:00
|
|
|
}
|
2024-04-02 03:12:53 +00:00
|
|
|
}
|
2024-04-01 18:52:57 +00:00
|
|
|
|
2024-04-02 05:05:17 +00:00
|
|
|
// Density based on the radius alone
|
2024-04-19 20:41:16 +00:00
|
|
|
let radius_plane = (fromjupiter.x * fromjupiter.x + fromjupiter.z * fromjupiter.z).sqrt();
|
2024-04-02 05:05:17 +00:00
|
|
|
let density_r = nature::ring_density((radius_plane / 1e6) as f32);
|
|
|
|
if density_r < 0.001 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Density based on radius and the vertical distance to the ring
|
2024-04-19 20:41:16 +00:00
|
|
|
let normalized_distance = fromjupiter.y / (RING_THICKNESS / 2.0);
|
2024-04-02 05:05:17 +00:00
|
|
|
let density = density_r * (-4.0 * normalized_distance.powf(2.0)).exp() as f32;
|
|
|
|
if density < 0.001 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-02 03:51:50 +00:00
|
|
|
let mut rng = fastrand::Rng::new();
|
2024-04-02 03:12:53 +00:00
|
|
|
for x in -stepmax..=stepmax {
|
|
|
|
for y in -stepmax..=stepmax {
|
|
|
|
for z in -stepmax..=stepmax {
|
|
|
|
let origin = I64Vec3::new(
|
|
|
|
player_cell.x + x as i64,
|
|
|
|
player_cell.y + y as i64,
|
|
|
|
player_cell.z + z as i64,
|
|
|
|
);
|
|
|
|
if db.0.contains_key(&origin) {
|
|
|
|
// already in db, nothing to do
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-04-02 03:51:50 +00:00
|
|
|
// Get a seed based on all of the origin axes
|
|
|
|
// Probably there's a faster way
|
|
|
|
rng.seed(origin.x as u64);
|
|
|
|
let seed = rng.u64(0..u64::MAX).wrapping_add(origin.y as u64);
|
|
|
|
rng.seed(seed);
|
|
|
|
let seed = rng.u64(0..u64::MAX).wrapping_add(origin.z as u64);
|
|
|
|
rng.seed(seed);
|
|
|
|
|
|
|
|
let rand_s = rng.f32();
|
2024-04-02 05:05:17 +00:00
|
|
|
let size_raw: f32 = (rand_s + 1e-4).powf(-0.5) as f32; // -> between ~1 and 100
|
|
|
|
let size_density = size_raw * density;
|
2024-04-02 03:41:12 +00:00
|
|
|
|
2024-04-02 05:05:17 +00:00
|
|
|
if size_density < 0.3 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if size_raw < 20.0 {
|
2024-04-02 03:41:12 +00:00
|
|
|
let dist = player_cell.as_dvec3().distance(origin.as_dvec3());
|
2024-04-02 05:05:17 +00:00
|
|
|
if dist > 6.0 {
|
2024-04-02 03:41:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 05:05:17 +00:00
|
|
|
let size: f32 = ASTEROID_SIZE_FACTOR * size_density;
|
2024-04-02 03:41:12 +00:00
|
|
|
|
2024-04-02 03:51:50 +00:00
|
|
|
let rand_x = rng.f64();
|
|
|
|
let rand_y = rng.f64();
|
|
|
|
let rand_z = rng.f64();
|
|
|
|
let rand_c = rng.bool();
|
|
|
|
let class = if rand_c { 0 } else { 1 };
|
2024-04-02 03:12:53 +00:00
|
|
|
|
|
|
|
//let max_viewdist = ASTEROID_VIEW_RADIUS / ASTEROID_SPAWN_STEP;
|
2024-04-02 03:51:50 +00:00
|
|
|
let wobble = ASTEROID_SPAWN_STEP * 0.5;
|
2024-04-19 20:41:16 +00:00
|
|
|
let pos = jupiter_pos + DVec3::new(
|
2024-04-02 03:41:12 +00:00
|
|
|
origin.x as f64 * ASTEROID_SPAWN_STEP + wobble * rand_x * 2.0 - 1.0,
|
|
|
|
origin.y as f64 * ASTEROID_SPAWN_STEP + wobble * rand_y * 2.0 - 1.0,
|
|
|
|
origin.z as f64 * ASTEROID_SPAWN_STEP + wobble * rand_z * 2.0 - 1.0,
|
2024-04-02 03:12:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Spawn
|
|
|
|
let mut entity_commands = commands.spawn((
|
|
|
|
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),
|
|
|
|
Rotation::from(Quat::from_rotation_y(-PI / 3.)),
|
|
|
|
Position::new(pos),
|
|
|
|
Asteroid,
|
2024-04-19 22:47:35 +00:00
|
|
|
DespawnOnPlayerDeath,
|
2024-04-02 03:12:53 +00:00
|
|
|
));
|
2024-04-07 16:35:19 +00:00
|
|
|
let model = match class {
|
|
|
|
0 => asteroid1_handle.0.clone(),
|
|
|
|
_ => asteroid2_handle.0.clone(),
|
2024-04-02 03:12:53 +00:00
|
|
|
};
|
|
|
|
entity_commands.insert(SceneBundle {
|
2024-04-07 16:35:19 +00:00
|
|
|
scene: model,
|
2024-04-02 03:12:53 +00:00
|
|
|
transform: Transform {
|
2024-04-02 05:05:17 +00:00
|
|
|
scale: Vec3::splat(size),
|
2024-04-02 03:12:53 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-04-01 18:52:57 +00:00
|
|
|
..default()
|
2024-04-02 03:12:53 +00:00
|
|
|
});
|
|
|
|
db.0.insert(origin, AsteroidData {
|
|
|
|
entity: entity_commands.id(),
|
|
|
|
//viewdistance: 99999999.0,
|
|
|
|
});
|
|
|
|
}
|
2024-04-01 18:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
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>,
|
2024-04-02 03:12:53 +00:00
|
|
|
mut db: ResMut<ActiveAsteroids>,
|
2024-04-02 15:17:31 +00:00
|
|
|
mut scene_spawner: ResMut<SceneSpawner>,
|
2024-04-01 21:54:56 +00:00
|
|
|
) {
|
|
|
|
for despawn in er_despawn.read() {
|
2024-04-02 03:12:53 +00:00
|
|
|
commands.entity(despawn.entity).despawn();
|
2024-04-02 15:17:31 +00:00
|
|
|
scene_spawner.despawn_instance(despawn.sceneinstance);
|
2024-04-02 03:12:53 +00:00
|
|
|
db.0.remove(&despawn.origin);
|
2024-04-01 21:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05 23:36:14 +00:00
|
|
|
mut q_life: Query<(&mut actor::LifeForm, &mut actor::ExperiencesGForce), With<actor::Player>>,
|
2024-04-14 21:38:55 +00:00
|
|
|
q_target: Query<(&Transform, &Position, &LinearVelocity), (With<hud::IsTargeted>, Without<actor::PlayerCamera>)>,
|
2024-04-05 01:31:52 +00:00
|
|
|
mut ew_playerdies: EventWriter<actor::PlayerDiesEvent>,
|
2024-04-14 12:55:00 +00:00
|
|
|
mut settings: ResMut<var::Settings>,
|
2024-04-19 20:29:57 +00:00
|
|
|
id2pos: Res<actor::Id2Pos>,
|
2024-04-07 23:44:36 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-04-01 03:25:35 +00:00
|
|
|
) {
|
2024-04-02 15:24:39 +00:00
|
|
|
if q_player.is_empty() || q_life.is_empty() {
|
2024-04-01 03:25:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-04-01 15:19:43 +00:00
|
|
|
let (trans, mut pos, mut v) = q_player.get_single_mut().unwrap();
|
2024-04-05 23:36:14 +00:00
|
|
|
let (mut lifeform, mut gforce) = 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-07 23:44:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-01 03:25:35 +00:00
|
|
|
if key_input.just_pressed(settings.key_cheat_stop) {
|
2024-04-05 23:36:14 +00:00
|
|
|
gforce.ignore_gforce_seconds = 1.0;
|
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-05 23:36:14 +00:00
|
|
|
gforce.ignore_gforce_seconds = 1.0;
|
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-05 23:36:14 +00:00
|
|
|
gforce.ignore_gforce_seconds = 1.0;
|
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-14 21:38:55 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 15:24:39 +00:00
|
|
|
if !settings.dev_mode {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if key_input.just_pressed(settings.key_cheat_pizza) {
|
2024-04-19 20:29:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-02 15:24:39 +00:00
|
|
|
}
|
|
|
|
if key_input.just_pressed(settings.key_cheat_farview1) {
|
2024-04-19 20:29:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-02 15:24:39 +00:00
|
|
|
}
|
|
|
|
if key_input.just_pressed(settings.key_cheat_farview2) {
|
2024-04-19 20:29:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-02 15:24:39 +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-05 01:31:52 +00:00
|
|
|
if key_input.just_pressed(settings.key_cheat_die) {
|
2024-04-11 19:17:34 +00:00
|
|
|
settings.god_mode = false;
|
2024-04-11 18:46:52 +00:00
|
|
|
ew_playerdies.send(actor::PlayerDiesEvent(actor::DamageType::Trauma));
|
2024-04-05 01:31:52 +00:00
|
|
|
}
|
2024-04-01 03:25:35 +00:00
|
|
|
}
|
2024-04-01 15:19:43 +00:00
|
|
|
|
2024-04-16 17:51:04 +00:00
|
|
|
// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts
|
2024-04-01 15:19:43 +00:00
|
|
|
// 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-16 21:31:08 +00:00
|
|
|
mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation), Without<Parent>>,
|
2024-04-01 15:19:43 +00:00
|
|
|
) {
|
|
|
|
if let Ok(player_pos) = q_player.get_single() {
|
2024-04-16 21:31:08 +00:00
|
|
|
for (mut transform, pos, rot) in &mut q_trans {
|
2024-04-16 17:51:04 +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,
|
|
|
|
);
|
2024-04-16 21:31:08 +00:00
|
|
|
transform.rotation = rot.as_quat();
|
2024-04-01 15:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|