8867a60c94
the "level" will now restart (currently to a hardcoded state) when the player touches an enemy.
52 lines
1,020 B
JavaScript
52 lines
1,020 B
JavaScript
import * as entity from "./entity.mjs";
|
|
import { register_tick } from "./tick.mjs";
|
|
import * as behaviours from "./behaviours/main.mjs";
|
|
|
|
|
|
|
|
const enemies = [];
|
|
|
|
function tick_enemy (enemy, deltatime) {
|
|
const meta = entity.get_meta(enemy);
|
|
const age = meta.get("age") + deltatime;
|
|
meta.set("age", age);
|
|
|
|
if (meta.has("behaviour")) {
|
|
const b = meta.get("behaviour");
|
|
if (behaviours.exists(b)) {
|
|
const callback = behaviours.get_tick(b);
|
|
callback(enemy, deltatime);
|
|
}
|
|
}
|
|
}
|
|
|
|
register_tick(deltatime => {
|
|
for (const enemy of enemies) {
|
|
tick_enemy(enemy, deltatime);
|
|
}
|
|
});
|
|
|
|
export function get_all () {
|
|
return enemies;
|
|
}
|
|
|
|
export function create (...args) {
|
|
const enemy = entity.create(...args);
|
|
|
|
const meta = entity.get_meta(enemy);
|
|
meta.set("age", 0);
|
|
|
|
return enemy;
|
|
}
|
|
|
|
export function set_behaviour (enemy, behaviour, params) {
|
|
behaviours.init(enemy, behaviour, params);
|
|
}
|
|
|
|
export function add (enemy) {
|
|
enemies.push(enemy);
|
|
}
|
|
|
|
export function remove_all () {
|
|
enemies.splice(0, enemies.length);
|
|
}
|