39 lines
1,022 B
JavaScript
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_y", {
|
|
tick: (enemy, deltatime) => {
|
|
const {x} = 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 y = meta.get("center_y") + 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 {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);
|
|
},
|
|
});
|