precision-game/js/behaviours/sine_x.mjs
2023-10-27 15:01:03 +02:00

39 lines
1,022 B
JavaScript

import { register_behaviour } from "./core.mjs";
import * as entity from "../entity.mjs";
import { underride } from "../underride.mjs";
register_behaviour("sine_x", {
tick: (enemy, deltatime) => {
const {y} = entity.get_pos(enemy);
const meta = entity.get_meta(enemy);
const age = meta.get("age");
// `movement_speed` is in hertz and `Math.sin` input is in radians.
const age_factor = meta.get("movement_speed") * Math.PI * 2;
const offset = meta.get("offset") * Math.PI * 2;
const x = meta.get("center_x") + Math.sin(age * age_factor + offset) * meta.get("movement_scale");
entity.set_pos(enemy, x, y);
},
init: (enemy, params = {}) => {
params = underride(params, {
movement_scale: 100,
movement_speed: 1/4,
offset: 0,
});
const {x} = entity.get_pos(enemy);
const meta = entity.get_meta(enemy);
meta.set("center_x", x);
meta.set("movement_scale", params.movement_scale);
meta.set("movement_speed", params.movement_speed);
meta.set("offset", params.offset);
},
});