Skip to content

Commit

Permalink
detect collisions against walls
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Oct 30, 2024
1 parent ddd4a80 commit 6b631d9
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -17,12 +17,15 @@ impl Plugin for GamePlugin {
}

#[derive(Component)]
#[require(IsOnGround, Velocity)]
#[require(IsOnGround, Velocity, AgainstWall)]
struct Player;

#[derive(Component, Default)]
struct IsOnGround(bool);

#[derive(Component, Default)]
struct AgainstWall(bool, bool);

#[derive(Component, Default)]
struct Velocity {
current: f32,
33 changes: 27 additions & 6 deletions src/game/player.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use bevy::{

use crate::GameState;

use super::{Ground, IsOnGround, Player, Velocity};
use super::{AgainstWall, Ground, IsOnGround, Player, Velocity};

pub struct PlayerPlugin;

@@ -46,11 +46,12 @@ fn control_player(
}

fn on_ground(
mut player: Query<(&Transform, &mut IsOnGround), With<Player>>,
mut player: Query<(&Transform, &mut IsOnGround, &mut AgainstWall), With<Player>>,
ground: Query<&Transform, (Without<Player>, With<Ground>)>,
) {
let mut is_on_ground = false;
let (player_transform, mut player_on_ground) = player.single_mut();
let mut is_against_wall = (false, false);
let (player_transform, mut player_on_ground, mut player_against_wall) = player.single_mut();

let player_aabb = Aabb2d::new(
Vec2::new(
@@ -76,12 +77,26 @@ fn on_ground(
);

if ground_aabb.intersects(&player_aabb) {
is_on_ground = true;
if ground_transform.translation.y > player_transform.translation.y - 34.0 {
if ground_transform.translation.x < player_transform.translation.x {
is_against_wall.0 = true;
} else {
is_against_wall.1 = true;
}
} else {
is_on_ground = true;
}
}
}
if is_on_ground != player_on_ground.0 {
player_on_ground.0 = is_on_ground;
}
if is_against_wall.0 != player_against_wall.0 {
player_against_wall.0 = is_against_wall.0;
}
if is_against_wall.1 != player_against_wall.1 {
player_against_wall.1 = is_against_wall.1;
}
}

fn gravity(mut player: Query<(&mut Transform, &IsOnGround), With<Player>>) {
@@ -92,15 +107,21 @@ fn gravity(mut player: Query<(&mut Transform, &IsOnGround), With<Player>>) {
}
}

fn moving(mut player: Query<(&mut Transform, &mut Velocity), With<Player>>) {
let (mut player_transform, mut velocity) = player.single_mut();
fn moving(mut player: Query<(&mut Transform, &mut Velocity, &AgainstWall), With<Player>>) {
let (mut player_transform, mut velocity, against_wall) = player.single_mut();

if velocity.jumping > 0.0 {
player_transform.translation.y += velocity.jumping;
velocity.jumping -= 0.5;
}

if velocity.current != 0.0 {
if against_wall.0 && velocity.current < 0.0 {
velocity.current = 0.0;
}
if against_wall.1 && velocity.current > 0.0 {
velocity.current = 0.0;
}
player_transform.translation.x += velocity.current;
}
if velocity.current != velocity.target {

0 comments on commit 6b631d9

Please sign in to comment.