precision-game/js/player.mjs

60 lines
1.3 KiB
JavaScript

import { assert } from "./test.mjs";
import * as Entity from "./entity.mjs";
import * as Input from "./input.mjs";
import { register_tick } from "./tick.mjs";
import * as Event from "./event.mjs";
import { underride } from "./underride.mjs";
const player = Entity.create(0, 0, 20, 20);
export function get_player () { return player; }
function tick_player (deltatime) {
const meta = Entity.get_meta(player);
const speed = meta.get("speed");
let delta_x = 0;
if (Input.pressed("ArrowLeft")) delta_x -= speed;
if (Input.pressed("ArrowRight")) delta_x += speed;
let delta_y = 0;
if (Input.pressed("ArrowUp")) delta_y -= speed;
if (Input.pressed("ArrowDown")) delta_y += speed;
Entity.move(player, delta_x * deltatime, delta_y * deltatime);
}
register_tick(tick_player);
Event.listen("level/unload", _ => {
Entity.set_visible(player, false);
Entity.set_physical(player, false);
});
Event.listen("level/load/player", (data = {}) => {
data = underride(data, {
pos: {
x: 0,
y: 0,
},
size: {
x: 20,
y: 20,
},
speed: 100,
});
Entity.set_pos(player, data.pos.x, data.pos.y);
Entity.set_size(player, data.size.x, data.size.y);
const meta = Entity.get_meta(player);
meta.set("speed", data.speed);
Entity.set_visible(player, true);
Entity.set_physical(player, true);
});