diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index ce2e475..b876de3 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -9,7 +9,7 @@ use super::screen::Screen; const NORMAL_PLAYER_SPEED: i32 = 4; const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; -const CAMERA_FOLLOW_SPEED: f32 = 1.0; +const CAMERA_FOLLOW_SPEED: f32 = 0.7; pub enum InGameState { BUYING, @@ -32,35 +32,49 @@ impl InGameScreen { &mut self, draw_handle: &mut RaylibDrawHandle, game_core: &mut GameCore, - window_center: Vector2 + window_center: Vector2, ) { - let player_screen_position = - draw_handle.get_screen_to_world2D(game_core.player.position, game_core.master_camera); // Handle player movement let mouse_pose = draw_handle.get_mouse_position(); - let mut raw_movement_direction = mouse_pose - player_screen_position; - raw_movement_direction.normalize(); - game_core.player.direction = raw_movement_direction; + let mouse_world_pose = draw_handle.get_screen_to_world2D(mouse_pose, game_core.master_camera); + let raw_movement_direction = mouse_world_pose - game_core.player.position; + let mut normalized_movement_direction = raw_movement_direction; + normalized_movement_direction.normalize(); + game_core.player.direction = normalized_movement_direction; + + // In the case the player is in "null", just jump the camera to them + if game_core.player.position == Vector2::zero() { + game_core.master_camera.target = game_core.player.position - (window_center / 2.0); + } // Handle action buttons - let user_request_boost = - draw_handle.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON); + let user_request_boost = draw_handle.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON); let user_request_action = draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON); // Move the player in their direction let speed_multiplier = match user_request_boost && game_core.player.boost_percent >= 0.0 { true => BOOST_PLAYER_SPEED as f32, - false => NORMAL_PLAYER_SPEED as f32 + false => NORMAL_PLAYER_SPEED as f32, }; - game_core.player.position += game_core.player.direction * speed_multiplier; + + // 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; + } // Move the camera to follow the player - let direction_from_cam_to_player = (game_core.player.position - window_center) - game_core.master_camera.target; - // game_core.master_camera.offset -= direction_from_cam_to_player * CAMERA_FOLLOW_SPEED; - game_core.master_camera.target += direction_from_cam_to_player * CAMERA_FOLLOW_SPEED; - + let direction_from_cam_to_player = + (game_core.player.position - window_center) - game_core.master_camera.target; + let player_screen_position = draw_handle.get_world_to_screen2D(game_core.player.position, game_core.master_camera); + + // Camera only moves if you get close to the edge of the screen + if player_screen_position.distance_to(window_center).abs() > (window_center.y - 40.0) { + game_core.master_camera.target += player_real_movement; + } + } fn render_player( @@ -71,15 +85,32 @@ impl InGameScreen { // Get the player let player = &game_core.player; + // Convert the player direction to a rotation + let player_rotation = Vector2::zero().angle_to(player.direction); + // TODO: tmp rect - context_2d.draw_rectangle( - player.position.x as i32 - 10, - player.position.y as i32 - 10, - 20, - 30, + context_2d.draw_rectangle_pro( + Rectangle { + x: player.position.x, + y: player.position.y, + width: player.size.x, + height: player.size.y, + }, + player.size / 2.0, + player_rotation.to_degrees() + 90.0, Color::BLACK, ); } + + fn render_world( + &mut self, + context_2d: &mut RaylibMode2D, + game_core: &mut GameCore, + ) { + + context_2d.draw_circle(0, 0, 10.0, Color::BLACK); + + } } impl Screen for InGameScreen { @@ -113,6 +144,9 @@ impl Screen for InGameScreen { { let mut context_2d = draw_handle.begin_mode2D(game_core.master_camera); + // Render the world + self.render_world(&mut context_2d, game_core); + // Render Player self.render_player(&mut context_2d, game_core); } diff --git a/src/player.rs b/src/player.rs index 3fc7c14..3fa3a58 100644 --- a/src/player.rs +++ b/src/player.rs @@ -6,6 +6,7 @@ use raylib::math::Vector2; pub struct Player { pub position: Vector2, pub direction: Vector2, + pub size: Vector2, pub coins: u32, pub boost_percent: f32 } @@ -14,6 +15,10 @@ impl Player { pub fn new() -> Self { Self { boost_percent: 1.0, + size: Vector2 { + x: 11.0 * 4.0, + y: 21.0 * 4.0 + }, ..Default::default() }