add jumping

This commit is contained in:
Schrottkatze 2024-05-09 20:42:55 +02:00
parent 68702e358f
commit 1176a8ec08
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
3 changed files with 104 additions and 76 deletions

View file

@ -8,6 +8,7 @@ const METER: f32 = 48.;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(RapierConfiguration::new(METER * 2.5))
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(METER))
.add_plugins(RapierDebugRenderPlugin::default())
.add_systems(Startup, (scene::setup_scene, setup_cam, player::add_player))
@ -25,79 +26,5 @@ fn setup_cam(mut commands: Commands) {
});
}
mod player;
mod scene;
mod player {
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*;
use crate::METER;
#[derive(Component)]
pub struct Player {
move_cooldown: Timer,
grounded: bool,
}
pub fn add_player(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let shape = Mesh2dHandle(meshes.add(Rectangle::new(0.8 * METER, 1.8 * METER)));
commands
.spawn(Player {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
grounded: false,
})
.insert(MaterialMesh2dBundle {
mesh: shape,
material: materials.add(Color::rgb(0.2, 0.9, 0.2)),
transform: Transform::from_xyz(0., 0., 0.),
..default()
})
.insert((
RigidBody::Dynamic,
Collider::cuboid(0.4 * METER, 0.9 * METER),
))
.insert(KinematicCharacterController { ..default() });
}
pub fn player_ground_collision(
player: Query<Entity, With<Player>>,
mut collision_events: EventReader<CollisionEvent>,
) {
let p = player.single();
for collision_event in collision_events.read() {
dbg!(p);
dbg!(collision_event);
}
}
pub fn move_player(
kb_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut Player, &mut KinematicCharacterController)>,
time: Res<Time>,
) {
let (mut player, mut controller) = query.single_mut();
if player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let mut mv = 0;
if kb_input.pressed(KeyCode::KeyA) {
moved = true;
mv += 1;
}
if kb_input.pressed(KeyCode::KeyD) {
moved = true;
mv -= 1;
}
if moved {
controller.translation = Some(Vec2::new(mv as f32 * 6., 0.));
}
}
}
}

101
src/player.rs Normal file
View file

@ -0,0 +1,101 @@
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*;
use crate::METER;
#[derive(Component)]
pub struct Player {
move_cooldown: Timer,
grounded: bool,
}
pub fn add_player(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let shape = Mesh2dHandle(meshes.add(Rectangle::new(0.8 * METER, 1.8 * METER)));
commands
.spawn(Player {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
// move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
grounded: false,
})
.insert(MaterialMesh2dBundle {
mesh: shape,
material: materials.add(Color::rgb(0.2, 0.9, 0.2)),
transform: Transform::from_xyz(0., 0., 0.),
..default()
})
.insert((
RigidBody::Dynamic,
Collider::cuboid(0.4 * METER, 0.9 * METER),
Velocity::default(),
))
// .insert(ActiveCollisionTypes::default() | ActiveCollisionTypes::DYNAMIC_STATIC)
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(KinematicCharacterController { ..default() });
}
pub fn player_ground_collision(
mut player: Query<(Entity, &mut Player)>,
scene_objs: Query<Entity, With<super::scene::SceneObj>>,
mut collision_events: EventReader<CollisionEvent>,
) {
let (p, mut player) = player.single_mut();
for collision_event in collision_events.read() {
match collision_event {
CollisionEvent::Started(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj))
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
player.grounded = true;
}
}
CollisionEvent::Stopped(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj))
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
player.grounded = false;
}
}
}
}
}
pub fn move_player(
kb_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(
&mut Velocity,
&mut Player,
&mut KinematicCharacterController,
)>,
time: Res<Time>,
) {
let (mut vel, mut player, mut controller) = query.single_mut();
if player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let mut mv = 0;
if kb_input.pressed(KeyCode::KeyA) {
moved = true;
mv -= 1;
}
if kb_input.pressed(KeyCode::KeyD) {
moved = true;
mv += 1;
}
if kb_input.pressed(KeyCode::KeyW) && player.grounded {
moved = true;
*vel = Velocity::linear(Vec2::new(0., 9. * METER));
}
if moved {
controller.translation = Some(Vec2::new(mv as f32 * 6., 0.));
}
}
}

View file

@ -7,7 +7,7 @@ use bevy_rapier2d::prelude::*;
use crate::METER;
#[derive(Component)]
struct SceneObj;
pub struct SceneObj;
pub fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,