51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import { register_behaviour } from "./core.mjs";
|
|
|
|
import * as entity from "../entity.mjs";
|
|
import { underride } from "../underride.mjs";
|
|
|
|
|
|
|
|
function lerp (t, start, end) {
|
|
return start + t * (end - start);
|
|
}
|
|
|
|
|
|
|
|
register_behaviour("path", {
|
|
tick: (enemy, deltatime) => {
|
|
const meta = entity.get_meta(enemy);
|
|
const age = meta.get("age");
|
|
|
|
let pos = meta.get("pos");
|
|
const points = meta.get("points");
|
|
const speeds = meta.get("speeds");
|
|
|
|
const phase_count = points.length;
|
|
const phase_current = Math.floor(pos) % phase_count;
|
|
const phase_previous = ((phase_current - 1) + phase_count) % phase_count;
|
|
|
|
const start = points[phase_previous];
|
|
const end = points[phase_current];
|
|
|
|
const pos_in_phase = pos % 1;
|
|
|
|
const x = lerp(pos_in_phase, start[0], end[0]);
|
|
const y = lerp(pos_in_phase, start[1], end[1]);
|
|
|
|
entity.set_pos(enemy, x, y);
|
|
|
|
pos += deltatime * speeds[phase_previous];
|
|
meta.set("pos", pos);
|
|
},
|
|
init: (enemy, params = {}) => {
|
|
params = underride(params, {
|
|
offset: 0,
|
|
});
|
|
|
|
const meta = entity.get_meta(enemy);
|
|
|
|
meta.set("pos", params.offset);
|
|
meta.set("points", params.points);
|
|
meta.set("speeds", params.speeds);
|
|
},
|
|
});
|