[game] Animation, looks bad
This commit is contained in:
parent
d5684b7da9
commit
951a2ce635
2 changed files with 28 additions and 1 deletions
BIN
game/bot.png
BIN
game/bot.png
Binary file not shown.
|
Before Width: | Height: | Size: 799 B After Width: | Height: | Size: 1.3 KiB |
29
game/main.ts
29
game/main.ts
|
|
@ -4,6 +4,9 @@ import { since_start } from "./time";
|
||||||
import { btn, Button } from "./input";
|
import { btn, Button } from "./input";
|
||||||
import { new_v2, vadd, vmul, vnorm } from "./vector";
|
import { new_v2, vadd, vmul, vnorm } from "./vector";
|
||||||
|
|
||||||
|
/// A nice looping frame counter.
|
||||||
|
let clock = 0;
|
||||||
|
|
||||||
let bot_sprite: number | undefined = undefined;
|
let bot_sprite: number | undefined = undefined;
|
||||||
|
|
||||||
// Note zelda overworld is 16x8 screens
|
// Note zelda overworld is 16x8 screens
|
||||||
|
|
@ -25,6 +28,8 @@ let robo_vel = new_v2(0);
|
||||||
let robo_pos = new_v2(10);
|
let robo_pos = new_v2(10);
|
||||||
|
|
||||||
export function update() {
|
export function update() {
|
||||||
|
clock = (clock + 1) % 20160;
|
||||||
|
|
||||||
// Acceleration from input
|
// Acceleration from input
|
||||||
let a = new_v2(0);
|
let a = new_v2(0);
|
||||||
if (btn(Button.Up)) {
|
if (btn(Button.Up)) {
|
||||||
|
|
@ -40,11 +45,20 @@ export function update() {
|
||||||
a.x += 1;
|
a.x += 1;
|
||||||
}
|
}
|
||||||
vnorm(a);
|
vnorm(a);
|
||||||
|
a = vmul(a, 0.4); // Speed.
|
||||||
|
|
||||||
// Friction
|
// Friction
|
||||||
let v = vmul(robo_vel, friction);
|
let v = vmul(robo_vel, friction);
|
||||||
v = vadd(v, a);
|
v = vadd(v, a);
|
||||||
|
|
||||||
|
// Zero if we're smaller than some epsilon.
|
||||||
|
if (Math.abs(v.x) < 0.01) {
|
||||||
|
v.x = 0;
|
||||||
|
}
|
||||||
|
if (Math.abs(v.y) < 0.01) {
|
||||||
|
v.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Motion
|
// Motion
|
||||||
let np = vadd(robo_pos, v);
|
let np = vadd(robo_pos, v);
|
||||||
|
|
||||||
|
|
@ -54,13 +68,26 @@ export function update() {
|
||||||
robo_vel = v;
|
robo_vel = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GARBAGE OBVIOUSLY
|
||||||
|
const robo_info = {
|
||||||
|
width: 32,
|
||||||
|
height: 32,
|
||||||
|
animations: [
|
||||||
|
{ start: 0, length: 2, speed: 20 },
|
||||||
|
{ start: 2, length: 8, speed: 8 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
export function draw() {
|
export function draw() {
|
||||||
cls(0.1, 0.2, 0.3);
|
cls(0.1, 0.2, 0.3);
|
||||||
if (bot_sprite != undefined) {
|
if (bot_sprite != undefined) {
|
||||||
// ...it gets resolved here?
|
// ...it gets resolved here?
|
||||||
use_texture(bot_sprite);
|
use_texture(bot_sprite);
|
||||||
|
|
||||||
spr(robo_pos.x, robo_pos.y, 32, 32, 0, 0, 32, 32);
|
const moving = robo_vel.x != 0 || robo_vel.y != 0;
|
||||||
|
const anim = robo_info.animations[moving ? 1 : 0];
|
||||||
|
const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0;
|
||||||
|
spr(robo_pos.x, robo_pos.y, 32, 32, frame * robo_info.width, 0, 32, 32);
|
||||||
}
|
}
|
||||||
// print("FRAME TIME:", since_last_frame());
|
// print("FRAME TIME:", since_last_frame());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue