Phunny Fysics
This commit is contained in:
parent
e55b54a313
commit
84633f00a0
5
game/dist/project-constants.json
vendored
5
game/dist/project-constants.json
vendored
@ -16,5 +16,10 @@
|
|||||||
"details.sm_failure": "Game went FUBAR",
|
"details.sm_failure": "Game went FUBAR",
|
||||||
"details.main_menu": "In the main menu"
|
"details.main_menu": "In the main menu"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"player": {
|
||||||
|
"max_velocity": 3,
|
||||||
|
"acceleration": 2,
|
||||||
|
"deceleration": 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,20 @@ pub struct DiscordConstants {
|
|||||||
pub strings: HashMap<String, String>,
|
pub strings: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constants relating to the Player
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct PlayerConstants {
|
||||||
|
|
||||||
|
/// Maximum velocity, tiles per second
|
||||||
|
pub max_velocity: u32,
|
||||||
|
|
||||||
|
/// Acceleration, tiles per second per second
|
||||||
|
pub acceleration: u32,
|
||||||
|
|
||||||
|
/// Deceleration, tiles per second per second
|
||||||
|
pub deceleration: u32,
|
||||||
|
}
|
||||||
|
|
||||||
/// This structure is filled with the contents of `dist/project-constants.json` at runtime
|
/// This structure is filled with the contents of `dist/project-constants.json` at runtime
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ProjectConstants {
|
pub struct ProjectConstants {
|
||||||
@ -40,6 +54,9 @@ pub struct ProjectConstants {
|
|||||||
/// The Discord constants
|
/// The Discord constants
|
||||||
pub discord: DiscordConstants,
|
pub discord: DiscordConstants,
|
||||||
|
|
||||||
|
/// The Player constants
|
||||||
|
pub player: PlayerConstants,
|
||||||
|
|
||||||
/// The target framerate of the game
|
/// The target framerate of the game
|
||||||
pub target_fps: u32,
|
pub target_fps: u32,
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ impl SceneRenderDelegate {
|
|||||||
.render_frame(raylib, rl_thread, &discord, global_resources, constants)
|
.render_frame(raylib, rl_thread, &discord, global_resources, constants)
|
||||||
.await;
|
.await;
|
||||||
self.scene_playable
|
self.scene_playable
|
||||||
.update_physics(raylib)
|
.update_physics(raylib, constants)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,24 +102,45 @@ impl PlayableScene {
|
|||||||
pub async fn update_physics(
|
pub async fn update_physics(
|
||||||
&mut self,
|
&mut self,
|
||||||
raylib: & raylib::RaylibHandle,
|
raylib: & raylib::RaylibHandle,
|
||||||
|
constants: &ProjectConstants,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
// Get time since last physics update
|
||||||
let time = SystemTime::now();
|
let time = SystemTime::now();
|
||||||
if time
|
let elapsed = time.duration_since(self.last_update).expect("Time Appears to Have Moved Backwards!");
|
||||||
.duration_since(self.last_update)
|
|
||||||
.expect("Time Appears to Have Moved Backwards!")
|
|
||||||
.as_millis() < 16
|
|
||||||
{
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
self.last_update = time;
|
self.last_update = time;
|
||||||
|
let delta_time = elapsed.as_millis() as f32 / 1000.0; // Physics will be scaled by this value
|
||||||
|
|
||||||
let player = &mut self.player;
|
let player = &mut self.player;
|
||||||
|
|
||||||
|
// Get input direction components
|
||||||
let h_axis = raylib.is_key_down(KeyboardKey::KEY_D) as i8 - raylib.is_key_down(KeyboardKey::KEY_A) as i8;
|
let h_axis = raylib.is_key_down(KeyboardKey::KEY_D) as i8 - raylib.is_key_down(KeyboardKey::KEY_A) as i8;
|
||||||
let v_axis = raylib.is_key_down(KeyboardKey::KEY_W) as i8 - raylib.is_key_down(KeyboardKey::KEY_S) as i8;
|
let v_axis = raylib.is_key_down(KeyboardKey::KEY_W) as i8 - raylib.is_key_down(KeyboardKey::KEY_S) as i8;
|
||||||
let direction = na::Vector2::new(h_axis as f32, v_axis as f32);
|
if h_axis != 0 || v_axis != 0 {
|
||||||
player.velocity += &direction;
|
// Normalize input and accelerate in desired direction
|
||||||
player.position += &player.velocity;
|
let direction = na::Vector2::new(h_axis as f32, v_axis as f32).normalize();
|
||||||
|
player.velocity += &direction.xy()
|
||||||
|
* constants.player.acceleration as f32
|
||||||
|
* constants.tile_size as f32
|
||||||
|
* delta_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
if player.velocity.magnitude() != 0.0 {
|
||||||
|
player.velocity -= player.velocity.normalize()
|
||||||
|
* constants.player.deceleration as f32
|
||||||
|
* constants.tile_size as f32
|
||||||
|
* delta_time;
|
||||||
|
if player.velocity.magnitude() < 0.01 {
|
||||||
|
player.velocity.set_magnitude(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((constants.player.max_velocity * constants.tile_size) as f32)
|
||||||
|
< player.velocity.magnitude() {
|
||||||
|
player.velocity.set_magnitude((constants.player.max_velocity * constants.tile_size) as f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
player.position += &player.velocity * delta_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user