Dead zone

Watch a background landmark while the character moves inside the box. What finally makes the landmark shift?
A box in the middle of the screen where the player can move without moving the camera.
Small movements stay inside the box and the view holds still. Only when the player pushes against an edge does the camera scroll. It keeps the picture calm in games with lots of quick back-and-forth.
Code
namespace dead_zone_lesson;
import stage;
import std;
import math;
var camera = 0.0;
var started = false;
export "dead-zone" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
let x = 210 + sin(time * 0.9) * 70 + sin(time * 0.37) * 60;
if !started {
camera = x - 80;
started = true;
}
let left = 52.0;
let right = 108.0;
if x - camera < left {
camera = x - left;
}
if x - camera > right {
camera = x - right;
}
let flip = cos(time * 0.9) * 63 + cos(time * 0.37) * 22 < 0;
view_start(clamp(camera, 0.0, 260.0), 0);
world(GY, -1000, 1000);
actor(x, GY, walk(time), 2, "base", "director", flip);
restore();
let edge = x - camera <= left + 0.5 || x - camera >= right - 0.5;
opacity(0.18);
rect(left, 36, right - left, GY - 36, if edge { MAG } else { WHITE });
opacity(1);
dash_box(left + 0.5, 36.5, right - left - 1, GY - 37, MAG);
}

Comments
No comments yet.