Merge pull request #6 from Ewpratten/player_orientation

Player orientation
This commit is contained in:
Evan Pratten 2021-04-23 23:34:36 -04:00 committed by GitHub
commit 97f6af1dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 20 deletions

View File

@ -9,7 +9,7 @@ use super::screen::Screen;
const NORMAL_PLAYER_SPEED: i32 = 4; const NORMAL_PLAYER_SPEED: i32 = 4;
const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2;
const CAMERA_FOLLOW_SPEED: f32 = 3.0; const CAMERA_FOLLOW_SPEED: f32 = 0.7;
pub enum InGameState { pub enum InGameState {
BUYING, BUYING,
@ -32,34 +32,49 @@ impl InGameScreen {
&mut self, &mut self,
draw_handle: &mut RaylibDrawHandle, draw_handle: &mut RaylibDrawHandle,
game_core: &mut GameCore, game_core: &mut GameCore,
window_center: Vector2,
) { ) {
let player_screen_position =
draw_handle.get_screen_to_world2D(game_core.player.position, game_core.master_camera);
// Handle player movement // Handle player movement
let mouse_pose = draw_handle.get_mouse_position(); let mouse_pose = draw_handle.get_mouse_position();
let mut raw_movement_direction = mouse_pose - player_screen_position; let mouse_world_pose = draw_handle.get_screen_to_world2D(mouse_pose, game_core.master_camera);
raw_movement_direction.normalize(); let raw_movement_direction = mouse_world_pose - game_core.player.position;
game_core.player.direction = raw_movement_direction; 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 // Handle action buttons
let user_request_boost = let user_request_boost = draw_handle.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON);
draw_handle.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON);
let user_request_action = let user_request_action =
draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON); draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON);
// Move the player in their direction // Move the player in their direction
let speed_multiplier = match user_request_boost && game_core.player.boost_percent >= 0.0 { let speed_multiplier = match user_request_boost && game_core.player.boost_percent >= 0.0 {
true => BOOST_PLAYER_SPEED as f32, 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 // Move the camera to follow the player
let direction_from_cam_to_player = player_screen_position; let direction_from_cam_to_player =
// game_core.master_camera.offset -= direction_from_cam_to_player * CAMERA_FOLLOW_SPEED; (game_core.player.position - window_center) - game_core.master_camera.target;
game_core.master_camera.target = game_core.player.position + 0; 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( fn render_player(
@ -70,15 +85,32 @@ impl InGameScreen {
// Get the player // Get the player
let player = &game_core.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 // TODO: tmp rect
context_2d.draw_rectangle( context_2d.draw_rectangle_pro(
player.position.x as i32 - 10, Rectangle {
player.position.y as i32 - 10, x: player.position.x,
20, y: player.position.y,
30, width: player.size.x,
height: player.size.y,
},
player.size / 2.0,
player_rotation.to_degrees() + 90.0,
Color::BLACK, Color::BLACK,
); );
} }
fn render_world(
&mut self,
context_2d: &mut RaylibMode2D<RaylibDrawHandle>,
game_core: &mut GameCore,
) {
context_2d.draw_circle(0, 0, 10.0, Color::BLACK);
}
} }
impl Screen for InGameScreen { impl Screen for InGameScreen {
@ -106,12 +138,15 @@ impl Screen for InGameScreen {
}; };
// Update player movement // Update player movement
self.update_player_movement(draw_handle, game_core); self.update_player_movement(draw_handle, game_core, window_center);
// Open a 2D context // Open a 2D context
{ {
let mut context_2d = draw_handle.begin_mode2D(game_core.master_camera); 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 // Render Player
self.render_player(&mut context_2d, game_core); self.render_player(&mut context_2d, game_core);
} }

View File

@ -6,6 +6,7 @@ use raylib::math::Vector2;
pub struct Player { pub struct Player {
pub position: Vector2, pub position: Vector2,
pub direction: Vector2, pub direction: Vector2,
pub size: Vector2,
pub coins: u32, pub coins: u32,
pub boost_percent: f32 pub boost_percent: f32
} }
@ -14,6 +15,10 @@ impl Player {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
boost_percent: 1.0, boost_percent: 1.0,
size: Vector2 {
x: 11.0 * 4.0,
y: 21.0 * 4.0
},
..Default::default() ..Default::default()
} }