From 8b260be5b7a549192b468713eee20575224b4844 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 24 Apr 2021 15:07:04 -0400 Subject: [PATCH] Proper colliders --- assets/worlds/mainworld.json | 4 ++-- src/logic/ingame/mod.rs | 13 +++++------- src/logic/ingame/playerlogic.rs | 10 +++++----- src/player.rs | 35 ++++++++++++++++++++++++++++++++- src/world.rs | 10 +++++++++- 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/assets/worlds/mainworld.json b/assets/worlds/mainworld.json index b9a4ce8..ca552aa 100644 --- a/assets/worlds/mainworld.json +++ b/assets/worlds/mainworld.json @@ -4,8 +4,8 @@ "y": 10000.0 }, "player_spawn": { - "x": 200.0, - "y": 100.0 + "x": 220.0, + "y": 50.0 }, "fish": [ { diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index f584351..18ab910 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -69,13 +69,8 @@ impl InGameScreen { // Render every collider for collider in game_core.world.colliders.iter() { context_2d.draw_rectangle_lines_ex( - Rectangle { - x: collider.x - (collider.width / 2.0), - y: collider.y - (collider.height / 2.0), - width: collider.width, - height: collider.height, - }, - 2, + collider, + 1, Color::RED, ); } @@ -119,7 +114,9 @@ impl Screen for InGameScreen { // Render the world self.render_world(&mut context_2d, game_core); - self.render_colliders(&mut context_2d, game_core); + if game_core.show_simple_debug_info{ + self.render_colliders(&mut context_2d, game_core); + } // Render entities let fish_clone = game_core.world.fish.clone(); diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index 4e8059e..6446ec5 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -130,18 +130,18 @@ pub fn update_player_movement( let player_real_movement = game_core.player.direction * speed_multiplier; if raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0 { game_core.player.is_moving = true; + game_core.player.position += player_real_movement; // Check for any collisions for collider in game_core.world.colliders.iter() { - if collider.check_collision_point_rec(game_core.player.position + player_real_movement) - { - // game_core.player.is_moving = false; + if game_core.player.collides_with_rec(collider) { + game_core.player.is_moving = false; break; } } - if game_core.player.is_moving { - game_core.player.position += player_real_movement; + if !game_core.player.is_moving { + game_core.player.position -= player_real_movement; } } else { game_core.player.is_moving = false; diff --git a/src/player.rs b/src/player.rs index b541685..01c5343 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,6 @@ -use raylib::math::Vector2; +use raylib::math::{Rectangle, Vector2}; + +use crate::lib::utils::triangles::rotate_vector; #[derive(Debug, Default)] pub struct Player { @@ -23,4 +25,35 @@ impl Player { ..Default::default() } } + + pub fn collides_with_rec(&self, rectangle: &Rectangle) -> bool { + // // Build a bounding box of the player by their corners + // let top_left_corner = self.position - (self.size / 2.0); + // let bottom_right_corner = self.position + (self.size / 2.0); + // let top_right_corner = Vector2 { + // x: bottom_right_corner.x, + // y: top_left_corner.y, + // }; + // let bottom_left_corner = Vector2 { + // x: top_left_corner.x, + // y: bottom_right_corner.y, + // }; + + // // Get the rotation + // let rotation = Vector2::zero().angle_to(self.direction); + + // // Rotate the bounds + // let top_left_corner = rotate_vector(top_left_corner, rotation); + // let bottom_right_corner = rotate_vector(bottom_right_corner, rotation); + // let top_right_corner = rotate_vector(top_right_corner, rotation); + // let bottom_left_corner = rotate_vector(bottom_left_corner, rotation); + + // // Check for collisions + // return rectangle.check_collision_point_rec(top_left_corner) + // || rectangle.check_collision_point_rec(bottom_right_corner) + // || rectangle.check_collision_point_rec(top_right_corner) + // || rectangle.check_collision_point_rec(bottom_left_corner); + + return rectangle.check_collision_circle_rec(self.position, (self.size.y * 0.5) / 2.0); + } } diff --git a/src/world.rs b/src/world.rs index 29cbf70..1f344fe 100644 --- a/src/world.rs +++ b/src/world.rs @@ -35,7 +35,15 @@ impl World { result.fish = FishEntity::new_from_positions(&result.fish_positions); // Init colliders - result.colliders = colliders; + result.colliders = Vec::new(); + for collider in colliders.iter(){ + result.colliders.push(Rectangle { + x: collider.x - (collider.width / 2.0), + y: collider.y - (collider.height / 2.0), + width: collider.width, + height: collider.height, + }); + } Ok(result) }