Head select (#4)

This commit is contained in:
extrawurst
2025-03-07 13:22:11 +01:00
committed by GitHub
parent 323e644ff4
commit 76e565e567
20 changed files with 252 additions and 8 deletions

View File

@@ -1,9 +1,11 @@
use std::time::Duration;
use crate::{
DebugVisuals,
alien::{ALIEN_ASSET_PATH, Animations},
camera::GameCameraRig,
cash::{Cash, CashCollectEvent},
heads_ui::HeadChanged,
tb_entities::SpawnPoint,
};
use avian3d::prelude::*;
@@ -22,6 +24,9 @@ pub struct Player;
#[derive(Component, Default)]
struct PlayerAnimations;
#[derive(Component, Default)]
struct PlayerHead;
#[derive(Resource, Default)]
struct PlayerSpawned {
spawned: bool,
@@ -45,6 +50,8 @@ pub fn plugin(app: &mut App) {
FixedUpdate,
apply_controls.in_set(TnuaUserControlsSystemSet),
);
app.add_observer(updaate_head);
}
fn spawn(
@@ -77,10 +84,11 @@ fn spawn(
Collider::capsule(1.2, 1.5),
LockedAxes::ROTATION_LOCKED,
TnuaController::default(),
TnuaAvian3dSensorShape(Collider::cylinder(1.0, 0.0)),
TnuaAvian3dSensorShape(Collider::cylinder(0.8, 0.0)),
))
.with_child((
Name::from("head"),
PlayerHead,
Transform::from_translation(Vec3::new(0., -0.5, 0.))
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI)),
SceneRoot(mesh),
@@ -93,8 +101,9 @@ fn spawn(
SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset(ALIEN_ASSET_PATH))),
));
commands.spawn(AudioPlayer::new(
asset_server.load("sfx/heads/angry demonstrator.ogg"),
commands.spawn((
AudioPlayer::new(asset_server.load("sfx/heads/angry demonstrator.ogg")),
PlaybackSettings::DESPAWN,
));
player_spawned.spawned = true;
@@ -164,7 +173,7 @@ fn apply_controls(
controller.basis(TnuaBuiltinWalk {
// The `desired_velocity` determines how the character will move.
desired_velocity: direction.normalize_or_zero() * 10.0,
desired_velocity: direction.normalize_or_zero() * 8.0,
// The `float_height` must be greater (even if by little) from the distance between the
// character's center and the lowest point of its collider.
float_height: 3.0,
@@ -185,11 +194,19 @@ fn apply_controls(
}
}
fn update_camera(player: Query<&Transform, With<Player>>, mut rig: Single<&mut Rig>) {
fn update_camera(
player: Query<&Transform, With<Player>>,
mut rig: Single<&mut Rig>,
res: Res<DebugVisuals>,
) {
let Some(player) = player.iter().next() else {
return;
};
if !res.cam_follow {
return;
}
rig.driver_mut::<GameCameraRig>()
.set_position_target(player.translation, player.rotation);
}
@@ -263,3 +280,32 @@ fn toggle_animation(
}
}
}
fn updaate_head(
trigger: Trigger<HeadChanged>,
mut commands: Commands,
asset_server: Res<AssetServer>,
head: Query<Entity, With<PlayerHead>>,
) {
let Ok(head) = head.get_single() else {
return;
};
let head_str = match trigger.0 {
0 => "angry demonstrator",
1 => "commando",
2 => "goblin",
3 => "highland hammer thrower",
_ => "french legionaire",
};
commands.spawn((
AudioPlayer::new(asset_server.load(format!("sfx/heads/{}.ogg", head_str))),
PlaybackSettings::DESPAWN,
));
let mesh = asset_server
.load(GltfAssetLabel::Scene(0).from_asset(format!("models/heads/{}.glb", head_str)));
commands.entity(head).insert(SceneRoot(mesh));
}