Fix faster diagonal acceleration for the player
All checks were successful
CI / Formatting (push) Successful in 1m27s

Also always accelerate the player using the same force, so that bigger players aren't overpowered.
This commit is contained in:
Michael Bradley 2025-05-24 00:43:44 -04:00
parent 10b525e4c7
commit 0896ccf691
Signed by: MichaelBradley
SSH key fingerprint: SHA256:o/aaeYtRubILK7OYYjYP12DmU7BsPUhKji1AgaQ+ge4
3 changed files with 41 additions and 18 deletions

View file

@ -1,3 +1,5 @@
use core::f32;
use avian2d::prelude::*;
use bevy::{
input::mouse::{AccumulatedMouseScroll, MouseScrollUnit},
@ -5,32 +7,46 @@ use bevy::{
prelude::*,
};
use super::objects::Player;
use super::objects::{Player, Radius};
/// Move the player character based on the keyboard input
pub fn move_player(
time: Res<Time>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut velocity: Single<&mut LinearVelocity, With<Player>>,
) -> Result {
let acceleration = 500.0;
mut query: Single<(&mut LinearVelocity, &Radius), With<Player>>,
) {
let (ref mut velocity, radius) = *query;
let delta_v = acceleration * time.delta_secs();
let acceleration_energy = 150000.0;
// Although it would arguably be more accurate to divide by pi to get the actual mass (or include the density
// somehow), `acceleration_energy` is in arbitrary units so we can just pretend it's proper physics
let delta_v = acceleration_energy * time.delta_secs() / radius.0.powi(2);
let mut direction = Vec2::ZERO;
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
velocity.y += delta_v;
direction.y += 1.0;
}
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
velocity.y -= delta_v;
direction.y -= 1.0;
}
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
velocity.x -= delta_v;
direction.x -= 1.0;
}
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
velocity.x += delta_v;
direction.x += 1.0;
}
Ok(())
// This would be easier if I could compare floats with 0 and still sleep at night
let circularize = if direction.x.abs() > 0.0 && direction.y.abs() > 0.0 {
f32::consts::FRAC_1_SQRT_2
} else {
1.0
};
// Kinda annoying I have to manually assign the fields here but I'll leave it in case I remember a better way later
velocity.x += direction.x * delta_v * circularize;
velocity.y += direction.y * delta_v * circularize;
}
/// Neatly exit game