12345678910111213141516171819202122232425262728293031 |
- import Vec2 from "./Vec2.js";
-
- export default class Shaker {
- constructor(decay = 7) {
- this.vec = new Vec2();
- this.decay = decay;
- this.shakeX = 0;
- this.shakeY = 0;
- }
-
- shake(x, y = x) {
- this.shakeX = x;
- this.shakeY = y;
- }
-
- update(dt) {
- if (this.shakeX > 0.1)
- this.vec.x = (Math.random() - 0.5) * this.shakeX * dt;
- else
- this.vec.x = 0;
-
- if (this.shakeY > 0.1)
- this.vec.y = (Math.random() - 0.5) * this.shakeY * dt;
- else
- this.vec.y = 0;
-
- var xRatio = 1 / (1 + (dt * this.decay));
- this.shakeX *= xRatio;
- this.shakeY *= xRatio;
- }
- }
|