Simplifying the codes, removing all unnecessary stuff, I know there are two functions with the same name but I am still simplifying the function to make it a self contain useful function for now so naming doesn't matter for now, still work in progress:
function shake()
{
this.shakeMag = 0;
this.shakeStart = 0;
this.shakeEnd = 0;
this.shakeMode = 0;
this.shakeEnforcePosition = 0;
this.shakeOriginalX = 0;
this.shakeOriginalY = 0;
this.axis = 0;
}
function tick2()
{
if (!this.enabled)
{
return;
}
// Is in a shake?
var now = this.runtime.kahanTime.sum;
var offx = 0, offy = 0;
if (now >= this.shakeStart && now < this.shakeEnd)
{
var mag = this.shakeMag * Math.min(this.runtime.timescale, 1);
// Mode 0 - reducing magnitude - lerp to zero
if (this.shakeMode === 0)
{
mag *= 1 - (now - this.shakeStart) / (this.shakeEnd - this.shakeStart);
}
var a = Math.random() * Math.PI * 2;
var d = Math.random() * mag;
offx = Math.cos(a) * d;
offy = Math.sin(a) * d;
}
//update only when necessary and one more time to enforce object position
if (offx != 0 || offy != 0 || (this.shakeEnforcePosition === 1 && this.shakeStart > 0))
{
if (this.axis == 1)
{
this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;
}
else if (this.axis == 2)
{
this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;
}
else
{
this.inst.x = this.shakeEnforcePosition ? this.shakeOriginalX + offx : this.inst.x + offx;
this.inst.y = this.shakeEnforcePosition ? this.shakeOriginalY + offy : this.inst.y + offy;
}
this.inst.set_bbox_changed();
//turn off
if (this.shakeEnforcePosition === 1 && now > this.shakeEnd)
{
this.shakeStart = 0;
}
}
}
function Shake(mag, dur, mode, enforcePosition, axis)
{
this.shakeMag = mag;
this.shakeStart = this.runtime.kahanTime.sum;
this.shakeEnd = this.shakeStart + dur;
this.shakeMode = mode;
this.shakeEnforcePosition = enforcePosition;
this.shakeOriginalX = this.inst.x;
this.shakeOriginalY = this.inst.y;
this.axis = axis;
}