From a1910c40751805af09161acb9cd9575dc2ba6dcb Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 22 Apr 2024 21:01:27 +0200 Subject: [PATCH] move model loading code into skeleton --- src/commands.rs | 30 ++++++++--------- src/hud.rs | 8 ++--- src/main.rs | 2 ++ src/skeleton.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++- src/world.rs | 47 +++++--------------------- 5 files changed, 118 insertions(+), 59 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 6478e31..a869241 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -14,7 +14,7 @@ extern crate regex; use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; use bevy::math::DVec3; -use crate::{actor, camera, chat, hud, nature, shading, world}; +use crate::{actor, camera, chat, hud, nature, shading, skeleton, world}; use regex::Regex; use std::f32::consts::PI; use std::f64::consts::PI as PI64; @@ -539,14 +539,14 @@ fn spawn_entities( ..default() }); } else if let Some(model) = &state.model { - actor.insert(SceneBundle { + actor.insert(SpatialBundle { transform: Transform { scale: Vec3::splat(state.model_scale), ..default() }, - scene: asset_server.load(world::asset_name_to_path(model.as_str())), ..default() }); + skeleton::load(model.as_str(), &mut actor, &*asset_server); } // Physics Parameters @@ -679,30 +679,30 @@ fn spawn_entities( } if let Some(ar_asset_name) = &state.ar_model { - commands.spawn(( + let mut entitycmd = commands.spawn(( hud::AugmentedRealityOverlay { owner: actor_entity, }, world::DespawnOnPlayerDeath, - SceneBundle { - scene: asset_server.load(world::asset_name_to_path(ar_asset_name)), + SpatialBundle { visibility: Visibility::Hidden, ..default() }, )); + skeleton::load(ar_asset_name, &mut entitycmd, &*asset_server); } if state.is_point_of_interest { - commands.spawn(( - hud::PointOfInterestMarker(actor_entity), - world::DespawnOnPlayerDeath, - hud::ToggleableHudElement, - SceneBundle { - scene: asset_server.load(world::asset_name_to_path("point_of_interest")), - visibility: Visibility::Hidden, - ..default() - }, + let mut entitycmd = commands.spawn(( + hud::PointOfInterestMarker(actor_entity), + world::DespawnOnPlayerDeath, + hud::ToggleableHudElement, + SpatialBundle { + visibility: Visibility::Hidden, + ..default() + }, )); + skeleton::load("point_of_interest", &mut entitycmd, &*asset_server); } if state.has_ring { diff --git a/src/hud.rs b/src/hud.rs index e72b39c..b338c26 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -8,7 +8,7 @@ // + + + ███ // + ▀████████████████████████████████████████████████████▀ -use crate::{actor, audio, camera, chat, nature, var, world}; +use crate::{actor, audio, camera, chat, nature, skeleton, var, world}; use bevy::prelude::*; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use bevy::transform::TransformSystem; @@ -356,16 +356,16 @@ fn setup( }); // Selectagon - commands.spawn(( + let mut entitycmd = commands.spawn(( Selectagon, ToggleableHudElement, OnlyHideWhenTogglingHud, - SceneBundle { - scene: asset_server.load(world::asset_name_to_path("selectagon")), + SpatialBundle { visibility: Visibility::Hidden, ..default() }, )); + skeleton::load("selectagon", &mut entitycmd, &*asset_server); // AR-related things ambient_light.brightness = if settings.hud_active { diff --git a/src/main.rs b/src/main.rs index 8767f34..3263433 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod commands; mod effects; mod hud; mod shading; +mod skeleton; mod var; mod world; @@ -73,6 +74,7 @@ impl Plugin for OutFlyPlugin { effects::EffectsPlugin, hud::HudPlugin, shading::ShadingPlugin, + skeleton::SkeletonPlugin, world::WorldPlugin, )); } diff --git a/src/skeleton.rs b/src/skeleton.rs index b8fc1c8..5478b8a 100644 --- a/src/skeleton.rs +++ b/src/skeleton.rs @@ -8,11 +8,99 @@ // + + + ███ // + ▀████████████████████████████████████████████████████▀ +use bevy::ecs::system::EntityCommands; use bevy::prelude::*; -use bevy_xpbd_3d::prelude::*; + +pub fn asset_name_to_path(name: &str) -> &'static str { + match name { + "suit" => "models/suit_with_collider.glb#Scene0", + "suit_ar_chefhat" => "models/suit_ar_chefhat.glb#Scene0", + "asteroid1" => "models/asteroid.glb#Scene0", + "asteroid2" => "models/asteroid2.glb#Scene0", + "asteroid_lum" => "models/asteroid_lum.glb#Scene0", + "moonlet" => "models/moonlet.glb#Scene0", + "monolith" => "models/monolith_neon.glb#Scene0", + "lightorb" => "models/lightorb.glb#Scene0", + "orb_busstop" => "models/orb_busstop.glb#Scene0", + "orb_busstop_dim" => "models/orb_busstop_dim.glb#Scene0", + "MeteorAceGT" => "models/MeteorAceGT.glb#Scene0", + "satellite" => "models/satellite.glb#Scene0", + "pizzeria" => "models/pizzeria2.glb#Scene0", + "pizzasign" => "models/pizzasign.glb#Scene0", + "selectagon" => "models/selectagon.glb#Scene0", + "orbitring" => "models/orbitring.glb#Scene0", + "clippy" => "models/clippy.glb#Scene0", + "clippy_ar" => "models/clippy_ar.glb#Scene0", + "whale" => "models/whale.glb#Scene0", + "point_of_interest" => "models/point_of_interest.glb#Scene0", + _ => "models/error.glb#Scene0", + } +} + +pub fn skeleton_name_to_skeletondef(_name: &str) -> Option { + return None; +} pub struct SkeletonPlugin; impl Plugin for SkeletonPlugin { fn build(&self, app: &mut App) { + app.add_systems(Update, animate_skeleton_parts); } } + +#[derive(Component)] pub struct SkeletonLimb; +pub struct SkeletonDef; + +//pub fn spawn( +// name: &str, +// commands: Commands, +// asset_server: &AssetServer, +//) -> EntityCommands { +// let mut entity_commands = commands.spawn_empty(); +// load(name, &mut entity_commands, asset_server); +// return entity_commands; +//} + +pub fn load( + name: &str, + entity_commands: &mut EntityCommands, + asset_server: &AssetServer, +) { + let skeletondef = skeleton_name_to_skeletondef(name); + if skeletondef.is_none() { + entity_commands.insert(load_scene_by_path(asset_name_to_path(name), asset_server)); + } + else { + //entity_commands.insert(); + } +} + +//pub fn load_scene( +// path: &str, +// asset_server: &AssetServer +//) -> Handle { +// load_scene_by_path(asset_name_to_path(path), asset_server) +//} + +#[inline] +pub fn load_scene_by_path( + path: &str, + asset_server: &AssetServer +) -> Handle { + let path_string = path.to_string(); + if let Some(handle) = asset_server.get_handle(&path_string) { + handle + } else { + asset_server.load(&path_string) + } +} + +pub fn _build_body( + _name: String, + mut _entity_commands: EntityCommands, +) { +} + +pub fn animate_skeleton_parts( +) { +} diff --git a/src/world.rs b/src/world.rs index 47fcd54..f63889f 100644 --- a/src/world.rs +++ b/src/world.rs @@ -8,7 +8,7 @@ // + + + ███ // + ▀████████████████████████████████████████████████████▀ -use crate::{actor, audio, hud, nature, shading, var}; +use crate::{actor, audio, hud, nature, shading, skeleton, var}; use bevy::prelude::*; use bevy::math::{DVec3, I64Vec3}; use bevy::scene::{InstanceId, SceneInstance}; @@ -30,33 +30,8 @@ const SKYBOX: bool = false; const ASTEROID_SPAWN_STEP: f64 = 500.0; const ASTEROID_VIEW_RADIUS: f64 = 3000.0; -const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0"; -const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0"; -pub fn asset_name_to_path(name: &str) -> &'static str { - match name { - "suit" => "models/suit_with_collider.glb#Scene0", - "suit_ar_chefhat" => "models/suit_ar_chefhat.glb#Scene0", - "asteroid1" => ASSET_ASTEROID1, - "asteroid2" => ASSET_ASTEROID2, - "asteroid_lum" => "models/asteroid_lum.glb#Scene0", - "moonlet" => "models/moonlet.glb#Scene0", - "monolith" => "models/monolith_neon.glb#Scene0", - "lightorb" => "models/lightorb.glb#Scene0", - "orb_busstop" => "models/orb_busstop.glb#Scene0", - "orb_busstop_dim" => "models/orb_busstop_dim.glb#Scene0", - "MeteorAceGT" => "models/MeteorAceGT.glb#Scene0", - "satellite" => "models/satellite.glb#Scene0", - "pizzeria" => "models/pizzeria2.glb#Scene0", - "pizzasign" => "models/pizzasign.glb#Scene0", - "selectagon" => "models/selectagon.glb#Scene0", - "orbitring" => "models/orbitring.glb#Scene0", - "clippy" => "models/clippy.glb#Scene0", - "clippy_ar" => "models/clippy_ar.glb#Scene0", - "whale" => "models/whale.glb#Scene0", - "point_of_interest" => "models/point_of_interest.glb#Scene0", - _ => "models/error.glb#Scene0", - } -} +const ASSET_NAME_ASTEROID1: &str = "asteroid1"; +const ASSET_NAME_ASTEROID2: &str = "asteroid2"; pub struct WorldPlugin; impl Plugin for WorldPlugin { @@ -115,12 +90,7 @@ pub fn setup( mut meshes: ResMut>, mut materials: ResMut>, mut materials_skybox: ResMut>, - asset_server: Res, ) { - // Load assets - commands.insert_resource(AsteroidModel1(asset_server.load(ASSET_ASTEROID1))); - commands.insert_resource(AsteroidModel2(asset_server.load(ASSET_ASTEROID2))); - // Generate starmap let sphere_handle = meshes.add(Sphere::new(1.0).mesh().uv(16, 16)); let mut starcount = 0; @@ -215,11 +185,10 @@ fn spawn_despawn_asteroids( q_player: Query<&Position, With>, mut ew_despawn: EventWriter, mut db: ResMut, - asteroid1_handle: Res, - asteroid2_handle: Res, mut q_asteroid: Query<(Entity, &SceneInstance), With>, mut last_player_cell: Local, id2pos: Res, + asset_server: Res, ) { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { //if q_player.is_empty() { @@ -356,17 +325,17 @@ fn spawn_despawn_asteroids( DespawnOnPlayerDeath, )); let model = match class { - 0 => asteroid1_handle.0.clone(), - _ => asteroid2_handle.0.clone(), + 0 => ASSET_NAME_ASTEROID1, + _ => ASSET_NAME_ASTEROID2, }; - entity_commands.insert(SceneBundle { - scene: model, + entity_commands.insert(SpatialBundle { transform: Transform { scale: Vec3::splat(size), ..default() }, ..default() }); + skeleton::load(model, &mut entity_commands, &*asset_server); db.0.insert(origin, AsteroidData { entity: entity_commands.id(), //viewdistance: 99999999.0,