@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
70 lines (52 loc) • 1.65 kB
JavaScript
import { EntityBehavior } from "../ecs/EntityBehavior.js";
import { Transform } from "../../../ecs/transform/Transform.js";
import Vector3 from "../../../../core/geom/Vector3.js";
import Quaternion from "../../../../core/geom/Quaternion.js";
import { BehaviorStatus } from "../BehaviorStatus.js";
export class RotationBehavior extends EntityBehavior {
constructor() {
super();
/**
* Rotation per second
* @type {Vector3}
*/
this.axis = new Vector3();
this.speed = 1;
this.angle = 0;
this.__offset = new Quaternion();
}
static fromJSON(j) {
const r = new RotationBehavior();
r.fromJSON(j);
return r;
}
/**
*
* @param axis
* @param speed
* @param angle
*/
fromJSON({ axis = Vector3.up, speed = 1, angle = 0 }) {
this.axis.fromJSON(axis);
this.speed = speed;
this.angle = angle;
}
initialize(context) {
super.initialize(context);
const transform = this.ecd.getComponent(this.entity, Transform);
this.__offset.copy(transform.rotation);
}
tick(timeDelta) {
/**
*
* @type {Transform}
*/
const transform = this.ecd.getComponent(this.entity, Transform);
const angleDelta = this.speed * timeDelta;
this.angle += angleDelta;
const r = transform.rotation;
r.fromAxisAngle(this.axis, this.angle);
r.multiplyQuaternions(r, this.__offset);
return BehaviorStatus.Running;
}
}