implemented a minimum physics rate
This commit is contained in:
parent
b221f65748
commit
4e95e2efca
2 changed files with 25 additions and 19 deletions
|
@ -4,10 +4,11 @@ var memory;
|
|||
var exports;
|
||||
const width = 160;
|
||||
const height = 144;
|
||||
const TIME_GAIN = 0.1; // game is unplayable at full speed with wasm refresh rates
|
||||
|
||||
function blit_frame() {
|
||||
// this has an insane performance impact, but it is required when using an
|
||||
// allocator from wasm, as there is no way to update the internal pointer
|
||||
// this is required when using an allocator from wasm as there is
|
||||
// no way to update the internal pointer when linear memory shifts
|
||||
image = new ImageData(
|
||||
new Uint8ClampedArray(
|
||||
memory.buffer,
|
||||
|
@ -17,7 +18,7 @@ function blit_frame() {
|
|||
width,
|
||||
);
|
||||
|
||||
ctx.putImageData(image, 0, 0, 0, 0, width-1, height-1);
|
||||
ctx.putImageData(image, 0, 0);
|
||||
}
|
||||
|
||||
function blit_text(text, len, x, y, size) {
|
||||
|
@ -77,7 +78,7 @@ async function init() {
|
|||
}
|
||||
|
||||
const FRAME_TIME = new Float32Array(exports.memory.buffer, exports.LAST_FRAME_TIME, 1);
|
||||
FRAME_TIME[0] = elapsed;
|
||||
FRAME_TIME[0] = elapsed * TIME_GAIN;
|
||||
|
||||
instance.exports.frame_entry();
|
||||
|
||||
|
|
|
@ -111,8 +111,8 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
|
|||
let mut camera = CAMERA.lock();
|
||||
let mut boat = BOAT.lock();
|
||||
|
||||
let gain = unsafe { // very much an approximation of constant-velocity animation
|
||||
LAST_FRAME_TIME.min(100.0) / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps
|
||||
let mut gain = unsafe { // very much an approximation of constant-velocity animation
|
||||
LAST_FRAME_TIME / (1000.0 / 20.0) // normalize to 20fps, cap simulation at 10fps
|
||||
};
|
||||
while let Some(key) = INPUTS.lock().pop() {
|
||||
match key[0] { // [tag:input_handler]
|
||||
|
@ -141,12 +141,15 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
|
|||
}
|
||||
|
||||
let wind = 0.0/RAD_TO_DEG;
|
||||
|
||||
let step = 1.0; // 50ms
|
||||
while gain > 0.0 { // when draw fps is low, physics will still run at a minimum of 20fps
|
||||
gain -= step;
|
||||
let gain = gain + step;
|
||||
|
||||
let vel = boat.get_velocity(wind);
|
||||
|
||||
let camera_vec = Vector2::new(camera[0],camera[1]);
|
||||
let boat_pos = boat.get_pos();
|
||||
|
||||
let depth = -sample_world(boat_pos+HALF, rand);
|
||||
let depth = -sample_world(boat.get_pos()+HALF, rand);
|
||||
if depth < -0.04 {
|
||||
boat.vel = 0.0;
|
||||
} else if depth < 0.0 {
|
||||
|
@ -157,9 +160,11 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
|
|||
boat.vel = noise::lerp(boat.vel, vel * 0.82, 0.13 * gain);
|
||||
boat.go(gain);
|
||||
}
|
||||
}
|
||||
|
||||
// draw sea
|
||||
const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0);
|
||||
let camera_vec = Vector2::new(camera[0],camera[1]);
|
||||
|
||||
#[cfg(feature = "rayon")]
|
||||
let buffer_iter = buffer.par_iter_mut();
|
||||
|
@ -173,7 +178,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
|
|||
point -= HALF;
|
||||
point *= camera[2];
|
||||
point += HALF;
|
||||
let n = sample_world(point+camera_vec+boat_pos, rand);
|
||||
let n = sample_world(point+camera_vec+boat.get_pos(), rand);
|
||||
*pix.1 =
|
||||
if n > 0.1 {
|
||||
let n = (n+0.1) * 300.0;
|
||||
|
|
Loading…
Reference in a new issue