[game] Walk controls (unbound)

This commit is contained in:
John Doty 2023-07-07 06:21:01 -07:00
parent 895098e17f
commit c934914ac5
3 changed files with 97 additions and 4 deletions

View file

@ -1,6 +1,8 @@
import { cls, print, spr, use_texture } from "./graphics";
import { load_texture } from "./assets";
import { since_start, since_last_frame } from "./time";
import { since_start } from "./time";
import { btn, Button } from "./input";
import { new_v2, vadd, vmul, vnorm } from "./vector";
let bot_sprite: number | undefined = undefined;
@ -14,7 +16,39 @@ export function init() {
});
}
export function update() {}
const friction = 0.6;
let robo_vel = new_v2(0);
let robo_pos = new_v2(10);
export function update() {
// Acceleration from input
let a = new_v2(0);
if (btn(Button.Up)) {
a.y -= 1;
}
if (btn(Button.Down)) {
a.y += 1;
}
if (btn(Button.Left)) {
a.x -= 1;
}
if (btn(Button.Right)) {
a.x += 1;
}
vnorm(a);
// Friction
let v = vmul(robo_vel, friction);
v = vadd(v, a);
// Motion
let np = vadd(robo_pos, v);
// TODO: Collide.
robo_pos = np;
robo_vel = v;
}
export function draw() {
cls(0.1, 0.2, 0.3);
@ -22,8 +56,7 @@ export function draw() {
// ...it gets resolved here?
use_texture(bot_sprite);
let y = (since_start() * 10) % 240;
spr(10, y, 32, 32, 0, 0);
spr(robo_pos.x, robo_pos.y, 32, 32, 0, 0);
}
// print("FRAME TIME:", since_last_frame());
}