From b4e74a5694f2031a13cd5e1401c9af37acc6a102 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 24 Apr 2021 09:49:39 -0400 Subject: [PATCH] Move James's changes to new file Co-authored-by: rsninja722 --- src/logic/ingame/mod.rs | 117 -------------------------------- src/logic/ingame/playerlogic.rs | 41 ++++++++++- 2 files changed, 40 insertions(+), 118 deletions(-) diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index e014d08..7297289 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -10,11 +10,6 @@ use crate::{ use super::screen::Screen; -const NORMAL_PLAYER_SPEED: i32 = 4; -const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; -const CAMERA_FOLLOW_SPEED: f32 = 0.7; -const TURN_SPEED: f32 = 0.05; - pub enum InGameState { BUYING, SWIMMING, @@ -31,118 +26,6 @@ impl InGameScreen { } } - fn update_player_movement( - &mut self, - draw_handle: &mut RaylibDrawHandle, - game_core: &mut GameCore, - window_center: Vector2, - ) { - - // Handle player movement - let mouse_pose = draw_handle.get_mouse_position(); - 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(); - - let tau: f32 = PI as f32 * 2.0; - // get angles as floats - let mut player_angle: f32 = Vector2::zero().angle_to(game_core.player.direction); - let mut desired_angle: f32 = Vector2::zero().angle_to(normalized_movement_direction); - - // make angle positive - if desired_angle < 0.0 { - desired_angle += tau; - } - - // turn towards mouse at turn speed - if player_angle % tau > desired_angle { - if (player_angle % tau) - desired_angle > PI as f32 { - player_angle += TURN_SPEED; - } else { - player_angle -= TURN_SPEED; - } - } else { - if desired_angle - (player_angle % tau) > PI as f32 { - player_angle -= TURN_SPEED; - } else { - player_angle += TURN_SPEED; - } - } - - // snap to mouse if close enough - if f32::abs(player_angle - desired_angle) < (TURN_SPEED * 1.1) { - player_angle = desired_angle; - } - if player_angle > tau { - player_angle -= tau; - } - if player_angle < 0.0 { - player_angle += tau; - } - - // set angle - game_core.player.direction = Vector2::new(f32::cos(player_angle), f32::sin(player_angle)); - - // 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_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, - }; - - // 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; - 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( - &mut self, - context_2d: &mut RaylibMode2D, - game_core: &mut GameCore, - ) { - // 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_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, diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index 01d5f0d..37c0a16 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -8,6 +8,7 @@ use crate::{ const NORMAL_PLAYER_SPEED: i32 = 4; const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; const CAMERA_FOLLOW_SPEED: f32 = 0.7; +const TURN_SPEED: f32 = 0.05; const BOOST_DECREASE_PER_SECOND: f32 = 0.75; const BOOST_REGEN_PER_SECOND: f32 = 0.25; const BREATH_DECREASE_PER_SECOND: f32 = 0.01; @@ -26,7 +27,45 @@ pub fn update_player_movement( 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; + + let tau: f32 = PI as f32 * 2.0; + // get angles as floats + let mut player_angle: f32 = Vector2::zero().angle_to(game_core.player.direction); + let mut desired_angle: f32 = Vector2::zero().angle_to(normalized_movement_direction); + + // make angle positive + if desired_angle < 0.0 { + desired_angle += tau; + } + + // turn towards mouse at turn speed + if player_angle % tau > desired_angle { + if (player_angle % tau) - desired_angle > PI as f32 { + player_angle += TURN_SPEED; + } else { + player_angle -= TURN_SPEED; + } + } else { + if desired_angle - (player_angle % tau) > PI as f32 { + player_angle -= TURN_SPEED; + } else { + player_angle += TURN_SPEED; + } + } + + // snap to mouse if close enough + if f32::abs(player_angle - desired_angle) < (TURN_SPEED * 1.1) { + player_angle = desired_angle; + } + if player_angle > tau { + player_angle -= tau; + } + if player_angle < 0.0 { + player_angle += tau; + } + + // set angle + game_core.player.direction = Vector2::new(f32::cos(player_angle), f32::sin(player_angle)); // In the case the player is in "null", just jump the camera to them if game_core.player.position == Vector2::zero() {