game_actor_camera_effect_shake_effect.js
import Vec2 from "../../../vector/vec2.js";
/**
* @module CameraShake
* @fileoverview Contains CameraShake class.
*/
/**
* @class
* Used to add a shake effect to the game camera.
*/
class CameraShake {
/**
* Create a new CameraShake Object.
* @param {Camera} camera Camera to be owned by
* @param {number} strength Intensity of the effect
* @param {number} length Duration of the effect
*/
constructor(camera, strength, length = 1) {
this.camera = camera;
this.strength = strength;
this.length = length;
this.val = new Vec2(0, 0);
this._make();
}
/**
* Update the camera shake effect.
* @param {number} elapsed Time since last update in seconds
*/
update(elapsed) {
if (this.val.dist(this.to) < 4) {
this.strength -= elapsed * 100 / this.length;
if (this.strength <= 0) {
this.remove();
return;
}
this._make();
}
const diff = this.to.sub(this.val).norm();
this.val.x += diff.x * elapsed * this.strength * 50;
this.val.y += diff.y * elapsed * this.strength * 50;
}
/**
* Initializes the effect.
*/
_make() {
this.to = new Vec2(
(Math.random() * 2 - 1) * this.strength,
(Math.random() * 2 - 1) * this.strength
);
}
/**
* Remove the effect from the camera.
*/
remove() {
this.camera.shake = null;
}
}
export default CameraShake;