[game] Tweaks

This commit is contained in:
John Doty 2023-08-23 20:10:22 -07:00
parent 38f5f95827
commit 756a3634c0
3 changed files with 16 additions and 16 deletions

View file

@ -56,7 +56,7 @@ export class Actor {
// this mean we do some "update" in render?
//
// TODO: Update physics in a better kind of way rather than an object call.
update_physics(level: Level | undefined) {
update_physics(level: Level) {
// This is very nice: https://deepnight.net/tutorial/a-simple-platformer-engine-part-1-basics/
// Apply friction to velocity and zero if we're close enough to zero.
const props = this.props;
@ -72,6 +72,9 @@ export class Actor {
let cy = props.cy;
// Adjust xr with velocity, check for collisions, etc.
// TODO: This code is somewhat wrong because it kinda assumes that we're
// the same size as our cell size, which is incorrect.
let xr = props.xr + velocity.x;
do {
// TODO: Cap velocity to 1 tile/frame? Then we wouldn't need this loop...
@ -113,8 +116,7 @@ export class Actor {
}
} while (yr > 1 || yr < 0);
// TODO: Collision detection
// const { w, h } = this.bounds;
// TODO: Entity collision detection
const new_position = new_v2((cx + xr) * 16, (cy + yr) * 16);
@ -167,7 +169,7 @@ export class Robo extends Actor {
a.x += 1;
}
vnorm(a);
this.props.velocity = vadd(this.props.velocity, vmul(a, 0.06));
this.props.velocity = vadd(this.props.velocity, vmul(a, 0.07));
}
draw(clock: number) {

View file

@ -262,11 +262,7 @@ export async function load_world(path: string): Promise<World> {
return { levels, tilesets };
}
export function has_collision(
level: Level | undefined,
cx: number,
cy: number
): boolean {
export function has_collision(level: Level, cx: number, cy: number): boolean {
if (!level) return true;
if (cx < 0 || cx >= level.cw) return false;
if (cy < 0 || cy >= level.ch) return false;

View file

@ -22,9 +22,9 @@ let actors: Actor[] = [];
// zelda screen is 16x11 tiles
// from a feeling point of view this is sufficient, apparently :D
function load_assets() {
function start_load_assets() {
// Start this load, but then...
let map_load = load_world("./overworld.ldtk").then((w) => {
load_world("./overworld.ldtk").then((w) => {
print("World loaded at", since_start());
world = w;
@ -47,17 +47,13 @@ function load_assets() {
}
}
});
Promise.all([map_load]).then(() => {
print("All are loaded.");
});
}
// TODO: Build a system whereby the signatures of the fundamental functions can be checked.
export function init() {
print("Hello world!");
load_assets();
start_load_assets();
}
interface Snapshot {
@ -66,6 +62,7 @@ interface Snapshot {
}
export function suspend(): Snapshot {
print("Suspend! ", actors.length, "actors");
return {
clock,
actors: actors.map((a) => {
@ -78,10 +75,15 @@ export function resume(snapshot: Snapshot | undefined) {
if (snapshot) {
clock = snapshot.clock || 0;
actors = snapshot.actors.map((s) => spawn_actor(s.type, s.props));
print("Resume! ", actors.length, "actors");
}
}
export function update() {
if (!level) {
return;
}
clock = (clock + 1) % 20160;
for (const actor of actors) {