Merge pull request #44 from Ewpratten/whirlpooltweaks

added velocity with friction, translucent shop back
This commit is contained in:
rsninja722 2021-04-26 13:59:25 -04:00 committed by GitHub
commit 3fbb50f985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 33 deletions

View File

@ -4,6 +4,8 @@ use crate::gamecore::GameCore;
const NORMAL_PLAYER_SPEED: i32 = 1; const NORMAL_PLAYER_SPEED: i32 = 1;
const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2;
const PLAYER_FRICTION: f32 = 1.05;
const WHIRLPOOL_PULL: f32 = 3.0;
const TURN_SPEED: f32 = 0.15; const TURN_SPEED: f32 = 0.15;
const BOOST_DECREASE_PER_SECOND: f32 = 0.65; const BOOST_DECREASE_PER_SECOND: f32 = 0.65;
const BOOST_REGEN_PER_SECOND: f32 = 0.25; const BOOST_REGEN_PER_SECOND: f32 = 0.25;
@ -157,18 +159,14 @@ pub fn update_player_movement(
.speed_increase; .speed_increase;
} }
let mut should_apply_friction: bool = true;
// Creates variable to calculate the distant
let mut movement_drift = Vector2::new(0.0, 0.0);
// Check each whirlpool for effects // Check each whirlpool for effects
for whirlpool in game_core.world.whirlpool.iter_mut(){ for whirlpool in game_core.world.whirlpool.iter_mut(){
// check if its in range and not to close // check if its in range and not to close
if game_core.player.position.distance_to(whirlpool.position) <= 100.0 && game_core.player.position.distance_to(whirlpool.position) >= 10.0{ if game_core.player.position.distance_to(whirlpool.position) <= 50.0 && game_core.player.position.distance_to(whirlpool.position) >= 10.0{
// Calculates info for formulas // Calculates info for formulas
@ -180,7 +178,7 @@ pub fn update_player_movement(
// Calculates force // Calculates force
let force = 15.0 / game_core.player.position.distance_to(whirlpool.position); let force = WHIRLPOOL_PULL / game_core.player.position.distance_to(whirlpool.position);
// Calculates componets of force // Calculates componets of force
let mut force_x = (force as f32 * angle.cos()).clamp(-5.0, 5.0); let mut force_x = (force as f32 * angle.cos()).clamp(-5.0, 5.0);
@ -196,56 +194,54 @@ pub fn update_player_movement(
} }
// Adds values to drift tracker // Adds values to drift tracker
movement_drift.x -= force_x; game_core.player.additional_vel.x -= force_x;
movement_drift.y -= force_y; game_core.player.additional_vel.y -= force_y;
should_apply_friction = false;
} }
} }
// Checks collision if should_apply_friction {
game_core.player.position.x += movement_drift.x; game_core.player.additional_vel.x /= PLAYER_FRICTION;
for collider in game_core.world.colliders.iter() { game_core.player.additional_vel.y /= PLAYER_FRICTION;
if game_core.player.collides_with_rec(collider) { if f32::round(game_core.player.additional_vel.x * 10.0) == 0.0 {
game_core.player.position.x -= movement_drift.x; game_core.player.additional_vel.x = 0.0;
break; }
} if f32::round(game_core.player.additional_vel.y * 10.0) == 0.0 {
game_core.player.additional_vel.y = 0.0;
}
} }
game_core.player.position.y += movement_drift.y; if !(raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0) {
for collider in game_core.world.colliders.iter() { player_real_movement = Vector2::zero();
if game_core.player.collides_with_rec(collider) { }
game_core.player.position.y -= movement_drift.y;
break;
}
}
// Handle movement and collisions // Handle movement and collisions
if raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0 if !game_core.player.is_stunned() {
&& !game_core.player.is_stunned()
{
if game_core.player.is_moving { if game_core.player.is_moving {
// move in x // move in x
game_core.player.position.x += player_real_movement.x; game_core.player.position.x += player_real_movement.x + game_core.player.additional_vel.x;
// Check for any collisions // Check for any collisions
for collider in game_core.world.colliders.iter() { for collider in game_core.world.colliders.iter() {
if game_core.player.collides_with_rec(collider) { if game_core.player.collides_with_rec(collider) {
game_core.player.position.x -= player_real_movement.x; game_core.player.position.x -= player_real_movement.x + game_core.player.additional_vel.x;
player_real_movement.x = 0.0; player_real_movement.x = 0.0;
game_core.player.additional_vel.x = 0.0;
break; break;
} }
} }
// move in y // move in y
game_core.player.position.y += player_real_movement.y; game_core.player.position.y += player_real_movement.y + game_core.player.additional_vel.y;
// Check for any collisions // Check for any collisions
for collider in game_core.world.colliders.iter() { for collider in game_core.world.colliders.iter() {
if game_core.player.collides_with_rec(collider) { if game_core.player.collides_with_rec(collider) {
game_core.player.position.y -= player_real_movement.y; game_core.player.position.y -= player_real_movement.y + game_core.player.additional_vel.y;
player_real_movement.y = 0.0; player_real_movement.y = 0.0;
game_core.player.additional_vel.y = 0.0;
break; break;
} }
} }
@ -268,7 +264,7 @@ pub fn update_player_movement(
// Camera only moves if you get close to the edge of the screen // Camera only moves if you get close to the edge of the screen
if player_screen_position.distance_to(window_center).abs() > 100.0 { if player_screen_position.distance_to(window_center).abs() > 100.0 {
game_core.master_camera.target += player_real_movement + movement_drift; game_core.master_camera.target += player_real_movement + game_core.player.additional_vel;
} }
// If the player is not on screen, snap the camera to them // If the player is not on screen, snap the camera to them

View File

@ -14,7 +14,7 @@ pub fn render_shop(
bounds: Rectangle, bounds: Rectangle,
) -> Option<GameState> { ) -> Option<GameState> {
// Render background // Render background
draw_handle.draw_rectangle_rec(bounds, Color::WHITE); draw_handle.draw_rectangle_rec(bounds, Color::new(255, 255, 255, 125));
draw_handle.draw_rectangle_lines_ex(bounds, 3, Color::BLACK); draw_handle.draw_rectangle_lines_ex(bounds, 3, Color::BLACK);
// Title // Title

View File

@ -32,6 +32,7 @@ impl PlayerInventory {
pub struct Player { pub struct Player {
pub position: Vector2, pub position: Vector2,
pub direction: Vector2, pub direction: Vector2,
pub additional_vel: Vector2,
pub size: Vector2, pub size: Vector2,
pub radius: f32, pub radius: f32,
pub coins: u32, pub coins: u32,
@ -48,6 +49,7 @@ pub struct Player {
impl Player { impl Player {
pub fn new(spawn: &Vector2) -> Self { pub fn new(spawn: &Vector2) -> Self {
Self { Self {
additional_vel: Vector2::zero(),
boost_percent: 1.0, boost_percent: 1.0,
size: Vector2 { x: 11.0, y: 21.0 }, size: Vector2 { x: 11.0, y: 21.0 },
breath_percent: 1.0, breath_percent: 1.0,