gibbon.js
Version:
Actor/Component system for use with pixi.js.
106 lines • 3.36 kB
JavaScript
import Component from "../component";
import { Point } from "pixi.js";
/**
* VirtualMover tracks velocity, acceleration, and rotation
* while leaving the actual position placement to another component.
*/
export default class VirtualMover extends Component {
/**
* @property {number} rotation - wraps gameObject rotation in radians.
*/
get rotation() { return this.clip.rotation; }
set rotation(v) {
if (v > Math.PI)
v -= 2 * Math.PI;
else if (v < -Math.PI)
v += 2 * Math.PI;
this.clip.rotation = v;
}
/**
* @property {PIXI.Point} velocity
*/
get velocity() { return this._velocity; }
set velocity(v) { this._velocity.set(v.x, v.y); }
/**
* @property {PIXI.Point} accel
*/
get accel() { return this._accel; }
set accel(v) {
if (v.x === 0 && v.y === 0) {
this._accel.set(0, 0);
}
else if (this._accelMax > 0) {
const d = this._accelMax / Math.sqrt(v.x * v.x + v.y * v.y);
this._accel.set(d * v.x, d * v.y);
}
else {
this._accel.set(v.x, v.y);
}
}
/**
* @property {number} velocityMax - Maximum absolute value of velocity.
*/
get velocityMax() { return this._speedMax; }
set velocityMax(v) { this._speedMax = v; }
/**
* @property {number} accelMax
*/
get accelMax() { return this._accelMax; }
set accelMax(v) { this._accelMax = v; }
/**
* @property {number} omegaAcc
*/
get omegaAcc() { return this._omegaAcc; }
set omegaAcc(v) { this._omegaAcc = v; }
/**
* @property {number} omega - angular velocity in radians/frame.
*/
get omega() { return this._omega; }
set omega(v) { this._omega = v; }
/**
* @property {number} omegaMax
*/
get omegaMax() { return this._omegaMax; }
set omegaMax(v) { this._omegaMax = v; }
_velocity = new Point();
_accel = new Point();
_speedMax = 4;
_accelMax = 1;
_omegaAcc = 0;
_omega = 0;
_omegaMax = Math.PI / 40;
_rotation = 0;
_position = new Point();
init() {
this._position.copyFrom(this.gameObject.position);
this._rotation = this.gameObject.rotation;
}
/**
* Set mover velocity.
* @param {number} vx
* @param {number} vy
*/
set(vx, vy) {
this._velocity.set(vx, vy);
}
update(delta) {
if (this._omegaAcc !== 0)
this._omega += this._omegaAcc * delta;
if (this._omega > this._omegaMax)
this._omega = this._omegaMax;
else if (this._omega < -this._omegaMax)
this._omega = -this._omegaMax;
this._rotation += this._omega * delta;
const vel = this._velocity;
vel.x += this._accel.x * delta;
vel.y += this._accel.y * delta;
let abs = vel.x * vel.x + vel.y * vel.y;
if (abs > this._speedMax * this._speedMax) {
abs = this._speedMax / Math.sqrt(abs);
vel.set(abs * vel.x, abs * vel.y);
}
this._position.x = this._position.x + vel.x * delta;
this._position.y = this._position.y + vel.y * delta;
}
}
//# sourceMappingURL=virtual-mover.js.map