Commit 1651cea3 authored by Rahix's avatar Rahix 🦀
Browse files

Merge 'Framerate and movement fixed'

Closes #19 and #20

See merge request !32
parents 7f9c2534 df971876
...@@ -3,6 +3,9 @@ use crate::utils; ...@@ -3,6 +3,9 @@ use crate::utils;
pub struct Clock { pub struct Clock {
perf: web_sys::Performance, perf: web_sys::Performance,
start: f64, start: f64,
last_tick: f64,
frame_delta: f32,
pub max_delta: f32,
} }
impl Clock { impl Clock {
...@@ -12,11 +15,24 @@ impl Clock { ...@@ -12,11 +15,24 @@ impl Clock {
.expect("cannot access `window.performance`"); .expect("cannot access `window.performance`");
let start = perf.now(); let start = perf.now();
Clock { perf, start } Clock {
perf,
start,
frame_delta: 0.03,
last_tick: start,
max_delta: 0.1,
}
} }
pub fn frame_delta(&self) -> f32 { pub fn frame_delta(&self) -> f32 {
0.1 self.frame_delta
}
pub fn update(&mut self, timestamp: f64) {
let timestamp = timestamp / 1000.0;
self.frame_delta = self.max_delta.min((timestamp - self.last_tick) as f32);
self.last_tick = timestamp;
crate::console_log!("Frame delta: {}", self.frame_delta());
} }
pub fn wall_time(&self) -> f64 { pub fn wall_time(&self) -> f64 {
......
...@@ -113,22 +113,30 @@ impl gamestate::State for InGameState { ...@@ -113,22 +113,30 @@ impl gamestate::State for InGameState {
.get_mut(&mut self.world, player) .get_mut(&mut self.world, player)
.unwrap(); .unwrap();
match event { match event {
gamestate::Event::KeyDown("w") => player_movable.velocity.y = -50.0, gamestate::Event::KeyDown("w") => player_movable.velocity.y = -300.0,
gamestate::Event::KeyUp("w") => player_movable.velocity.y = 0.0, gamestate::Event::KeyUp("w") => player_movable.velocity.y = 0.0,
gamestate::Event::KeyDown("a") => player_movable.velocity.x = -50.0, gamestate::Event::KeyDown("a") => player_movable.velocity.x = -300.0,
gamestate::Event::KeyUp("a") => player_movable.velocity.x = 0.0, gamestate::Event::KeyUp("a") => player_movable.velocity.x = 0.0,
gamestate::Event::KeyDown("s") => player_movable.velocity.y = 50.0, gamestate::Event::KeyDown("s") => player_movable.velocity.y = 300.0,
gamestate::Event::KeyUp("s") => player_movable.velocity.y = 0.0, gamestate::Event::KeyUp("s") => player_movable.velocity.y = 0.0,
gamestate::Event::KeyDown("d") => player_movable.velocity.x = 50.0, gamestate::Event::KeyDown("d") => player_movable.velocity.x = 300.0,
gamestate::Event::KeyUp("d") => player_movable.velocity.x = 0.0, gamestate::Event::KeyUp("d") => player_movable.velocity.x = 0.0,
_ => (), _ => (),
} }
if player_movable.velocity.x != 0.0 || player_movable.velocity.y != 0.0 {
player_movable.velocity = player_movable.velocity.normalize() * 300.0;
}
gamestate::Transition::Keep gamestate::Transition::Keep
} }
fn update(&mut self, _timestamp: f64) -> gamestate::Transition { fn update(&mut self, timestamp: f64) -> gamestate::Transition {
use legion::IntoQuery; use legion::IntoQuery;
self.resources
.get_mut::<resources::Clock>()
.unwrap()
.update(timestamp);
{ {
let rendering = self.resources.get::<resources::Rendering>().unwrap(); let rendering = self.resources.get::<resources::Rendering>().unwrap();
rendering.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); rendering.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment