precision-game/js/behaviours/sine_y.mjs

40 lines
1,022 B
JavaScript
Raw Normal View History

2023-10-27 12:11:18 +00:00
import { register_behaviour } from "./core.mjs";
import * as entity from "../entity.mjs";
import { underride } from "../underride.mjs";
register_behaviour("sine_y", {
tick: (enemy, deltatime) => {
const {x} = entity.get_pos(enemy);
const meta = entity.get_meta(enemy);
const age = meta.get("age");
2023-10-27 12:11:18 +00:00
// `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 y = meta.get("center_y") + Math.sin(age * age_factor + offset) * meta.get("movement_scale");
2023-10-27 12:11:18 +00:00
entity.set_pos(enemy, x, y);
},
init: (enemy, params = {}) => {
params = underride(params, {
movement_scale: 100,
movement_speed: 1/4,
offset: 0,
2023-10-27 12:11:18 +00:00
});
const {y} = entity.get_pos(enemy);
const meta = entity.get_meta(enemy);
meta.set("center_y", y);
meta.set("movement_scale", params.movement_scale);
meta.set("movement_speed", params.movement_speed);
meta.set("offset", params.offset);
2023-10-27 12:11:18 +00:00
},
});