outfly/src/world.rs

617 lines
20 KiB
Rust
Raw Normal View History

2024-03-20 03:34:09 +00:00
extern crate regex;
2024-03-19 02:54:16 +00:00
use crate::{actor, camera, 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};
use bevy::pbr::CascadeShadowConfigBuilder;
2024-03-17 14:46:51 +00:00
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
use std::f32::consts::PI;
2024-03-16 20:44:51 +00:00
2024-03-19 21:16:30 +00:00
const JUPITER_SIZE: f32 = 80000.0;
2024-03-18 03:39:26 +00:00
const ASTEROID_SIZE: f32 = 100.0;
const MOON_SIZE: f32 = 200.0;
2024-03-18 03:39:26 +00:00
const MARS_SIZE: f32 = 10.0;
const ASTRONAUT_SIZE: f32 = 5.0;
2024-03-19 17:15:19 +00:00
const PIZZERIA_SIZE: f32 = 30.0;
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 17:27:13 +00:00
const ASSET_ASTRONAUT: &str = "external/alien.glb#Scene0";
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-19 17:15:19 +00:00
const ASSET_PIZZERIA: &str = "models/pizzeria.glb#Scene0";
2024-03-19 21:16:30 +00:00
const ASSET_JUPITER: &str = "models/jupiter.glb#Scene0";
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);
app.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)));
2024-03-17 23:04:23 +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-17 22:49:50 +00:00
// Add player
commands.spawn((
actor::Player,
2024-03-19 15:14:12 +00:00
actor::Actor {
angular_momentum: Quat::IDENTITY,
..default()
},
2024-03-17 22:49:50 +00:00
actor::LifeForm::default(),
actor::Suit {
oxygen: nature::OXY_H,
integrity: 0.3,
2024-03-17 22:49:50 +00:00
..default()
},
2024-03-16 20:44:51 +00:00
Camera3dBundle {
2024-03-17 13:16:25 +00:00
camera: Camera {
hdr: true, // HDR is required for bloom
..default()
},
2024-03-16 20:44:51 +00:00
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
camera::CameraController::default(),
2024-03-18 19:58:16 +00:00
// Skybox {
// image: cubemap_handle,
// brightness: SKYBOX_BRIGHTNESS,
// },
2024-03-17 13:16:25 +00:00
BloomSettings {
2024-03-17 13:29:33 +00:00
composite_mode: BloomCompositeMode::EnergyConserving,
2024-03-17 13:16:25 +00:00
..default()
},
2024-03-16 20:44:51 +00:00
));
2024-03-16 22:11:56 +00:00
2024-03-18 03:39:26 +00:00
// Add some hand-placed asteroids
let sphere_handle = meshes.add(Sphere::new(1.0));
2024-03-17 01:00:41 +00:00
let gray_handle = materials.add(StandardMaterial {
base_color: Color::GRAY,
2024-03-16 22:11:56 +00:00
perceptual_roughness: 1.0,
..default()
});
2024-03-17 01:00:41 +00:00
let brown_handle = materials.add(StandardMaterial {
2024-03-17 00:23:35 +00:00
base_color: Color::Rgba { alpha: 1.0, red: 0.8, green: 0.5, blue: 0.1 },
2024-03-17 00:05:03 +00:00
perceptual_roughness: 1.0,
..default()
});
2024-03-18 03:39:26 +00:00
commands.spawn((
actor::Actor::default(),
PbrBundle {
mesh: sphere_handle.clone(),
material: gray_handle.clone(),
transform: Transform::from_xyz(
2000.0,
2024-03-18 03:39:26 +00:00
0.0,
0.0,
).with_scale(Vec3::splat(MOON_SIZE)),
..default()
},
));
commands.spawn((
actor::Actor::default(),
PbrBundle {
mesh: sphere_handle.clone(),
material: brown_handle.clone(),
transform: Transform::from_xyz(
300.0,
40.0,
250.0,
).with_scale(Vec3::splat(MARS_SIZE)),
..default()
},
));
2024-03-18 03:39:26 +00:00
// Generate a bunch of asteriods
let maxdist = 10;
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,
};
commands.spawn((
2024-03-19 20:09:20 +00:00
actor::Actor {
v: Vec3::new(-0.00, 0.0, 0.35),
2024-03-19 22:50:11 +00:00
angular_momentum: Quat::from_euler(EulerRot::XYZ, 0.001, 0.001, 0.0003),
2024-03-19 20:09:20 +00:00
..default()
},
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),
..default()
2024-03-19 20:09:20 +00:00
},
));
}
}
}
// Generate starmap
2024-03-19 02:54:16 +00:00
for star in nature::STARS {
let brightness = star[3] * 2000.0;
let (r, g, b) = nature::star_color_index_to_rgb(star[4]);
let star_color_handle = materials.add(StandardMaterial {
base_color: Color::rgb(r, g, b),
emissive: Color::rgb_linear(r*brightness, g*brightness, b*brightness),
..default()
});
let dist = 1e6;
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],
)
.with_scale(Vec3::splat((1000.0*star[3]).clamp(500.0, 2000.0))),
..default()
}
));
}
2024-03-17 13:16:25 +00:00
2024-03-17 00:25:51 +00:00
// Add alien
commands.spawn((
2024-03-19 15:18:52 +00:00
actor::Actor {
2024-03-19 17:15:19 +00:00
v: Vec3::new(-0.05, 0.2, 0.35),
2024-03-19 15:18:52 +00:00
..default()
},
2024-03-19 17:15:19 +00:00
actor::Talker { conv_id: "hialien".to_string() },
SceneBundle {
2024-03-17 00:25:51 +00:00
transform: Transform {
translation: Vec3::new(
20.0,
0.0,
2024-03-17 00:25:51 +00:00
0.0,
),
rotation: Quat::from_rotation_y(-PI / 3.),
2024-03-18 03:39:26 +00:00
scale: Vec3::splat(ASTRONAUT_SIZE),
2024-03-17 00:25:51 +00:00
},
2024-03-18 03:39:26 +00:00
scene: asset_server.load(ASSET_ASTRONAUT),
2024-03-17 00:25:51 +00:00
..default()
},
));
2024-03-17 00:25:51 +00:00
2024-03-19 17:15:19 +00:00
// Add pizza alien
commands.spawn((
actor::Actor {
v: Vec3::new(-0.05, 0.2, 0.35),
angular_momentum: Quat::from_euler(EulerRot::XYZ, 0.0, 0.0001, 0.0),
..default()
},
actor::Talker { conv_id: "pizzeria".to_string() },
SceneBundle {
transform: Transform {
translation: Vec3::new(
2024-03-19 17:27:13 +00:00
-2265.0,
2024-03-19 17:15:19 +00:00
10.0,
0.0,
),
rotation: Quat::from_rotation_y(-PI / 3.),
scale: Vec3::splat(ASTRONAUT_SIZE),
},
scene: asset_server.load(ASSET_ASTRONAUT),
..default()
},
));
// Add pizza place
commands.spawn((
actor::Actor {
v: Vec3::new(0.0, 0.0, 0.0),
angular_momentum: Quat::from_euler(EulerRot::XYZ, 0.0, 0.0001, 0.0),
..default()
},
SceneBundle {
transform: Transform {
translation: Vec3::new(
2024-03-19 17:27:13 +00:00
-2300.0,
2024-03-19 17:15:19 +00:00
10.0,
0.0,
),
rotation: Quat::from_rotation_y(-2.0 * -PI / 2.),
scale: Vec3::splat(PIZZERIA_SIZE),
},
scene: asset_server.load(ASSET_PIZZERIA),
..default()
},
));
2024-03-19 21:16:30 +00:00
commands.spawn((
actor::Actor {
angular_momentum: Quat::from_euler(EulerRot::XYZ, 0.0, 0.0001, 0.0),
..default()
},
SceneBundle {
transform: Transform {
translation: Vec3::new(
300000.0,
0.0,
500000.0,
),
2024-03-19 21:29:27 +00:00
rotation: Quat::from_rotation_y(PI * -1.40),
2024-03-19 21:16:30 +00:00
scale: Vec3::splat(JUPITER_SIZE),
},
scene: asset_server.load(ASSET_JUPITER),
..default()
},
));
2024-03-19 17:15:19 +00:00
// Add Light from the Sun
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
2024-03-16 22:44:46 +00:00
illuminance: 1000.0,
shadows_enabled: false,
..default()
},
transform: Transform::from_rotation(Quat::from_rotation_y(PI/2.0)),
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
}
#[derive(Debug)]
enum DefClass {
Actor,
Chat,
None,
}
#[derive(Debug)]
2024-03-20 03:54:39 +00:00
struct ParserState {
class: DefClass,
// Generic fields
2024-03-20 03:54:39 +00:00
name: String,
chat: String,
// Actor fields
pos: Vec3,
model: String,
model_scale: f32,
pronoun: String,
is_lifeform: bool,
is_alive: bool,
is_suited: bool,
// Chat fields
2024-03-20 03:54:39 +00:00
delay: f64,
text: String,
level: String,
label: String,
goto: String,
is_choice: bool,
stores_item: bool,
2024-03-20 03:54:39 +00:00
}
impl Default for ParserState {
fn default() -> Self {
Self {
class: DefClass::None,
2024-03-20 03:54:39 +00:00
name: "".to_string(),
chat: "".to_string(),
pos: Vec3::new(0.0, 0.0, 0.0),
model: "".to_string(),
model_scale: 1.0,
pronoun: "they/them".to_string(),
is_lifeform: false,
is_alive: false,
is_suited: false,
2024-03-20 03:54:39 +00:00
delay: 0.0,
text: "".to_string(),
level: "chat".to_string(),
2024-03-20 03:54:39 +00:00
label: "".to_string(),
goto: "".to_string(),
is_choice: false,
stores_item: false,
}
}
}
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;
}
fn reset(&mut self) {
*self = Self::default();
}
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(),
sound: if self.is_choice { "".to_string() } else { "chat".to_string() },
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(),
}
}
fn spawn_chat(&mut self, commands: &mut Commands) {
if self.stores_item {
2024-03-20 05:02:06 +00:00
debug!("{:#?}", self.as_chatbranch());
commands.spawn(self.as_chatbranch());
2024-03-20 03:54:39 +00:00
}
self.reset_message();
2024-03-20 03:54:39 +00:00
}
fn spawn_actor(&mut self, commands: &mut Commands) {
let component_actor = actor::Actor::default();
let component_lifeform = actor::LifeForm::default();
let component_suit = actor::Suit::default();
commands.spawn((component_actor, component_lifeform, component_suit));
self.reset();
}
fn spawn_entities(&mut self, commands: &mut Commands) {
match self.class {
DefClass::Actor => { self.spawn_actor(commands); }
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(
mut commands: Commands,
2024-03-20 03:34:09 +00:00
) {
let re1 = Regex::new(r"^\s*([a-z]+)\s+(.*)$").unwrap();
let re2 = Regex::new("\"([^\"]*)\"|([0-9]+(?:\\.[0-9]+)?)").unwrap();
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 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 05:02:06 +00:00
warn!("Syntax Error in definitions line {}: `{}`", line_nr, line);
2024-03-20 03:34:09 +00:00
}
continue;
}
let command = caps.unwrap().get(1).unwrap().as_str();
let mut parts: Vec<&str> = Vec::new();
parts.push(command);
for caps in re2.captures_iter(line) {
if let Some(part) = caps.get(1) {
parts.push(&part.as_str());
}
if let Some(part) = caps.get(2) {
parts.push(&part.as_str());
}
}
match parts.as_slice() {
// Parsing actors
["actor", x, y, z, model] => {
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;
}
}
["alive", "yes"] => {
state.is_alive = true;
state.is_lifeform = true;
}
["suited", "yes"] => {
state.is_suited = true;
}
["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;
}
}
// 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);
state.class = DefClass::Chat;
state.spawn_entities(&mut commands);
state.reset_chat();
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] => {
debug!("Registering message (sleep={}): {}", sleep, text);
state.spawn_entities(&mut commands);
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();
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);
state.spawn_entities(&mut commands);
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] => {
debug!("Registering choice (sleep={}): {}", sleep, text);
state.spawn_entities(&mut commands);
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();
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);
state.spawn_entities(&mut commands);
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] => {
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
}
_ => {
error!("No match for [{}]", parts.join(","));
}
}
}
state.spawn_entities(&mut commands);
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;
// }
//}