First person camera

Input States
This commit is contained in:
2025-06-27 19:34:55 -04:00
parent e198ac3cd0
commit 036fc19567
11 changed files with 200 additions and 189 deletions

View File

@@ -1,10 +1,11 @@
use crate::{
components::{
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused},
player::{GravityDirection, MoveSpeed, PlayerDrag},
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
player::PlayerDrag,
tags::{Player, Ship},
},
plugins::*,
plugins::{state_management::StateManagementPlugin, *},
states::input::{InputState, PlayerState},
};
use bevy::{
prelude::*,
@@ -19,19 +20,18 @@ impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) {
app.add_plugins((
FollowCamPlugin,
CameraPlugin,
StateManagementPlugin,
// ShipPlugin,
TypesPlugin,
PlayerPlugin,
// CharacterControllerPlugin,
));
app.add_plugins((
RapierPhysicsPlugin::<NoUserData>::default(),
#[cfg(feature = "dev-phys")]
RapierDebugRenderPlugin::default(),
));
app.add_systems(Startup, (setup_scene).chain());
app.add_systems(Update, camera_toggle);
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
}
}
@@ -41,39 +41,37 @@ fn setup_scene(
mut materials: ResMut<Assets<StandardMaterial>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
) {
// window.cursor_options.visible = false;
// window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
let player = commands
let player_eye = commands
.spawn((
Name::new("Player"),
Player,
PlayerDrag(0.5),
Collider::capsule_y(0.5, 0.5),
RigidBody::KinematicPositionBased,
KinematicCharacterController::default(),
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
Name::new("Eye"),
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
CameraPitch::default(),
))
.id();
let mut player_commands = commands.spawn((
Name::new("Player"),
Player,
PlayerDrag(0.5),
Collider::capsule_y(0.5, 0.5),
RigidBody::KinematicPositionBased,
KinematicCharacterController::default(),
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
));
player_commands.add_child(player_eye);
commands.spawn((
Name::new("Camera Root"),
Name::new("Camera"),
Transform::from_xyz(0.0, 1.3, 0.0),
CameraRoot,
Visibility::default(),
FollowCam {
distance: 20.,
height: 10.,
target: player,
},
children![(
Camera3d::default(),
CameraPitch::default(),
MainCamera,
Transform::default()
)],
MainCamera,
Camera3d::default(),
CameraMode::Player,
CameraAttachment(player_eye),
));
commands.spawn((
@@ -102,84 +100,56 @@ fn spawn_ship(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
camera: Single<Entity, With<MainCamera>>,
) {
let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5));
let material = materials.add(Color::BLACK);
let ship = commands
.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
Velocity::zero(),
Damping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
],
))
.id();
commands.entity(camera.into_inner()).insert(FollowCam {
distance: 20.,
height: 10.,
target: ship,
});
}
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<Entity, With<CameraRoot>>,
mut commands: Commands,
) {
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
commands.entity(camera.into_inner()).remove::<Unfocused>();
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
} else {
commands.entity(camera.into_inner()).insert(Unfocused);
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
Velocity::zero(),
Damping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
],
));
}