@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
40 lines (39 loc) • 989 B
JavaScript
class Dash {
/**
* Creates a dash movement.
*
* @param speed - Movement speed in units per second
* @param direction - Direction vector (will be normalized)
* @param duration - Duration in seconds
*/
constructor(speed, direction, duration) {
this.speed = speed;
this.duration = duration;
this.elapsed = 0;
this.finished = false;
const magnitude = Math.hypot(direction.x, direction.y);
this.direction = magnitude > 0 ? { x: direction.x / magnitude, y: direction.y / magnitude } : { x: 1, y: 0 };
}
update(body, dt) {
if (this.finished) {
return;
}
this.elapsed += dt;
if (this.elapsed <= this.duration) {
body.setVelocity({
x: this.direction.x * this.speed,
y: this.direction.y * this.speed
});
} else {
body.setVelocity({ x: 0, y: 0 });
this.finished = true;
}
}
isFinished() {
return this.finished;
}
}
export {
Dash
};
//# sourceMappingURL=index33.js.map