wip character controller
This commit is contained in:
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 character_controller;
|
||||
pub mod tags;
|
||||
|
||||
@@ -2,7 +2,7 @@ mod plugins;
|
||||
mod utils;
|
||||
use bevy::{prelude::*, window::PresentMode};
|
||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||
use plugins::game::GamePlugin;
|
||||
use plugins::GamePlugin;
|
||||
mod components;
|
||||
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::{
|
||||
components::{
|
||||
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::*;
|
||||
|
||||
@@ -18,7 +19,7 @@ pub struct GamePlugin;
|
||||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut bevy::app::App) {
|
||||
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin));
|
||||
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin, CharacterControllerPlugin));
|
||||
app.add_plugins((
|
||||
PhysicsPlugins::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
@@ -36,10 +37,22 @@ fn setup_scene(
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||
mut gravity: ResMut<Gravity>,
|
||||
) {
|
||||
gravity.0 = Vec3::ZERO;
|
||||
|
||||
window.cursor_options.visible = false;
|
||||
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((
|
||||
Name::new("Camera Root"),
|
||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
pub mod follow_cam;
|
||||
pub mod free_cam;
|
||||
pub mod game;
|
||||
pub mod ship;
|
||||
pub mod ship_cam;
|
||||
pub mod types;
|
||||
mod character_controller;
|
||||
mod follow_cam;
|
||||
mod free_cam;
|
||||
mod game;
|
||||
mod player;
|
||||
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 {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, ship_controls);
|
||||
// app.add_systems(Update, ship_controls);
|
||||
#[cfg(feature = "dev-viz")]
|
||||
app.add_systems(Update, ship_debug);
|
||||
}
|
||||
@@ -26,7 +26,7 @@ fn ship_controls(
|
||||
>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user