implement scene loader (transformations are still a bit wonky)

This commit is contained in:
yuni 2024-06-11 05:49:23 +02:00
parent a3661cc43f
commit f2246a247f
2 changed files with 423 additions and 324 deletions

View file

@ -27,13 +27,22 @@ pub struct CmdPlugin;
impl Plugin for CmdPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, load_defs);
app.add_systems(
Update,
handle_spawn_events
.before(spawn_entities)
.before(spawn_scenes),
);
app.add_systems(Update, spawn_entities);
app.add_systems(Update, spawn_scenes.after(spawn_entities));
app.add_systems(Update, process_mesh);
app.add_systems(
PreUpdate,
hide_colliders.run_if(any_with_component::<NeedsSceneColliderRemoved>),
);
app.add_event::<SpawnEvent>();
app.add_event::<SpawnActorEvent>();
app.add_event::<SpawnSceneEvent>();
}
}
@ -41,9 +50,14 @@ impl Plugin for CmdPlugin {
pub struct NeedsSceneColliderRemoved;
#[derive(Event)]
pub struct SpawnEvent(ParserState);
#[derive(Event)]
pub struct SpawnActorEvent(ParserState);
#[derive(Event)]
pub struct SpawnSceneEvent(ParserState);
#[derive(PartialEq, Clone)]
enum DefClass {
Actor,
Scene,
None,
}
@ -347,6 +361,21 @@ pub fn load_defs(mut ew_spawn: EventWriter<SpawnEvent>) {
continue;
}
}
["scene", x, y, z, name] => {
ew_spawn.send(SpawnEvent(state));
state = ParserState::default();
state.class = DefClass::Scene;
state.name = Some(name.to_string());
if let (Ok(x_float), Ok(y_float), Ok(z_float)) =
(x.parse::<f64>(), y.parse::<f64>(), z.parse::<f64>())
{
state.pos = DVec3::new(x_float, y_float, z_float);
} else {
error!("Can't parse coordinates as floats in def: {line}");
state = ParserState::default();
continue;
}
}
["relativeto", id] => {
state.relative_to = Some(id.to_string());
}
@ -646,8 +675,90 @@ pub fn load_defs(mut ew_spawn: EventWriter<SpawnEvent>) {
ew_spawn.send(SpawnEvent(state));
}
fn spawn_entities(
fn handle_spawn_events(
mut er_spawn: EventReader<SpawnEvent>,
mut ew_spawnscene: EventWriter<SpawnSceneEvent>,
mut ew_spawnactor: EventWriter<SpawnActorEvent>,
) {
for state in er_spawn.read() {
match state.0.class {
DefClass::Actor => {
ew_spawnactor.send(SpawnActorEvent(state.0.clone()));
}
DefClass::Scene => {
ew_spawnscene.send(SpawnSceneEvent(state.0.clone()));
}
DefClass::None => {}
}
}
}
fn spawn_scenes(
mut er_spawnscene: EventReader<SpawnSceneEvent>,
mut ew_spawn: EventWriter<SpawnEvent>,
) {
for state_wrapper in er_spawnscene.read() {
let root_state = &state_wrapper.0;
let scene_defs = include!("data/scenes.in");
for (name, template, pos, rot) in scene_defs {
if Some(name.to_string()) == root_state.name {
match template {
"cruiser" => {
let mut state = ParserState::default();
state.class = DefClass::Actor;
state.pos = DVec3::new(pos[0], -pos[2], pos[1]);
state.model = Some("cruiser".to_string());
state.rotation = Quat::from_euler(EulerRot::XYZ, rot[0], rot[1], rot[2]);
// command: relativeto ?
state.relative_to = root_state.relative_to.clone();
// command: name Cruiser
state.name = Some("Cruiser".to_string());
// command: scale 5
state.model_scale = 5.0;
// command: vehicle yes
state.is_vehicle = true;
// command: angularmomentum 0 0 0
state.angular_momentum = DVec3::ZERO;
// command: collider handcrafted
state.collider_is_one_mesh_of_scene = true;
// command: thrust 16 16 8 100000 3
state.thrust_forward = 16.0;
state.thrust_back = 16.0;
state.thrust_sideways = 8.0;
state.reaction_wheels = 100000.0;
state.warmup_seconds = 3.0;
// command: engine ion
state.engine_type = actor::EngineType::Ion;
// command: camdistance 50
state.camdistance = 50.0;
// command: density 500
state.density = 500.0;
// command: pointofinterest yes
state.is_point_of_interest = true;
ew_spawn.send(SpawnEvent(state));
}
_ => {}
}
}
}
}
}
fn spawn_entities(
mut er_spawn: EventReader<SpawnActorEvent>,
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
@ -667,7 +778,7 @@ fn spawn_entities(
};
let state = &state_wrapper.0;
let mut rotation = state.rotation;
if state.class == DefClass::Actor {
// Preprocessing
let mut absolute_pos = if let Some(id) = &state.relative_to {
match id2pos.0.get(&id.to_string()) {
@ -695,9 +806,8 @@ fn spawn_entities(
}
};
let orbital_period = nature::simple_orbital_period(mass, r);
phase_radians += if let Ok(epoch) =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
{
phase_radians +=
if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
let now = epoch.as_secs_f64() + 614533234154.0; // random
PI * 2.0 * (now % orbital_period) / orbital_period
} else {
@ -984,8 +1094,8 @@ fn spawn_entities(
}
if state.has_ring {
let ring_radius = state.model_scale
* (nature::JUPITER_RING_RADIUS / nature::JUPITER_RADIUS) as f32;
let ring_radius =
state.model_scale * (nature::JUPITER_RING_RADIUS / nature::JUPITER_RADIUS) as f32;
commands.spawn((
world::DespawnOnPlayerDeath,
MaterialMeshBundle {
@ -1006,7 +1116,6 @@ fn spawn_entities(
}
}
}
}
pub fn hide_colliders(
mut q_mesh: Query<(&mut Visibility, &Name), (Added<Visibility>, With<Handle<Mesh>>)>,

View file

@ -268,18 +268,8 @@ actor 0 59305 0 suitv2
health 0.3
rotationy 135
actor 10 -30 20 cruiser
name "Cruiser"
scene 10 -30 20 test
relativeto player
scale 5
vehicle yes
collider handcrafted
thrust 16 16 8 100000 3
engine ion
camdistance 50
density 500
angularmomentum 0.1 0.1 0.3
pointofinterest yes
actor -55e3 44e3 0 suitv2
template person