custom camera rig
* should not go into level geometry anymore
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use crate::{
|
||||
DebugVisuals,
|
||||
alien::{ALIEN_ASSET_PATH, Animations},
|
||||
camera::GameCameraRig,
|
||||
camera::{CameraArmRotation, CameraTarget},
|
||||
cash::{Cash, CashCollectEvent},
|
||||
controls::Controls,
|
||||
heads_ui::HeadChanged,
|
||||
physics_layers::GameLayer,
|
||||
tb_entities::SpawnPoint,
|
||||
};
|
||||
use avian3d::prelude::*;
|
||||
@@ -12,10 +12,9 @@ use bevy::{
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
use bevy_dolly::prelude::Rig;
|
||||
use bevy_tnua::{TnuaUserControlsSystemSet, prelude::*};
|
||||
use bevy_tnua_avian3d::TnuaAvian3dSensorShape;
|
||||
use std::{f32::consts::PI, time::Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct Player;
|
||||
@@ -24,7 +23,7 @@ pub struct Player;
|
||||
struct PlayerAnimations;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct PlayerHead;
|
||||
struct PlayerHead;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct PlayerRig;
|
||||
@@ -47,7 +46,6 @@ pub fn plugin(app: &mut App) {
|
||||
Update,
|
||||
(
|
||||
spawn,
|
||||
update_camera,
|
||||
collect_cash,
|
||||
toggle_animation,
|
||||
setup_animations_marker_for_player,
|
||||
@@ -86,30 +84,39 @@ fn spawn(
|
||||
.spawn((
|
||||
Name::from("player"),
|
||||
Player,
|
||||
CameraTarget,
|
||||
transform,
|
||||
TransformInterpolation,
|
||||
TransformExtrapolation,
|
||||
Visibility::default(),
|
||||
RigidBody::Dynamic,
|
||||
Collider::capsule(1.2, 1.5),
|
||||
CollisionLayers::new(LayerMask(GameLayer::Player.to_bits()), LayerMask::ALL),
|
||||
LockedAxes::ROTATION_LOCKED,
|
||||
TnuaController::default(),
|
||||
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),
|
||||
))
|
||||
.with_child((
|
||||
Name::from("body rig"),
|
||||
PlayerRig,
|
||||
Transform::from_translation(Vec3::new(0., -3., 0.))
|
||||
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI))
|
||||
.with_scale(Vec3::splat(1.5)),
|
||||
SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset(ALIEN_ASSET_PATH))),
|
||||
));
|
||||
.with_children(|parent| {
|
||||
parent
|
||||
.spawn((
|
||||
Name::from("body rig"),
|
||||
PlayerRig,
|
||||
CameraArmRotation,
|
||||
Transform::from_translation(Vec3::new(0., -3., 0.))
|
||||
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI))
|
||||
.with_scale(Vec3::splat(1.4)),
|
||||
SceneRoot(
|
||||
asset_server.load(GltfAssetLabel::Scene(0).from_asset(ALIEN_ASSET_PATH)),
|
||||
),
|
||||
))
|
||||
.with_child((
|
||||
Name::from("head"),
|
||||
PlayerHead,
|
||||
Transform::from_translation(Vec3::new(0., 1.6, 0.))
|
||||
.with_scale(Vec3::splat(0.7)),
|
||||
SceneRoot(mesh),
|
||||
));
|
||||
});
|
||||
|
||||
commands.spawn((
|
||||
AudioPlayer::new(asset_server.load("sfx/heads/angry demonstrator.ogg")),
|
||||
@@ -122,7 +129,7 @@ fn spawn(
|
||||
fn rotate_view(
|
||||
mut controls: ResMut<Controls>,
|
||||
// todo: Put the player head as a child of the rig to avoid this mess:
|
||||
mut player: Query<&mut Transform, Or<(With<PlayerRig>, With<PlayerHead>)>>,
|
||||
mut player: Query<&mut Transform, With<PlayerRig>>,
|
||||
) {
|
||||
for mut tr in &mut player {
|
||||
tr.rotate_y(controls.keyboard_state.look_dir.x * -0.001);
|
||||
@@ -207,25 +214,6 @@ fn apply_controls(
|
||||
}
|
||||
}
|
||||
|
||||
fn update_camera(
|
||||
player: Query<&GlobalTransform, With<PlayerRig>>,
|
||||
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() * Quat::from_rotation_y(PI),
|
||||
);
|
||||
}
|
||||
|
||||
fn collect_cash(
|
||||
mut commands: Commands,
|
||||
mut collision_event_reader: EventReader<CollisionStarted>,
|
||||
|
||||
Reference in New Issue
Block a user