friction
This commit is contained in:
+7
-2
@@ -21,11 +21,16 @@ pub const MARGIN_BELLOW: f32 = 400.0;
|
||||
// Player
|
||||
pub const PLAYER_HITBOX: Vec2 = Vec2::new(20.0, 40.0);
|
||||
pub const FALL_SPEED: f32 = 500.0;
|
||||
pub const DIVE_SPEED: f32 = 2700.0;
|
||||
pub const HORIZONTAL_SPEED: f32 = 450.0;
|
||||
pub const DIVE_SPEED: f32 = 2000.0;
|
||||
pub const HORIZONTAL_SPEED: f32 = 400.0;
|
||||
pub const START_POSITION: Vec3 = Vec3::new(0.0, 0.0, 2.0);
|
||||
pub const CAMERAE_OFFSET: f32 = 280.0;
|
||||
|
||||
// Movement intertia
|
||||
pub const HORIZONTAL_ACCEL: f32 = 2600.0;
|
||||
pub const HORIZONTAL_FRICTION: f32 = 1800.0;
|
||||
pub const VERTICAL_RATE: f32 = 4000.0;
|
||||
|
||||
// Validator
|
||||
pub const INERTIA_MARGIN: f32 = 4.0;
|
||||
pub const DESIGN_LATERAL_SPEED_RATIO: f32 = 0.6;
|
||||
|
||||
+57
-13
@@ -2,9 +2,12 @@ use bevy::prelude::*;
|
||||
|
||||
use crate::config::DIVE_SPEED;
|
||||
use crate::config::FALL_SPEED;
|
||||
use crate::config::HORIZONTAL_ACCEL;
|
||||
use crate::config::HORIZONTAL_FRICTION;
|
||||
use crate::config::HORIZONTAL_SPEED;
|
||||
use crate::config::PLAYER_HITBOX;
|
||||
use crate::config::START_POSITION;
|
||||
use crate::config::VERTICAL_RATE;
|
||||
use crate::gameplay::collision::Collider;
|
||||
use crate::gameplay::collision::check_collision_with_player;
|
||||
use crate::states::GameState;
|
||||
@@ -34,9 +37,43 @@ fn spawn_player(mut commands: Commands) {
|
||||
));
|
||||
}
|
||||
|
||||
// Move value to target by max_step
|
||||
fn approach(value: f32, target: f32, max_step: f32) -> f32 {
|
||||
if value < target {
|
||||
(value + max_step).min(target)
|
||||
} else {
|
||||
(value - max_step).max(target)
|
||||
}
|
||||
}
|
||||
|
||||
// For debug
|
||||
#[derive(Resource)]
|
||||
pub struct MoveSettings {
|
||||
pub horizontal_speed: f32,
|
||||
pub horizontal_accel: f32,
|
||||
pub horizontal_friction: f32,
|
||||
pub fall_speed: f32,
|
||||
pub dive_speed: f32,
|
||||
pub vertical_rate: f32,
|
||||
}
|
||||
|
||||
impl Default for MoveSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
horizontal_speed: HORIZONTAL_SPEED,
|
||||
horizontal_accel: HORIZONTAL_ACCEL,
|
||||
horizontal_friction: HORIZONTAL_FRICTION,
|
||||
fall_speed: FALL_SPEED,
|
||||
dive_speed: DIVE_SPEED,
|
||||
vertical_rate: VERTICAL_RATE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Input + movement system for the player
|
||||
pub(crate) fn move_player(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
settings: Res<MoveSettings>,
|
||||
time: Res<Time>,
|
||||
mut player: Query<(&mut Velocity, &mut Transform), With<Player>>,
|
||||
mut fall_started: ResMut<FallStarted>,
|
||||
@@ -66,21 +103,27 @@ pub(crate) fn move_player(
|
||||
0.0
|
||||
};
|
||||
|
||||
// let facteur = 1.0 - (-RATE * time.delta_secs()).exp();
|
||||
let target = Vec2::new(
|
||||
direction * HORIZONTAL_SPEED,
|
||||
-if input.pressed(KeyCode::KeyS) {
|
||||
DIVE_SPEED
|
||||
} else {
|
||||
FALL_SPEED
|
||||
},
|
||||
let dt = time.delta_secs();
|
||||
|
||||
// Inertia
|
||||
if direction != 0.0 {
|
||||
velocity.0.x = approach(
|
||||
velocity.0.x,
|
||||
direction * settings.horizontal_speed,
|
||||
settings.horizontal_accel * dt,
|
||||
);
|
||||
} else {
|
||||
velocity.0.x = approach(velocity.0.x, 0.0, settings.horizontal_friction * dt);
|
||||
}
|
||||
let vertical_target = if input.pressed(KeyCode::KeyS) {
|
||||
-settings.dive_speed
|
||||
} else {
|
||||
-settings.fall_speed
|
||||
};
|
||||
velocity.0.y = approach(velocity.0.y, vertical_target, settings.vertical_rate * dt);
|
||||
|
||||
// velocity.0 = velocity.0.lerp(target, facteur);
|
||||
velocity.0 = target;
|
||||
|
||||
// Position update (no smoothness in velocity)
|
||||
transform.translation += velocity.0.extend(0.0) * time.delta_secs();
|
||||
// Position update
|
||||
transform.translation += velocity.0.extend(0.0) * dt;
|
||||
}
|
||||
|
||||
// Debug
|
||||
@@ -119,6 +162,7 @@ impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::InGame), (spawn_player, start_run))
|
||||
.init_resource::<FallStarted>()
|
||||
.init_resource::<MoveSettings>()
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user