Path: blob/main/public/games/files/garbage-collector/js/effects/shake.js
1036 views
/*globals define*/1define([2'utils'3], function( Utils ) {4'use strict';56function Shake() {7this.magnitude = 0;8this.duration = 0;910this.time = 0;1112// Variables pertaining to the current shake cycle.13// Change shake angle at frequency (20 fps).14this.shakePeriod = 1 / 20;15this.shakeTime = 0;16this.shakeMagnitude = 0;17this.shakeAngle = 0;18}1920Shake.prototype.applyTransform = function( ctx ) {21if ( !this.magnitude ) {22return;23}2425ctx.translate(26Math.cos( this.shakeAngle ) * this.shakeMagnitude,27Math.sin( this.shakeAngle ) * this.shakeMagnitude28);29};3031Shake.prototype.update = function( dt ) {32if ( !this.magnitude ) {33return;34}3536this.time += dt;37if ( this.time > this.duration ) {38this.magnitude = 0;39this.time = 0;40return;41}4243this.shakeTime += dt;44// A new oscillation!45if ( this.shakeTime > this.shakePeriod ) {46this.shakeTime = 0;47this.shakeAngle = Math.random() * Utils.PI2;4849// Lerp scale magnitude down to zero.50var scale = ( this.duration - this.time ) / this.duration;51this.shakeMagnitude = this.magnitude * scale;52}53};5455Shake.prototype.shake = function( magnitude, duration ) {56this.magnitude = magnitude || 0;57this.duration = duration || 0;5859this.update(0);60};6162Object.defineProperty( Shake.prototype, 'frequency', {63get: function() {64return 1 / this.shakePeriod;65},6667set: function( frequency ) {68this.shakePeriod = 1 / frequency;69}70});7172return Shake;73});747576