collider debugging

This commit is contained in:
Evan Pratten 2021-04-24 14:47:31 -04:00
parent 66a965511e
commit d52af77a47
6 changed files with 46 additions and 17 deletions

View File

@ -3,6 +3,10 @@
"x": 10000.0,
"y": 10000.0
},
"player_spawn": {
"x": 200.0,
"y": 100.0
},
"fish": [
{
"x": 500.0,

View File

@ -107,6 +107,7 @@ impl GameCore {
world: World,
progress: GameProgress,
) -> Self {
let player = Player::new(&world.player_spawn);
Self {
state: GameState::Loading,
last_state: GameState::Loading,
@ -117,13 +118,13 @@ impl GameCore {
.expect("Failed to load game assets. Can not launch!"),
master_camera: Camera2D {
offset: Vector2::zero(),
target: Vector2::zero(),
target: world.player_spawn,
rotation: 0.0,
zoom: 2.0,
},
show_simple_debug_info: false,
world: world,
player: Player::new(),
player,
progress: progress,
}
}

View File

@ -36,8 +36,8 @@ impl InGameScreen {
height: game_core.resources.cave_mid_layer.height as f32,
};
let world_bounds = Rectangle {
x: -200.0,
y: -100.0,
x: 0.0,
y: 0.0,
width: game_core.resources.cave_mid_layer.width as f32,
height: game_core.resources.cave_mid_layer.height as f32,
};
@ -56,6 +56,18 @@ impl InGameScreen {
Color::WHITE,
);
}
fn render_colliders(
&mut self,
context_2d: &mut RaylibMode2D<RaylibDrawHandle>,
game_core: &mut GameCore,
) {
// Render every collider
for collider in game_core.world.colliders.iter() {
context_2d.draw_rectangle_lines_ex(collider, 2, Color::RED);
}
}
}
impl Screen for InGameScreen {
@ -95,6 +107,7 @@ impl Screen for InGameScreen {
// Render the world
self.render_world(&mut context_2d, game_core);
self.render_colliders(&mut context_2d, game_core);
// Render entities
let mut fish = &mut game_core.world.fish;

View File

@ -129,8 +129,20 @@ pub fn update_player_movement(
// Only do this if the mouse is far enough away
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.position += player_real_movement;
game_core.player.is_moving = true;
// 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;
break;
}
}
if game_core.player.is_moving {
game_core.player.position += player_real_movement;
}
} else {
game_core.player.is_moving = false;
}
@ -146,12 +158,15 @@ pub fn update_player_movement(
game_core.master_camera.target += player_real_movement;
}
// If the player is not on screen, snap the camera to them
if player_screen_position.distance_to(window_center).abs() > window_center.y {
game_core.master_camera.target = game_core.player.position - (window_center / 2.0);
}
// // Clamp camera target y to 0
// if game_core.master_camera.target.y < -100.0 {
// game_core.master_camera.target.y = -100.0;
// }
}
pub fn render_player(context_2d: &mut RaylibMode2D<RaylibDrawHandle>, game_core: &mut GameCore) {

View File

@ -1,7 +1,5 @@
use raylib::math::Vector2;
#[derive(Debug, Default)]
pub struct Player {
pub position: Vector2,
@ -12,20 +10,17 @@ pub struct Player {
pub breath_percent: f32,
pub is_moving: bool,
pub is_boosting: bool,
pub is_boost_charging: bool
pub is_boost_charging: bool,
}
impl Player {
pub fn new() -> Self {
pub fn new(spawn: &Vector2) -> Self {
Self {
boost_percent: 1.0,
size: Vector2 {
x: 11.0,
y: 21.0
},
size: Vector2 { x: 11.0, y: 21.0 },
breath_percent: 1.0,
position: spawn.clone(),
..Default::default()
}
}
}
}

View File

@ -10,6 +10,7 @@ use crate::entities::fish::FishEntity;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct World {
pub end_position: Vector2,
pub player_spawn: Vector2,
#[serde(rename = "fish")]
pub fish_positions: Vec<Vector2>,