gibbon.js
Version:
Actor/Component system for use with pixi.js.
53 lines • 1.61 kB
JavaScript
import { Component } from '../core/component';
import { Mover } from './mover';
/**
* Linearly interpolate Actor between two points over
* a defined time frame.
*/
export class LerpMover extends Component {
lerpTimeSec = 300;
target = { x: 0, y: 0 };
/**
* Time spent on current interpolation.
*/
deltaTime = 0;
lerping = false;
/**
* Whether to add current velocity to target point
* every frame.
*/
addVelocity = false;
setDest(target) {
if (target) {
this.target.x = target.x;
this.target.y = target.y;
this.lerping = true;
this.deltaTime = 0;
}
else {
this.lerping = false;
}
}
update(deltaS) {
if (this.lerping && this.target != null) {
this.deltaTime += deltaS;
if (this.addVelocity) {
const mover = this.get(Mover);
if (mover) {
this.position.x += mover.x * deltaS;
this.position.y += mover.y * deltaS;
}
}
const t = this.deltaTime / this.lerpTimeSec;
if (t >= 1) {
this.position.x = this.target.x;
this.position.y = this.target.y;
this.lerping = false;
}
else {
this.position.set(this.position.x * (1 - t) + t * this.target.x, this.position.y * (1 - t) + t * this.target.y);
}
}
}
}
//# sourceMappingURL=lerp-mover.js.map