2024-03-20 03:34:09 +00:00
|
|
|
extern crate regex;
|
2024-03-29 18:41:46 +00:00
|
|
|
use crate::{actor, nature};
|
2024-03-20 03:34:09 +00:00
|
|
|
use regex::Regex;
|
2024-03-16 20:44:51 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-18 19:58:16 +00:00
|
|
|
//use bevy::core_pipeline::Skybox;
|
|
|
|
//use bevy::asset::LoadState;
|
|
|
|
//use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension};
|
2024-03-16 22:37:18 +00:00
|
|
|
use bevy::pbr::CascadeShadowConfigBuilder;
|
2024-03-29 15:33:12 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-03-16 22:37:18 +00:00
|
|
|
use std::f32::consts::PI;
|
2024-03-16 20:44:51 +00:00
|
|
|
|
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-03-18 19:58:16 +00:00
|
|
|
//const SKYBOX_BRIGHTNESS: f32 = 300.0;
|
|
|
|
//const SKYBOX_BRIGHTNESS_AR: f32 = 100.0;
|
2024-03-18 03:39:26 +00:00
|
|
|
|
2024-03-18 19:58:16 +00:00
|
|
|
//const ASSET_CUBEMAP: &str = "textures/cubemap-fs8.png";
|
|
|
|
//const ASSET_CUBEMAP_AR: &str = "textures/out.png";
|
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-20 19:37:35 +00:00
|
|
|
fn asset_name_to_path(name: &str) -> &'static str {
|
|
|
|
match name {
|
2024-03-21 01:51:07 +00:00
|
|
|
"suit" => "models/suit.glb#Scene0",
|
2024-03-20 20:12:37 +00:00
|
|
|
"jupiter" => "models/jupiter.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-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-20 03:34:09 +00:00
|
|
|
app.add_systems(Startup, (setup, load_defs));
|
2024-03-18 14:40:35 +00:00
|
|
|
//app.add_systems(Update, asset_loaded.after(load_cubemap_asset));
|
2024-03-18 19:58:16 +00:00
|
|
|
//app.add_systems(Update, swap_world_on_ar_toggle);
|
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-03-18 19:58:16 +00:00
|
|
|
app.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)));
|
2024-03-29 15:39:37 +00:00
|
|
|
app.insert_resource(Gravity(Vec3::splat(0.0)));
|
2024-03-17 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 21:13:37 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Star;
|
|
|
|
|
2024-03-18 19:58:16 +00:00
|
|
|
//#[derive(Resource)]
|
|
|
|
//pub struct WorldState {
|
|
|
|
// is_loaded: bool,
|
|
|
|
// entities_viewn_through_ar: bool,
|
|
|
|
// cubemap_handle: Handle<Image>,
|
|
|
|
// cubemap_ar_handle: Handle<Image>,
|
|
|
|
//}
|
|
|
|
//impl WorldState {
|
|
|
|
// pub fn get_cubemap_handle(&self) -> Handle<Image> {
|
|
|
|
// if self.entities_viewn_through_ar {
|
|
|
|
// self.cubemap_ar_handle.clone()
|
|
|
|
// } else {
|
|
|
|
// self.cubemap_handle.clone()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
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-03-16 20:44:51 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
2024-03-18 19:58:16 +00:00
|
|
|
// let cubemap_handle = asset_server.load(ASSET_CUBEMAP);
|
|
|
|
// commands.insert_resource(WorldState {
|
|
|
|
// is_loaded: false,
|
|
|
|
// entities_viewn_through_ar: settings.hud_active,
|
|
|
|
// cubemap_handle: asset_server.load(ASSET_CUBEMAP),
|
|
|
|
// cubemap_ar_handle: asset_server.load(ASSET_CUBEMAP_AR),
|
|
|
|
// });
|
2024-03-18 14:40:35 +00:00
|
|
|
|
2024-03-18 03:39:26 +00:00
|
|
|
// Generate a bunch of asteriods
|
2024-03-30 21:59:18 +00:00
|
|
|
let maxdist = 4;
|
2024-03-18 21:13:37 +00:00
|
|
|
for i in -maxdist..maxdist {
|
|
|
|
for j in -maxdist..maxdist {
|
|
|
|
for k in -maxdist..maxdist {
|
2024-03-18 03:00:41 +00:00
|
|
|
let offset = 500.0;
|
2024-03-19 20:09:20 +00:00
|
|
|
let dist = 8e3;
|
2024-03-18 03:39:26 +00:00
|
|
|
let wobble = dist/2.0;
|
2024-03-18 03:00:41 +00:00
|
|
|
let (i, j, k) = (i as f32, j as f32, k as f32);
|
2024-03-19 20:09:20 +00:00
|
|
|
let asset = match ((i+j+k) as i32) % 2 {
|
|
|
|
0 => ASSET_ASTEROID1,
|
|
|
|
_ => ASSET_ASTEROID2,
|
|
|
|
};
|
2024-03-18 02:42:14 +00:00
|
|
|
commands.spawn((
|
2024-03-30 21:59:18 +00:00
|
|
|
actor::Actor::default(),
|
|
|
|
RigidBody::Dynamic,
|
2024-03-30 22:13:22 +00:00
|
|
|
AngularVelocity(Vec3::new(0.1, 0.1, 0.03)),
|
2024-03-30 21:59:18 +00:00
|
|
|
LinearVelocity(Vec3::new(0.0, 0.0, 0.35)),
|
|
|
|
Collider::sphere(1.0),
|
2024-03-19 20:09:20 +00:00
|
|
|
SceneBundle {
|
|
|
|
transform: Transform {
|
|
|
|
translation: Vec3::new(
|
|
|
|
offset + dist * i + wobble * (j+k/PI).sin() * (k+j/PI).cos(),
|
|
|
|
offset + dist * j + wobble * (k+i/PI).sin() * (i+k/PI).cos(),
|
|
|
|
offset + dist * k + wobble * (i+j/PI).sin() * (j+i/PI).cos(),
|
|
|
|
),
|
|
|
|
rotation: Quat::from_rotation_y(-PI / 3.),
|
|
|
|
scale: Vec3::splat(ASTEROID_SIZE),
|
|
|
|
},
|
|
|
|
scene: asset_server.load(asset),
|
2024-03-18 02:42:14 +00:00
|
|
|
..default()
|
2024-03-19 20:09:20 +00:00
|
|
|
},
|
2024-03-18 02:42:14 +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-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-03-21 01:11:07 +00:00
|
|
|
let dist = 1e7;
|
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-19 17:15:19 +00:00
|
|
|
// Add Light from the Sun
|
2024-03-16 22:37:18 +00:00
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
|
|
directional_light: DirectionalLight {
|
2024-03-16 22:44:46 +00:00
|
|
|
illuminance: 1000.0,
|
2024-03-16 22:52:26 +00:00
|
|
|
shadows_enabled: false,
|
2024-03-16 22:37:18 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-03-18 23:03:02 +00:00
|
|
|
transform: Transform::from_rotation(Quat::from_rotation_y(PI/2.0)),
|
2024-03-16 22:37:18 +00:00
|
|
|
cascade_shadow_config: CascadeShadowConfigBuilder {
|
|
|
|
first_cascade_far_bound: 7.0,
|
|
|
|
maximum_distance: 25.0,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-16 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:04:06 +00:00
|
|
|
enum DefClass {
|
|
|
|
Actor,
|
|
|
|
Chat,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2024-03-20 03:54:39 +00:00
|
|
|
struct ParserState {
|
2024-03-20 19:04:06 +00:00
|
|
|
class: DefClass,
|
|
|
|
|
|
|
|
// Generic fields
|
2024-03-20 03:54:39 +00:00
|
|
|
name: String,
|
2024-03-20 19:04:06 +00:00
|
|
|
chat: String,
|
|
|
|
|
|
|
|
// Actor fields
|
2024-03-23 20:26:56 +00:00
|
|
|
id: String,
|
2024-03-20 19:04:06 +00:00
|
|
|
pos: Vec3,
|
|
|
|
model: String,
|
|
|
|
model_scale: f32,
|
2024-03-20 19:50:57 +00:00
|
|
|
rotation: Quat,
|
2024-03-30 22:13:22 +00:00
|
|
|
angular_momentum: Vec3,
|
2024-03-20 19:04:06 +00:00
|
|
|
pronoun: String,
|
2024-03-29 18:41:46 +00:00
|
|
|
is_player: bool,
|
2024-03-20 19:04:06 +00:00
|
|
|
is_lifeform: bool,
|
|
|
|
is_alive: bool,
|
|
|
|
is_suited: bool,
|
2024-03-28 12:26:14 +00:00
|
|
|
is_vehicle: bool,
|
2024-03-30 18:57:35 +00:00
|
|
|
has_physics: bool,
|
2024-03-28 12:26:14 +00:00
|
|
|
thrust_forward: f32,
|
|
|
|
thrust_sideways: f32,
|
|
|
|
thrust_back: f32,
|
|
|
|
reaction_wheels: f32,
|
2024-03-28 23:01:17 +00:00
|
|
|
warmup_seconds: f32,
|
2024-03-28 13:10:10 +00:00
|
|
|
engine_type: actor::EngineType,
|
2024-03-23 20:26:56 +00:00
|
|
|
oxygen: f32,
|
2024-03-29 17:20:12 +00:00
|
|
|
mass: f32,
|
|
|
|
collider: Collider,
|
2024-03-29 18:41:46 +00:00
|
|
|
camdistance: f32,
|
2024-03-30 18:54:01 +00:00
|
|
|
suit_integrity: f32,
|
2024-03-20 19:04:06 +00:00
|
|
|
|
|
|
|
// Chat fields
|
2024-03-20 03:54:39 +00:00
|
|
|
delay: f64,
|
|
|
|
text: String,
|
|
|
|
level: String,
|
|
|
|
label: String,
|
|
|
|
goto: String,
|
|
|
|
is_choice: bool,
|
2024-03-20 04:52:02 +00:00
|
|
|
stores_item: bool,
|
2024-03-23 19:13:37 +00:00
|
|
|
script: String,
|
|
|
|
script_parameter: String,
|
2024-03-23 19:49:48 +00:00
|
|
|
script_parameter2: String,
|
2024-03-20 03:54:39 +00:00
|
|
|
}
|
|
|
|
impl Default for ParserState {
|
|
|
|
fn default() -> Self {
|
2024-03-20 20:03:22 +00:00
|
|
|
let default_actor = actor::Actor::default();
|
2024-03-28 12:26:14 +00:00
|
|
|
let default_engine = actor::Engine::default();
|
2024-03-20 03:54:39 +00:00
|
|
|
Self {
|
2024-03-20 19:04:06 +00:00
|
|
|
class: DefClass::None,
|
2024-03-20 19:50:57 +00:00
|
|
|
name: "NONAME".to_string(),
|
2024-03-20 19:04:06 +00:00
|
|
|
chat: "".to_string(),
|
|
|
|
|
2024-03-23 20:26:56 +00:00
|
|
|
id: "".to_string(),
|
2024-03-20 19:04:06 +00:00
|
|
|
pos: Vec3::new(0.0, 0.0, 0.0),
|
|
|
|
model: "".to_string(),
|
|
|
|
model_scale: 1.0,
|
2024-03-20 19:50:57 +00:00
|
|
|
rotation: Quat::IDENTITY,
|
2024-03-30 22:13:22 +00:00
|
|
|
angular_momentum: Vec3::new(0.03, 0.3, 0.09),
|
2024-03-20 19:04:06 +00:00
|
|
|
pronoun: "they/them".to_string(),
|
2024-03-29 18:41:46 +00:00
|
|
|
is_player: false,
|
2024-03-20 19:04:06 +00:00
|
|
|
is_lifeform: false,
|
|
|
|
is_alive: false,
|
|
|
|
is_suited: false,
|
2024-03-28 12:26:14 +00:00
|
|
|
is_vehicle: false,
|
2024-03-30 18:57:35 +00:00
|
|
|
has_physics: true,
|
2024-03-28 12:26:14 +00:00
|
|
|
thrust_forward: default_engine.thrust_forward,
|
|
|
|
thrust_sideways: default_engine.thrust_forward,
|
|
|
|
thrust_back: default_engine.thrust_back,
|
|
|
|
reaction_wheels: default_engine.reaction_wheels,
|
2024-03-28 23:01:17 +00:00
|
|
|
warmup_seconds: default_engine.warmup_seconds,
|
2024-03-28 13:10:10 +00:00
|
|
|
engine_type: default_engine.engine_type,
|
2024-03-23 20:26:56 +00:00
|
|
|
oxygen: nature::OXY_D,
|
2024-03-29 17:20:12 +00:00
|
|
|
mass: 1.0,
|
|
|
|
collider: Collider::sphere(1.0),
|
2024-03-29 18:41:46 +00:00
|
|
|
camdistance: default_actor.camdistance,
|
2024-03-30 18:54:01 +00:00
|
|
|
suit_integrity: 1.0,
|
2024-03-20 19:04:06 +00:00
|
|
|
|
2024-03-20 03:54:39 +00:00
|
|
|
delay: 0.0,
|
|
|
|
text: "".to_string(),
|
2024-03-20 04:52:02 +00:00
|
|
|
level: "chat".to_string(),
|
2024-03-20 03:54:39 +00:00
|
|
|
label: "".to_string(),
|
|
|
|
goto: "".to_string(),
|
|
|
|
is_choice: false,
|
2024-03-20 04:52:02 +00:00
|
|
|
stores_item: false,
|
2024-03-23 19:13:37 +00:00
|
|
|
script: "".to_string(),
|
|
|
|
script_parameter: "".to_string(),
|
2024-03-23 19:49:48 +00:00
|
|
|
script_parameter2: "".to_string(),
|
2024-03-20 04:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl ParserState {
|
|
|
|
fn reset_message(&mut self) {
|
|
|
|
let default = ParserState::default();
|
|
|
|
self.label = default.label;
|
|
|
|
self.delay = default.delay;
|
|
|
|
self.goto = default.goto;
|
|
|
|
self.level = default.level;
|
|
|
|
self.text = default.text;
|
|
|
|
self.is_choice = default.is_choice;
|
|
|
|
}
|
|
|
|
fn reset_chat(&mut self) {
|
|
|
|
let default = ParserState::default();
|
|
|
|
self.reset_message();
|
|
|
|
self.stores_item = default.stores_item;
|
|
|
|
}
|
2024-03-20 19:04:06 +00:00
|
|
|
fn reset(&mut self) {
|
|
|
|
*self = Self::default();
|
|
|
|
}
|
2024-03-20 04:52:02 +00:00
|
|
|
fn as_chatbranch(&self) -> actor::ChatBranch {
|
|
|
|
return actor::ChatBranch {
|
|
|
|
id: self.chat.clone(),
|
|
|
|
name: self.name.clone(),
|
|
|
|
label: self.label.clone(),
|
|
|
|
delay: self.delay.clone(),
|
2024-03-20 05:42:39 +00:00
|
|
|
sound: if self.is_choice { "".to_string() } else { "chat".to_string() },
|
2024-03-20 04:52:02 +00:00
|
|
|
level: self.level.clone(),
|
|
|
|
reply: if self.is_choice { "".to_string() } else { self.text.clone() },
|
|
|
|
choice: if self.is_choice { self.text.clone() } else { "".to_string() },
|
|
|
|
goto: self.goto.clone(),
|
2024-03-23 19:13:37 +00:00
|
|
|
script: self.script.clone(),
|
|
|
|
script_parameter: self.script_parameter.clone(),
|
2024-03-23 19:49:48 +00:00
|
|
|
script_parameter2: self.script_parameter2.clone(),
|
2024-03-20 04:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:04:06 +00:00
|
|
|
fn spawn_chat(&mut self, commands: &mut Commands) {
|
2024-03-20 04:52:02 +00:00
|
|
|
if self.stores_item {
|
2024-03-20 05:02:06 +00:00
|
|
|
debug!("{:#?}", self.as_chatbranch());
|
2024-03-20 04:52:02 +00:00
|
|
|
commands.spawn(self.as_chatbranch());
|
2024-03-20 03:54:39 +00:00
|
|
|
}
|
2024-03-20 05:20:58 +00:00
|
|
|
self.reset_message();
|
2024-03-20 03:54:39 +00:00
|
|
|
}
|
2024-03-20 19:37:35 +00:00
|
|
|
fn spawn_actor(&mut self, commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
2024-03-29 16:18:43 +00:00
|
|
|
let mut actor = commands.spawn_empty();
|
|
|
|
actor.insert(actor::Actor {
|
2024-03-23 20:26:56 +00:00
|
|
|
id: self.id.clone(),
|
2024-03-29 18:41:46 +00:00
|
|
|
camdistance: self.camdistance,
|
2024-03-20 19:50:57 +00:00
|
|
|
..default()
|
2024-03-29 16:18:43 +00:00
|
|
|
});
|
|
|
|
actor.insert(SceneBundle {
|
2024-03-20 19:37:35 +00:00
|
|
|
transform: Transform {
|
|
|
|
translation: self.pos,
|
|
|
|
scale: Vec3::splat(self.model_scale),
|
2024-03-20 19:50:57 +00:00
|
|
|
rotation: self.rotation,
|
2024-03-20 19:37:35 +00:00
|
|
|
},
|
|
|
|
scene: asset_server.load(asset_name_to_path(self.model.as_str())),
|
|
|
|
..default()
|
2024-03-29 16:18:43 +00:00
|
|
|
});
|
2024-03-29 17:20:12 +00:00
|
|
|
|
|
|
|
// Physics Parameters
|
2024-03-30 18:57:35 +00:00
|
|
|
if self.has_physics {
|
|
|
|
let fix_scale = 1.0 / self.model_scale.powf(3.0);
|
|
|
|
actor.insert(RigidBody::Dynamic);
|
2024-03-30 22:13:22 +00:00
|
|
|
actor.insert(AngularVelocity(self.angular_momentum));
|
2024-03-30 18:57:35 +00:00
|
|
|
actor.insert(self.collider.clone());
|
|
|
|
actor.insert(ColliderDensity(self.mass * fix_scale));
|
|
|
|
}
|
2024-03-30 22:13:22 +00:00
|
|
|
// TODO: angular velocity for objects without collisions, static objects
|
2024-03-29 17:20:12 +00:00
|
|
|
|
2024-03-29 18:41:46 +00:00
|
|
|
// Optional Components
|
|
|
|
if self.is_player {
|
|
|
|
actor.insert(actor::Player);
|
|
|
|
actor.insert(actor::PlayerCamera);
|
|
|
|
}
|
2024-03-23 20:26:56 +00:00
|
|
|
if self.is_lifeform {
|
2024-03-29 16:18:43 +00:00
|
|
|
actor.insert(actor::LifeForm::default());
|
|
|
|
actor.insert(actor::Suit {
|
|
|
|
oxygen: self.oxygen,
|
|
|
|
oxygen_max: nature::OXY_D,
|
2024-03-30 18:54:01 +00:00
|
|
|
integrity: self.suit_integrity,
|
2024-03-29 16:18:43 +00:00
|
|
|
..default()
|
|
|
|
});
|
2024-03-20 19:37:35 +00:00
|
|
|
}
|
2024-03-29 16:18:43 +00:00
|
|
|
if !self.chat.is_empty() {
|
|
|
|
actor.insert(actor::Talker {
|
|
|
|
conv_id: self.chat.clone(),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if self.is_vehicle {
|
2024-03-30 21:31:07 +00:00
|
|
|
actor.insert(actor::Vehicle::default());
|
2024-03-29 18:41:46 +00:00
|
|
|
}
|
|
|
|
if self.is_vehicle || self.is_suited {
|
2024-03-29 16:18:43 +00:00
|
|
|
actor.insert(actor::Engine {
|
|
|
|
thrust_forward: self.thrust_forward,
|
|
|
|
thrust_back: self.thrust_back,
|
|
|
|
thrust_sideways: self.thrust_sideways,
|
|
|
|
reaction_wheels: self.reaction_wheels,
|
|
|
|
warmup_seconds: self.warmup_seconds,
|
|
|
|
engine_type: self.engine_type,
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-20 19:37:35 +00:00
|
|
|
}
|
2024-03-29 16:18:43 +00:00
|
|
|
|
2024-03-29 16:21:46 +00:00
|
|
|
//info!("Spawning actor {} with model {} at {}/{}/{}",
|
|
|
|
// self.name, self.model, self.pos.x, self.pos.y, self.pos.z);
|
2024-03-20 19:04:06 +00:00
|
|
|
self.reset();
|
|
|
|
}
|
2024-03-20 19:37:35 +00:00
|
|
|
fn spawn_entities(&mut self, commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
2024-03-20 19:04:06 +00:00
|
|
|
match self.class {
|
2024-03-20 19:37:35 +00:00
|
|
|
DefClass::Actor => { self.spawn_actor(commands, asset_server); }
|
2024-03-20 19:04:06 +00:00
|
|
|
DefClass::Chat => { self.spawn_chat(commands); }
|
|
|
|
DefClass::None => {}
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 03:54:39 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 03:34:09 +00:00
|
|
|
pub fn load_defs(
|
2024-03-20 04:52:02 +00:00
|
|
|
mut commands: Commands,
|
2024-03-20 19:37:35 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
2024-03-20 03:34:09 +00:00
|
|
|
) {
|
2024-03-20 20:38:25 +00:00
|
|
|
let re1 = Regex::new(r"^\s*([a-z_-]+)\s+(.*)$").unwrap();
|
|
|
|
let re2 = Regex::new("\"([^\"]*)\"|(-?[0-9]+(?:\\.[0-9]+)?)|([a-zA-Z_-][a-zA-Z0-9_-]*)").unwrap();
|
2024-03-20 03:34:09 +00:00
|
|
|
let defs_string = include_str!("defs.txt");
|
|
|
|
let mut lines = defs_string.lines();
|
2024-03-20 03:54:39 +00:00
|
|
|
let mut state = ParserState::default();
|
2024-03-20 20:36:53 +00:00
|
|
|
let mut command;
|
|
|
|
let mut parameters;
|
2024-03-20 03:54:39 +00:00
|
|
|
|
2024-03-20 03:34:09 +00:00
|
|
|
let mut line_nr = -1;
|
|
|
|
while let Some(line) = lines.next() {
|
|
|
|
line_nr += 1;
|
|
|
|
let caps = re1.captures(line);
|
|
|
|
if caps.is_none() {
|
|
|
|
if line.trim() != "" {
|
2024-03-20 19:50:57 +00:00
|
|
|
error!("Syntax Error in definitions line {}: `{}`", line_nr, line);
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-20 20:36:53 +00:00
|
|
|
if let Some(caps) = caps {
|
|
|
|
command = caps.get(1).unwrap().as_str();
|
|
|
|
parameters = caps.get(2).unwrap().as_str();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Failed to read regex captures in line {}: `{}`", line_nr, line);
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
|
|
|
|
let mut parts: Vec<&str> = Vec::new();
|
|
|
|
parts.push(command);
|
2024-03-20 20:36:53 +00:00
|
|
|
for caps in re2.captures_iter(parameters) {
|
2024-03-20 03:34:09 +00:00
|
|
|
if let Some(part) = caps.get(1) {
|
|
|
|
parts.push(&part.as_str());
|
|
|
|
}
|
|
|
|
if let Some(part) = caps.get(2) {
|
|
|
|
parts.push(&part.as_str());
|
|
|
|
}
|
2024-03-20 20:36:53 +00:00
|
|
|
if let Some(part) = caps.get(3) {
|
|
|
|
parts.push(&part.as_str());
|
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match parts.as_slice() {
|
2024-03-20 19:04:06 +00:00
|
|
|
// Parsing actors
|
|
|
|
["actor", x, y, z, model] => {
|
2024-03-20 20:03:22 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
|
|
|
state.reset();
|
2024-03-20 19:04:06 +00:00
|
|
|
state.class = DefClass::Actor;
|
|
|
|
state.model = model.to_string();
|
|
|
|
if let (Ok(x_float), Ok(y_float), Ok(z_float)) =
|
|
|
|
(x.parse::<f32>(), y.parse::<f32>(), z.parse::<f32>()) {
|
|
|
|
state.pos = Vec3::new(x_float, y_float, z_float);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse coordinates as floats in def: {line}");
|
|
|
|
state.reset();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 20:26:56 +00:00
|
|
|
["id", id] => {
|
|
|
|
state.id = id.to_string();
|
|
|
|
}
|
2024-03-20 19:04:06 +00:00
|
|
|
["alive", "yes"] => {
|
|
|
|
state.is_alive = true;
|
|
|
|
state.is_lifeform = true;
|
|
|
|
state.is_suited = true;
|
|
|
|
}
|
2024-03-28 12:26:14 +00:00
|
|
|
["vehicle", "yes"] => {
|
|
|
|
state.is_vehicle = true;
|
|
|
|
}
|
2024-03-23 20:26:56 +00:00
|
|
|
["oxygen", amount] => {
|
|
|
|
if let Ok(amount) = amount.parse::<f32>() {
|
|
|
|
state.is_lifeform = true;
|
|
|
|
state.is_suited = true;
|
|
|
|
state.oxygen = amount;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:04:06 +00:00
|
|
|
["pronoun", pronoun] => {
|
|
|
|
state.pronoun = pronoun.to_string();
|
|
|
|
}
|
|
|
|
["chatid", chat] => {
|
|
|
|
state.chat = chat.to_string();
|
|
|
|
}
|
|
|
|
["scale", scale] => {
|
|
|
|
if let Ok(scale_float) = scale.parse::<f32>() {
|
|
|
|
state.model_scale = scale_float;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 13:19:18 +00:00
|
|
|
["rotationx", rotation_x] => {
|
|
|
|
if let Ok(rotation_x_float) = rotation_x.parse::<f32>() {
|
|
|
|
state.rotation = Quat::from_rotation_x(PI * rotation_x_float);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:50:57 +00:00
|
|
|
["rotationy", rotation_y] => {
|
|
|
|
if let Ok(rotation_y_float) = rotation_y.parse::<f32>() {
|
|
|
|
state.rotation = Quat::from_rotation_y(PI * rotation_y_float);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
["angularmomentum", x, y, z] => {
|
|
|
|
if let (Ok(x_float), Ok(y_float), Ok(z_float)) =
|
|
|
|
(x.parse::<f32>(), y.parse::<f32>(), z.parse::<f32>()) {
|
2024-03-30 22:13:22 +00:00
|
|
|
state.angular_momentum = Vec3::new(x_float, y_float, z_float);
|
2024-03-20 19:50:57 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-28 23:01:17 +00:00
|
|
|
["thrust", forward, back, sideways, reaction_wheels, warmup_time] => {
|
|
|
|
if let (Ok(forward_float), Ok(back_float), Ok(sideways_float), Ok(reaction_wheels_float), Ok(warmup_time_float)) = (forward.parse::<f32>(), back.parse::<f32>(), sideways.parse::<f32>(), reaction_wheels.parse::<f32>(), warmup_time.parse::<f32>()) {
|
2024-03-28 12:26:14 +00:00
|
|
|
state.thrust_forward = forward_float;
|
|
|
|
state.thrust_back = back_float;
|
|
|
|
state.thrust_sideways = sideways_float;
|
|
|
|
state.reaction_wheels = reaction_wheels_float;
|
2024-03-28 23:01:17 +00:00
|
|
|
state.warmup_seconds = warmup_time_float;
|
2024-03-28 12:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 13:10:10 +00:00
|
|
|
["engine", "rocket"] => {
|
|
|
|
state.engine_type = actor::EngineType::Rocket;
|
|
|
|
}
|
2024-03-29 03:36:20 +00:00
|
|
|
["engine", "ion"] => {
|
|
|
|
state.engine_type = actor::EngineType::Ion;
|
|
|
|
}
|
2024-03-30 18:54:01 +00:00
|
|
|
["engine", "monopropellant"] => {
|
|
|
|
state.engine_type = actor::EngineType::Monopropellant;
|
|
|
|
}
|
|
|
|
["health", value] => {
|
|
|
|
if let Ok(value_float) = value.parse::<f32>() {
|
|
|
|
state.suit_integrity = value_float;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 17:20:12 +00:00
|
|
|
["mass", value] => {
|
|
|
|
if let Ok(value_float) = value.parse::<f32>() {
|
|
|
|
state.mass = value_float;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-30 18:57:35 +00:00
|
|
|
["physics", "off"] => {
|
|
|
|
state.has_physics = false;
|
|
|
|
}
|
2024-03-29 17:20:12 +00:00
|
|
|
["collider", "sphere", radius] => {
|
|
|
|
if let Ok(radius_float) = radius.parse::<f32>() {
|
|
|
|
state.collider = Collider::sphere(radius_float);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
["collider", "capsule", height, radius] => {
|
|
|
|
if let (Ok(height_float), Ok(radius_float)) = (height.parse::<f32>(), radius.parse::<f32>()) {
|
|
|
|
state.collider = Collider::capsule(height_float, radius_float);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 18:41:46 +00:00
|
|
|
["player", "yes"] => {
|
|
|
|
state.is_player = true;
|
|
|
|
state.is_alive = true;
|
|
|
|
}
|
|
|
|
["camdistance", value] => {
|
|
|
|
if let Ok(value_float) = value.parse::<f32>() {
|
|
|
|
state.camdistance = value_float;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error!("Can't parse float: {line}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:04:06 +00:00
|
|
|
|
|
|
|
// Parsing chats
|
2024-03-20 03:34:09 +00:00
|
|
|
["chat", chat_name] => {
|
2024-03-20 03:54:39 +00:00
|
|
|
debug!("Registering chat: {}", chat_name);
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 04:52:02 +00:00
|
|
|
state.reset_chat();
|
2024-03-20 19:37:35 +00:00
|
|
|
state.class = DefClass::Chat;
|
2024-03-20 04:52:02 +00:00
|
|
|
state.chat = chat_name.to_string();
|
2024-03-20 03:54:39 +00:00
|
|
|
}
|
|
|
|
["name", name] => {
|
|
|
|
debug!("Registering name: {}", name);
|
|
|
|
state.name = name.to_string();
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
["msg", sleep, text] => {
|
2024-03-20 04:52:02 +00:00
|
|
|
debug!("Registering message (sleep={}): {}", sleep, text);
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 03:54:39 +00:00
|
|
|
if let Ok(sleep_float) = sleep.parse::<f64>() {
|
|
|
|
state.delay = sleep_float;
|
|
|
|
state.text = text.to_string();
|
2024-03-20 04:52:02 +00:00
|
|
|
state.stores_item = true;
|
|
|
|
state.is_choice = false;
|
2024-03-20 03:54:39 +00:00
|
|
|
} else {
|
|
|
|
error!("The 'sleep' value for this message is not a float: {}", line);
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
2024-03-20 05:36:55 +00:00
|
|
|
["msg", sleep, label, goto, text] => {
|
|
|
|
debug!("Registering message (sleep={}): {}", sleep, text);
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 05:36:55 +00:00
|
|
|
if let Ok(sleep_float) = sleep.parse::<f64>() {
|
|
|
|
state.delay = sleep_float;
|
|
|
|
state.text = text.to_string();
|
|
|
|
state.stores_item = true;
|
|
|
|
state.is_choice = false;
|
|
|
|
state.goto = goto.to_string();
|
|
|
|
state.label = label.to_string();
|
|
|
|
} else {
|
|
|
|
error!("The 'sleep' value for this message is not a float: {}", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
["choice", sleep, text] => {
|
2024-03-20 04:52:02 +00:00
|
|
|
debug!("Registering choice (sleep={}): {}", sleep, text);
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 03:54:39 +00:00
|
|
|
if let Ok(sleep_float) = sleep.parse::<f64>() {
|
|
|
|
state.delay = sleep_float;
|
|
|
|
state.text = text.to_string();
|
2024-03-20 04:52:02 +00:00
|
|
|
state.stores_item = true;
|
|
|
|
state.is_choice = true;
|
2024-03-20 03:54:39 +00:00
|
|
|
} else {
|
|
|
|
error!("The 'sleep' value for this message is not a float: {}", line);
|
2024-03-20 05:36:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
["choice", sleep, label, goto, text] => {
|
|
|
|
debug!("Registering choice (sleep={}): {}", sleep, text);
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 05:36:55 +00:00
|
|
|
if let Ok(sleep_float) = sleep.parse::<f64>() {
|
|
|
|
state.delay = sleep_float;
|
|
|
|
state.text = text.to_string();
|
|
|
|
state.stores_item = true;
|
|
|
|
state.is_choice = true;
|
|
|
|
state.goto = goto.to_string();
|
|
|
|
state.label = label.to_string();
|
|
|
|
} else {
|
|
|
|
error!("The 'sleep' value for this message is not a float: {}", line);
|
2024-03-20 03:54:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
["goto", label] => {
|
2024-03-20 03:54:39 +00:00
|
|
|
debug!("Registering goto: {}", label);
|
|
|
|
state.goto = label.to_string();
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
["label", label] => {
|
2024-03-20 03:54:39 +00:00
|
|
|
debug!("Registering label: {}", label);
|
|
|
|
state.label = label.to_string();
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
["lvl", level] => {
|
2024-03-20 04:52:02 +00:00
|
|
|
debug!("Registering level: {}", level);
|
2024-03-20 03:54:39 +00:00
|
|
|
state.level = level.to_string();
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
2024-03-23 19:13:37 +00:00
|
|
|
["script", scriptname, parameter] => {
|
|
|
|
state.script = scriptname.to_string();
|
|
|
|
state.script_parameter = parameter.to_string();
|
2024-03-23 19:49:48 +00:00
|
|
|
state.script_parameter2 = "".to_string();
|
|
|
|
}
|
|
|
|
["script", scriptname, parameter, parameter2] => {
|
|
|
|
state.script = scriptname.to_string();
|
|
|
|
state.script_parameter = parameter.to_string();
|
|
|
|
state.script_parameter2 = parameter2.to_string();
|
2024-03-23 19:13:37 +00:00
|
|
|
}
|
2024-03-20 03:34:09 +00:00
|
|
|
_ => {
|
|
|
|
error!("No match for [{}]", parts.join(","));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:37:35 +00:00
|
|
|
state.spawn_entities(&mut commands, &asset_server);
|
2024-03-20 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 19:58:16 +00:00
|
|
|
//pub fn swap_world_on_ar_toggle(
|
|
|
|
// asset_server: Res<AssetServer>,
|
|
|
|
// mut images: ResMut<Assets<Image>>,
|
|
|
|
// mut worldstate: ResMut<WorldState>,
|
|
|
|
// mut skyboxes: Query<&mut Skybox>,
|
|
|
|
// settings: Res<settings::Settings>,
|
|
|
|
//) {
|
|
|
|
// if settings.hud_active != worldstate.entities_viewn_through_ar {
|
|
|
|
// worldstate.is_loaded = false;
|
|
|
|
// worldstate.entities_viewn_through_ar = settings.hud_active;
|
|
|
|
// }
|
|
|
|
// if !worldstate.is_loaded && asset_server.load_state(&worldstate.get_cubemap_handle()) == LoadState::Loaded {
|
|
|
|
// let cubemap_handle = &worldstate.get_cubemap_handle();
|
|
|
|
// let image = images.get_mut(cubemap_handle).unwrap();
|
|
|
|
// if image.texture_descriptor.array_layer_count() == 1 {
|
|
|
|
// image.reinterpret_stacked_2d_as_array(image.height() / image.width());
|
|
|
|
// image.texture_view_descriptor = Some(TextureViewDescriptor {
|
|
|
|
// dimension: Some(TextureViewDimension::Cube),
|
|
|
|
// ..default()
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for mut skybox in &mut skyboxes {
|
|
|
|
// skybox.image = cubemap_handle.clone();
|
|
|
|
// skybox.brightness = if settings.hud_active {
|
|
|
|
// SKYBOX_BRIGHTNESS_AR
|
|
|
|
// } else {
|
|
|
|
// SKYBOX_BRIGHTNESS
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// worldstate.is_loaded = true;
|
|
|
|
// }
|
|
|
|
//}
|