move model loading code into skeleton

This commit is contained in:
yuni 2024-04-22 21:01:27 +02:00
parent a12ffac841
commit a1910c4075
5 changed files with 118 additions and 59 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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,
));
}

View file

@ -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<SkeletonDef> {
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<Scene> {
// load_scene_by_path(asset_name_to_path(path), asset_server)
//}
#[inline]
pub fn load_scene_by_path(
path: &str,
asset_server: &AssetServer
) -> Handle<Scene> {
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(
) {
}

View file

@ -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<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut materials_skybox: ResMut<Assets<shading::SkyBox>>,
asset_server: Res<AssetServer>,
) {
// 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<actor::PlayerCamera>>,
mut ew_despawn: EventWriter<DespawnEvent>,
mut db: ResMut<ActiveAsteroids>,
asteroid1_handle: Res<AsteroidModel1>,
asteroid2_handle: Res<AsteroidModel2>,
mut q_asteroid: Query<(Entity, &SceneInstance), With<Asteroid>>,
mut last_player_cell: Local<I64Vec3>,
id2pos: Res<actor::Id2Pos>,
asset_server: Res<AssetServer>,
) {
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,