added walking and jumping (no ground checking)
This commit is contained in:
parent
a682cbb839
commit
3e9d23102d
2 changed files with 71 additions and 7 deletions
|
|
@ -16,29 +16,45 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_xpbd_3d::prelude::*;
|
use bevy_xpbd_3d::{math::*, prelude::*};
|
||||||
|
|
||||||
pub struct CharacterControllerPlugin;
|
pub struct CharacterControllerPlugin;
|
||||||
|
|
||||||
impl Plugin for CharacterControllerPlugin{
|
impl Plugin for CharacterControllerPlugin{
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, update_grounded);
|
app.add_systems(Update,(
|
||||||
|
update_grounded,
|
||||||
|
keyboard_input,
|
||||||
|
move_character,
|
||||||
|
dampen_movement)
|
||||||
|
);
|
||||||
|
app.add_event::<MovementAction>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Event)]
|
||||||
|
pub enum MovementAction {
|
||||||
|
Move(Vector2),
|
||||||
|
Jump
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[component(storage = "SparseSet")]
|
#[component(storage = "SparseSet")]
|
||||||
pub struct Grounded;
|
pub struct Grounded;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CharacterController{
|
pub struct CharacterController{
|
||||||
pub walk_speed: f32,
|
pub movement_acceleration: Scalar,
|
||||||
|
pub movement_dampening_factor: Scalar,
|
||||||
|
pub jump_impulse: Scalar,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CharacterController {
|
impl Default for CharacterController {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
CharacterController{
|
CharacterController{
|
||||||
walk_speed: 4.0,
|
movement_acceleration: 30.0,
|
||||||
|
movement_dampening_factor: 0.95,
|
||||||
|
jump_impulse: 7.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -58,7 +74,6 @@ impl CharacterControllerBundle {
|
||||||
rigid_body: RigidBody::Dynamic,
|
rigid_body: RigidBody::Dynamic,
|
||||||
collider: controller_collider,
|
collider: controller_collider,
|
||||||
locked_axis: LockedAxes::ROTATION_LOCKED,
|
locked_axis: LockedAxes::ROTATION_LOCKED,
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -80,3 +95,52 @@ pub fn update_grounded(mut character_controller_query: Query<&CharacterControlle
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn keyboard_input(mut movement_event_writer: EventWriter<MovementAction>, keyboard_input: Res<ButtonInput<KeyCode>>){
|
||||||
|
let up = keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]);
|
||||||
|
let down = keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]);
|
||||||
|
let left = keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]);
|
||||||
|
let right = keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]);
|
||||||
|
|
||||||
|
let horizontal = right as i8 - left as i8;
|
||||||
|
let vertical = up as i8 - down as i8;
|
||||||
|
let direction = Vector2::new(horizontal as Scalar, vertical as Scalar).clamp_length_max(1.0);
|
||||||
|
|
||||||
|
if direction != Vector2::ZERO {
|
||||||
|
movement_event_writer.send(MovementAction::Move(direction));
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||||
|
movement_event_writer.send(MovementAction::Jump);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_character(time: Res<Time>, mut movement_event_reader: EventReader<MovementAction>,
|
||||||
|
mut qeury: Query<(&mut LinearVelocity, &CharacterController)>
|
||||||
|
){
|
||||||
|
let delta_time = time.delta_seconds_f64().adjust_precision();
|
||||||
|
|
||||||
|
for event in movement_event_reader.read() {
|
||||||
|
for (mut velocity, character_controller) in &mut qeury{
|
||||||
|
match event{
|
||||||
|
MovementAction::Move(direction) => {
|
||||||
|
velocity.x += direction.x * character_controller.movement_acceleration * delta_time;
|
||||||
|
velocity.z -= direction.y * character_controller.movement_acceleration * delta_time;
|
||||||
|
},
|
||||||
|
MovementAction::Jump => {
|
||||||
|
velocity.y = character_controller.jump_impulse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dampen_movement(mut qeury: Query<(&mut LinearVelocity, &CharacterController)>){
|
||||||
|
for (mut velocity, character_controller) in &mut qeury {
|
||||||
|
// We could use `LinearDamping`, but we don't want to dampen movement along the Y axis
|
||||||
|
velocity.x *= character_controller.movement_dampening_factor;
|
||||||
|
velocity.z *= character_controller.movement_dampening_factor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -36,11 +36,11 @@ fn setup(
|
||||||
) {
|
) {
|
||||||
// plane
|
// plane
|
||||||
commands.spawn((PbrBundle {
|
commands.spawn((PbrBundle {
|
||||||
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
|
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
|
||||||
material: materials.add(Color::WHITE),
|
material: materials.add(Color::WHITE),
|
||||||
transform: Transform::from_xyz(0., 0., 0.),
|
transform: Transform::from_xyz(0., 0., 0.),
|
||||||
..default()
|
..default()
|
||||||
}, Collider::cuboid(5.0, 0.1, 5.0), RigidBody::Static));
|
}, Collider::cuboid(50.0, 0.1, 50.0), RigidBody::Static));
|
||||||
// cube
|
// cube
|
||||||
// commands.spawn((RigidBody::Dynamic, PbrBundle {
|
// commands.spawn((RigidBody::Dynamic, PbrBundle {
|
||||||
// mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
|
// mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue