wip character controller
This commit is contained in:
17
.zed/debug.json
Normal file
17
.zed/debug.json
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// Project-local debug tasks
|
||||||
|
//
|
||||||
|
// For more documentation on how to configure debug tasks,
|
||||||
|
// see: https://zed.dev/docs/debugger
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"label": "Debug Run",
|
||||||
|
"build": {
|
||||||
|
"command": "cargo",
|
||||||
|
"args": ["build"]
|
||||||
|
},
|
||||||
|
"program": "$ZED_WORKTREE_ROOT/target/debug/space-game.exe",
|
||||||
|
"cwd": "$ZED_WORKTREE_ROOT",
|
||||||
|
"request": "launch",
|
||||||
|
"adapter": "GDB"
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -21,7 +21,7 @@ dev = [
|
|||||||
"dev-viz"
|
"dev-viz"
|
||||||
]
|
]
|
||||||
dev-viz = []
|
dev-viz = []
|
||||||
dev-phsy = []
|
dev-phys = []
|
||||||
|
|
||||||
[lints.clippy]
|
[lints.clippy]
|
||||||
# Bevy supplies arguments to systems via dependency injection, so it's natural for systems to
|
# Bevy supplies arguments to systems via dependency injection, so it's natural for systems to
|
||||||
|
|||||||
60
src/components/character_controller.rs
Normal file
60
src/components/character_controller.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
use avian3d::prelude::*;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Component, Reflect, Clone, Copy)]
|
||||||
|
#[require(
|
||||||
|
GravityDirection,
|
||||||
|
RigidBody,
|
||||||
|
Transform,
|
||||||
|
ExternalForce,
|
||||||
|
GravityScale,
|
||||||
|
CharacterMotion,
|
||||||
|
CharacterRotation
|
||||||
|
)]
|
||||||
|
pub struct CharacterController {
|
||||||
|
pub step_height: f32,
|
||||||
|
pub max_slope: f32,
|
||||||
|
height: f32,
|
||||||
|
radius: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for CharacterController {
|
||||||
|
fn default() -> Self {
|
||||||
|
return Self {
|
||||||
|
height: 1.0,
|
||||||
|
radius: 0.5,
|
||||||
|
max_slope: 0.5,
|
||||||
|
step_height: 0.25,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl CharacterController {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn with_size(mut self, height: f32, radius: f32) -> Self {
|
||||||
|
self.height = height;
|
||||||
|
self.radius = radius;
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn height(&self) -> f32 {
|
||||||
|
self.height
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn radius(&self) -> f32 {
|
||||||
|
self.radius
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Component, Default, Reflect)]
|
||||||
|
pub struct CharacterMotion(pub Vec3);
|
||||||
|
|
||||||
|
#[derive(Component, Default, Reflect)]
|
||||||
|
pub struct CharacterRotation(pub Quat);
|
||||||
|
|
||||||
|
#[derive(Component, Reflect)]
|
||||||
|
pub struct GravityDirection(pub Dir3);
|
||||||
|
|
||||||
|
impl Default for GravityDirection {
|
||||||
|
fn default() -> Self {
|
||||||
|
return GravityDirection(Dir3::NEG_Y);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod camera;
|
pub mod camera;
|
||||||
|
pub mod character_controller;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ mod plugins;
|
|||||||
mod utils;
|
mod utils;
|
||||||
use bevy::{prelude::*, window::PresentMode};
|
use bevy::{prelude::*, window::PresentMode};
|
||||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||||
use plugins::game::GamePlugin;
|
use plugins::GamePlugin;
|
||||||
mod components;
|
mod components;
|
||||||
mod resources;
|
mod resources;
|
||||||
|
|
||||||
|
|||||||
46
src/plugins/character_controller.rs
Normal file
46
src/plugins/character_controller.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use avian3d::prelude::{Collider, ExternalForce, GravityScale, LockedAxes, Mass};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::character_controller::{CharacterController, GravityDirection};
|
||||||
|
|
||||||
|
pub struct CharacterControllerPlugin;
|
||||||
|
|
||||||
|
impl Plugin for CharacterControllerPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(PreStartup, setup_hooks);
|
||||||
|
app.add_systems(Update, apply_gravity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_hooks(world: &mut World) {
|
||||||
|
world
|
||||||
|
.register_component_hooks::<CharacterController>()
|
||||||
|
.on_insert(|mut world, ctx| {
|
||||||
|
let controller = world.get::<CharacterController>(ctx.entity).unwrap();
|
||||||
|
let height = controller.height();
|
||||||
|
let radius = controller.radius();
|
||||||
|
|
||||||
|
let mut commands = world.commands();
|
||||||
|
let mut entity_commands = commands.entity(ctx.entity);
|
||||||
|
entity_commands.insert((
|
||||||
|
LockedAxes::ROTATION_LOCKED,
|
||||||
|
Collider::capsule(radius, height),
|
||||||
|
ExternalForce::ZERO.with_persistence(false),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_gravity(
|
||||||
|
controllers: Query<
|
||||||
|
(&mut ExternalForce, &GravityScale, &GravityDirection, Option<&Mass>),
|
||||||
|
With<CharacterController>,
|
||||||
|
>,
|
||||||
|
) {
|
||||||
|
for (mut force, grav_scale, grav_dir, mass) in controllers {
|
||||||
|
if grav_scale.0 == 0.0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let m = if let Some(mass) = mass { mass.0 } else { 1.0 };
|
||||||
|
force.apply_force(grav_dir.0 * grav_scale.0 * 9.3 * m);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,9 +7,10 @@ use bevy::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
components::{
|
components::{
|
||||||
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused},
|
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused},
|
||||||
tags::Ship,
|
character_controller::CharacterController,
|
||||||
|
tags::{Player, Ship},
|
||||||
},
|
},
|
||||||
plugins::{follow_cam::FollowCamPlugin, free_cam::FreeCamPlugin, ship::ShipPlugin, types::TypesPlugin},
|
plugins::*,
|
||||||
};
|
};
|
||||||
// use bevy_rapier3d::prelude::*;
|
// use bevy_rapier3d::prelude::*;
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ pub struct GamePlugin;
|
|||||||
|
|
||||||
impl Plugin for GamePlugin {
|
impl Plugin for GamePlugin {
|
||||||
fn build(&self, app: &mut bevy::app::App) {
|
fn build(&self, app: &mut bevy::app::App) {
|
||||||
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin));
|
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin, CharacterControllerPlugin));
|
||||||
app.add_plugins((
|
app.add_plugins((
|
||||||
PhysicsPlugins::default(),
|
PhysicsPlugins::default(),
|
||||||
#[cfg(feature = "dev-phys")]
|
#[cfg(feature = "dev-phys")]
|
||||||
@@ -36,10 +37,22 @@ fn setup_scene(
|
|||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||||
|
mut gravity: ResMut<Gravity>,
|
||||||
) {
|
) {
|
||||||
|
gravity.0 = Vec3::ZERO;
|
||||||
|
|
||||||
window.cursor_options.visible = false;
|
window.cursor_options.visible = false;
|
||||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Name::new("Player"),
|
||||||
|
CharacterController::default(),
|
||||||
|
Player,
|
||||||
|
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, 0.0)),
|
||||||
|
));
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Camera Root"),
|
Name::new("Camera Root"),
|
||||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||||
|
|||||||
@@ -1,6 +1,17 @@
|
|||||||
pub mod follow_cam;
|
mod character_controller;
|
||||||
pub mod free_cam;
|
mod follow_cam;
|
||||||
pub mod game;
|
mod free_cam;
|
||||||
pub mod ship;
|
mod game;
|
||||||
pub mod ship_cam;
|
mod player;
|
||||||
pub mod types;
|
mod ship;
|
||||||
|
mod ship_cam;
|
||||||
|
mod types;
|
||||||
|
|
||||||
|
pub use character_controller::*;
|
||||||
|
pub use follow_cam::*;
|
||||||
|
pub use free_cam::*;
|
||||||
|
pub use game::*;
|
||||||
|
pub use player::*;
|
||||||
|
pub use ship::*;
|
||||||
|
pub use ship_cam::*;
|
||||||
|
pub use types::*;
|
||||||
|
|||||||
9
src/plugins/player.rs
Normal file
9
src/plugins/player.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
pub struct PlayerPlugin;
|
||||||
|
|
||||||
|
impl Plugin for PlayerPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ pub struct ShipPlugin;
|
|||||||
|
|
||||||
impl Plugin for ShipPlugin {
|
impl Plugin for ShipPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, ship_controls);
|
// app.add_systems(Update, ship_controls);
|
||||||
#[cfg(feature = "dev-viz")]
|
#[cfg(feature = "dev-viz")]
|
||||||
app.add_systems(Update, ship_debug);
|
app.add_systems(Update, ship_debug);
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ fn ship_controls(
|
|||||||
>,
|
>,
|
||||||
key: Res<ButtonInput<KeyCode>>,
|
key: Res<ButtonInput<KeyCode>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut mouse_motion: EventReader<MouseMotion>,
|
mouse_motion: EventReader<MouseMotion>,
|
||||||
) {
|
) {
|
||||||
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
|
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user