testing
This commit is contained in:
@@ -7,6 +7,7 @@ use bevy::prelude::*;
|
||||
RigidBody,
|
||||
Transform,
|
||||
ExternalForce,
|
||||
LinearVelocity,
|
||||
GravityScale,
|
||||
CharacterMotion,
|
||||
CharacterRotation
|
||||
@@ -16,6 +17,7 @@ pub struct CharacterController {
|
||||
pub max_slope: f32,
|
||||
height: f32,
|
||||
radius: f32,
|
||||
pub is_grounded: bool,
|
||||
}
|
||||
|
||||
impl Default for CharacterController {
|
||||
@@ -25,6 +27,7 @@ impl Default for CharacterController {
|
||||
radius: 0.5,
|
||||
max_slope: 0.5,
|
||||
step_height: 0.25,
|
||||
is_grounded: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
use avian3d::prelude::{Collider, ExternalForce, GravityScale, LockedAxes, Mass};
|
||||
use avian3d::prelude::{
|
||||
Collider, ExternalForce, GravityScale, LinearVelocity, LockedAxes, Mass, RayCaster, RayHits, RigidBody,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::components::character_controller::{CharacterController, GravityDirection};
|
||||
use crate::components::character_controller::{
|
||||
CharacterController, CharacterMotion, CharacterRotation, 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);
|
||||
app.add_systems(Update, is_grounded);
|
||||
app.add_systems(Update, (apply_gravity, apply_forces, apply_motion).chain());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,24 +28,60 @@ fn setup_hooks(world: &mut World) {
|
||||
let mut commands = world.commands();
|
||||
let mut entity_commands = commands.entity(ctx.entity);
|
||||
entity_commands.insert((
|
||||
LockedAxes::ROTATION_LOCKED,
|
||||
RigidBody::Kinematic,
|
||||
Collider::capsule(radius, height),
|
||||
ExternalForce::ZERO.with_persistence(false),
|
||||
RayCaster::new(Vec3::ZERO, Dir3::NEG_Y).with_max_distance(1.0),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
fn is_grounded(controllers: Query<(&mut CharacterController, &RayHits)>) {
|
||||
for (mut controller, hits) in controllers {
|
||||
controller.is_grounded = !hits.is_empty();
|
||||
println!("{}", controller.is_grounded);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_gravity(
|
||||
controllers: Query<
|
||||
(&mut ExternalForce, &GravityScale, &GravityDirection, Option<&Mass>),
|
||||
With<CharacterController>,
|
||||
>,
|
||||
controllers: Query<(
|
||||
&mut ExternalForce,
|
||||
&GravityScale,
|
||||
&GravityDirection,
|
||||
Option<&Mass>,
|
||||
&CharacterController,
|
||||
)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (mut force, grav_scale, grav_dir, mass) in controllers {
|
||||
if grav_scale.0 == 0.0 {
|
||||
for (mut force, grav_scale, grav_dir, mass, controller) in controllers {
|
||||
if controller.is_grounded {
|
||||
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);
|
||||
force.apply_force(grav_dir.0 * grav_scale.0 * 9.3 * m * time.delta_secs());
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_motion(
|
||||
controllers: Query<
|
||||
(&LinearVelocity, &CharacterMotion, &CharacterRotation, &mut Transform),
|
||||
With<CharacterController>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (lin_vel, motion, _rotation, mut transform) in controllers {
|
||||
let move_vec = transform.rotation * motion.0;
|
||||
let vel = (lin_vel.0 + move_vec) * time.delta_secs();
|
||||
transform.translation += vel;
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_forces(
|
||||
controllers: Query<(&mut LinearVelocity, &mut ExternalForce), With<CharacterController>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (mut vel, mut force) in controllers {
|
||||
vel.0 += force.force() * time.delta_secs();
|
||||
force.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,20 @@ pub struct GamePlugin;
|
||||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut bevy::app::App) {
|
||||
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin, CharacterControllerPlugin));
|
||||
app.add_plugins((
|
||||
FollowCamPlugin,
|
||||
// ShipPlugin,
|
||||
TypesPlugin,
|
||||
PlayerPlugin,
|
||||
CharacterControllerPlugin,
|
||||
));
|
||||
app.add_plugins((
|
||||
PhysicsPlugins::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
PhysicsDebugPlugin::default(),
|
||||
));
|
||||
app.insert_resource(Gravity::ZERO);
|
||||
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
|
||||
app.add_systems(Startup, (setup_scene).chain());
|
||||
|
||||
app.add_systems(Update, camera_toggle);
|
||||
}
|
||||
@@ -41,23 +47,30 @@ fn setup_scene(
|
||||
) {
|
||||
gravity.0 = Vec3::ZERO;
|
||||
|
||||
window.cursor_options.visible = false;
|
||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||
// 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)),
|
||||
));
|
||||
let player = 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, 10.0)),
|
||||
))
|
||||
.id();
|
||||
|
||||
commands.spawn((
|
||||
Name::new("Camera Root"),
|
||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||
CameraRoot,
|
||||
Visibility::default(),
|
||||
FollowCam {
|
||||
distance: 20.,
|
||||
height: 10.,
|
||||
target: player,
|
||||
},
|
||||
children![(
|
||||
Camera3d::default(),
|
||||
CameraPitch::default(),
|
||||
|
||||
@@ -1,9 +1,29 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::components::{character_controller::CharacterMotion, tags::Player};
|
||||
|
||||
pub struct PlayerPlugin;
|
||||
|
||||
impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
todo!()
|
||||
app.add_systems(PreUpdate, keyboard_input);
|
||||
}
|
||||
}
|
||||
|
||||
fn keyboard_input(key: Res<ButtonInput<KeyCode>>, mut player: Single<&mut CharacterMotion, With<Player>>) {
|
||||
let mut move_vec = Vec3::ZERO;
|
||||
|
||||
if key.pressed(KeyCode::KeyW) {
|
||||
move_vec.z = -1.0;
|
||||
} else if key.pressed(KeyCode::KeyS) {
|
||||
move_vec.z = 1.0;
|
||||
}
|
||||
|
||||
if key.pressed(KeyCode::KeyA) {
|
||||
move_vec.x = -1.0;
|
||||
} else if key.pressed(KeyCode::KeyD) {
|
||||
move_vec.x = 1.0;
|
||||
}
|
||||
|
||||
player.0 = move_vec;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user