more asset loader usage

This commit is contained in:
2025-03-26 08:32:40 +01:00
parent 7dd9999860
commit 91e5435c64
4 changed files with 34 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
use crate::{ use crate::{
GameState, billboards::Billboard, player::Player, sounds::PlaySound, GameState, billboards::Billboard, loading_assets::GameAssets, player::Player,
squish_animation::SquishAnimation, sounds::PlaySound, squish_animation::SquishAnimation,
}; };
use avian3d::prelude::*; use avian3d::prelude::*;
use bevy::prelude::*; use bevy::prelude::*;
@@ -21,14 +21,12 @@ pub fn plugin(app: &mut App) {
app.add_observer(on_spawn); app.add_observer(on_spawn);
} }
fn on_spawn(trigger: Trigger<KeySpawn>, mut commands: Commands, asset_server: Res<AssetServer>) { fn on_spawn(trigger: Trigger<KeySpawn>, mut commands: Commands, assets: Res<GameAssets>) {
let KeySpawn(position, id) = trigger.event(); let KeySpawn(position, id) = trigger.event();
let angle = rand::random::<f32>() * PI * 2.; let angle = rand::random::<f32>() * PI * 2.;
let spawn_dir = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize(); let spawn_dir = Quat::from_rotation_y(angle) * Vec3::new(0.5, 0.6, 0.).normalize();
let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/key.glb"));
commands commands
.spawn(( .spawn((
Name::new("key"), Name::new("key"),
@@ -41,7 +39,11 @@ fn on_spawn(trigger: Trigger<KeySpawn>, mut commands: Commands, asset_server: Re
RigidBody::Dynamic, RigidBody::Dynamic,
Restitution::new(0.6), Restitution::new(0.6),
)) ))
.with_child((Billboard, SquishAnimation(2.6), SceneRoot(mesh))); .with_child((
Billboard,
SquishAnimation(2.6),
SceneRoot(assets.mesh_key.clone()),
));
} }
fn collect_key( fn collect_key(

View File

@@ -42,6 +42,18 @@ pub struct UIAssets {
pub struct GameAssets { pub struct GameAssets {
#[asset(path = "textures/fx/impact.png")] #[asset(path = "textures/fx/impact.png")]
pub impact_atlas: Handle<Image>, pub impact_atlas: Handle<Image>,
#[asset(path = "models/key.glb#Scene0")]
pub mesh_key: Handle<Scene>,
#[asset(path = "models/spawn.glb#Scene0")]
pub mesh_spawn: Handle<Scene>,
#[asset(path = "models/alien_naked.glb#Scene0")]
pub mesh_alien: Handle<Scene>,
#[asset(path = "models/cash.glb#Scene0")]
pub mesh_cash: Handle<Scene>,
} }
pub struct LoadingPlugin; pub struct LoadingPlugin;

View File

@@ -1,6 +1,6 @@
use crate::{ use crate::{
GameState, GameState,
alien::{ALIEN_ASSET_PATH, Animations}, alien::Animations,
camera::{CameraArmRotation, CameraTarget}, camera::{CameraArmRotation, CameraTarget},
cash::{Cash, CashCollectEvent}, cash::{Cash, CashCollectEvent},
control::{ control::{
@@ -8,6 +8,7 @@ use crate::{
controller::{CharacterControllerBundle, PlayerMovement}, controller::{CharacterControllerBundle, PlayerMovement},
}, },
heads_ui::HeadChanged, heads_ui::HeadChanged,
loading_assets::GameAssets,
physics_layers::GameLayer, physics_layers::GameLayer,
tb_entities::SpawnPoint, tb_entities::SpawnPoint,
}; };
@@ -68,6 +69,7 @@ fn spawn(
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
query: Query<&Transform, With<SpawnPoint>>, query: Query<&Transform, With<SpawnPoint>>,
mut player_spawned: ResMut<PlayerSpawned>, mut player_spawned: ResMut<PlayerSpawned>,
assets: Res<GameAssets>,
) { ) {
if player_spawned.spawned { if player_spawned.spawned {
return; return;
@@ -114,9 +116,7 @@ fn spawn(
Transform::from_translation(Vec3::new(0., -1.45, 0.)) Transform::from_translation(Vec3::new(0., -1.45, 0.))
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI)) .with_rotation(Quat::from_rotation_y(std::f32::consts::PI))
.with_scale(Vec3::splat(1.4)), .with_scale(Vec3::splat(1.4)),
SceneRoot( SceneRoot(assets.mesh_alien.clone()),
asset_server.load(GltfAssetLabel::Scene(0).from_asset(ALIEN_ASSET_PATH)),
),
)) ))
.with_child(( .with_child((
Name::from("head"), Name::from("head"),

View File

@@ -6,6 +6,7 @@ use bevy_trenchbroom::class::Target;
use bevy_trenchbroom::prelude::*; use bevy_trenchbroom::prelude::*;
use crate::cash::Cash; use crate::cash::Cash;
use crate::loading_assets::GameAssets;
use crate::physics_layers::GameLayer; use crate::physics_layers::GameLayer;
#[derive(PointClass, Component, Reflect, Default)] #[derive(PointClass, Component, Reflect, Default)]
@@ -17,7 +18,7 @@ pub struct SpawnPoint {}
impl SpawnPoint { impl SpawnPoint {
fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) { fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
let Some(asset_server) = world.get_resource::<AssetServer>() else { let Some(assets) = world.get_resource::<GameAssets>() else {
return; return;
}; };
@@ -30,7 +31,7 @@ impl SpawnPoint {
let mut this_transform = *this_transform; let mut this_transform = *this_transform;
this_transform.translation += Vec3::new(0., -0.3, 0.); this_transform.translation += Vec3::new(0., -0.3, 0.);
let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/spawn.glb")); let mesh = assets.mesh_spawn.clone();
world.commands().entity(entity).insert(( world.commands().entity(entity).insert((
this_transform, this_transform,
@@ -125,6 +126,10 @@ impl EnemySpawn {
return; return;
}; };
let Some(assets) = world.get_resource::<GameAssets>() else {
return;
};
let this = world.get_entity(entity).unwrap().get::<Self>().unwrap(); let this = world.get_entity(entity).unwrap().get::<Self>().unwrap();
let this_transform = world let this_transform = world
.get_entity(entity) .get_entity(entity)
@@ -135,7 +140,7 @@ impl EnemySpawn {
let mut this_transform = *this_transform; let mut this_transform = *this_transform;
this_transform.translation += Vec3::new(0., 1., 0.); this_transform.translation += Vec3::new(0., 1., 0.);
let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/alien_naked.glb")); let mesh = assets.mesh_alien.clone();
let head_mesh = asset_server let head_mesh = asset_server
.load(GltfAssetLabel::Scene(0).from_asset(format!("models/heads/{}.glb", this.head))); .load(GltfAssetLabel::Scene(0).from_asset(format!("models/heads/{}.glb", this.head)));
@@ -179,11 +184,11 @@ pub struct CashSpawn {}
impl CashSpawn { impl CashSpawn {
fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) { fn on_add(mut world: DeferredWorld, entity: Entity, _id: ComponentId) {
let Some(asset_server) = world.get_resource::<AssetServer>() else { let Some(assets) = world.get_resource::<GameAssets>() else {
return; return;
}; };
let mesh = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/cash.glb")); let mesh = assets.mesh_cash.clone();
world.commands().entity(entity).insert(( world.commands().entity(entity).insert((
Name::new("cash"), Name::new("cash"),