| 1 | /** |
| 2 | * Snake Food |
| 3 | */ |
| 4 | |
| 5 | var gem = |
| 6 | { |
| 7 | start: function() |
| 8 | { |
| 9 | this.name = "gem"; |
| 10 | this.renderer.subImage = Math.floor(Math.random() * this.renderer.image.subImages); |
| 11 | |
| 12 | this.transform.scale.x = 0; |
| 13 | this.transform.scale.y = 0; |
| 14 | |
| 15 | var scaleUpTween = new TWEEN.Tween(this.transform.scale); |
| 16 | |
| 17 | scaleUpTween.to({x: [1.5, 1], y: [1.5, 1]}, 800); |
| 18 | |
| 19 | scaleUpTween.easing(TWEEN.Easing.Back.Out); |
| 20 | |
| 21 | scaleUpTween.start(); |
| 22 | |
| 23 | this.shakeLength = 1000; |
| 24 | this.shakes = 4; |
| 25 | this.shakeAngle = 45; |
| 26 | |
| 27 | this.minShakeTimer = 5; |
| 28 | this.maxShakeTimer = 20; |
| 29 | this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer)); |
| 30 | this.currentShakeTimer = 0; |
| 31 | |
| 32 | GameAudio.play("bubble"); |
| 33 | }, |
| 34 | |
| 35 | update: function(deltaTime) |
| 36 | { |
| 37 | this.currentShakeTimer += deltaTime; |
| 38 | |
| 39 | if(this.currentShakeTimer >= this.shakeTimer) |
| 40 | { |
| 41 | this.shake(); |
| 42 | |
| 43 | this.shakeTimer = this.minShakeTimer + (Math.random() * (this.maxShakeTimer - this.minShakeTimer)); |
| 44 | |
| 45 | this.currentShakeTimer = 0; |
| 46 | } |
| 47 | }, |
| 48 | |
| 49 | onDestroy: function() |
| 50 | { |
| 51 | gemSpawner.currentActive--; |
| 52 | }, |
| 53 | |
| 54 | onCollision: function(other) |
| 55 | { |
| 56 | if (other.name == "snake" && !this.destroyed) |
| 57 | { |
| 58 | other.grow(1, -1); |
| 59 | this.destroy(); |
| 60 | } |
| 61 | }, |
| 62 | |
| 63 | shake: function(callback, context) |
| 64 | { |
| 65 | var singleShakeTime = this.shakes / this.shakeLength; |
| 66 | |
| 67 | var shakeTween = new TWEEN.Tween(this.transform); |
| 68 | |
| 69 | var angles = []; |
| 70 | |
| 71 | var offset = Math.round(Math.random() * 5); |
| 72 | |
| 73 | for (var i = 0; i < this.shakes; i++) |
| 74 | { |
| 75 | var intensiy = 1 - (i / this.shakes); |
| 76 | |
| 77 | // Direction of the shake (is 'i' odd?) |
| 78 | var dir = !((i + offset) % 2)? -1 : 1; |
| 79 | |
| 80 | var angleRad = this.shakeAngle; |
| 81 | |
| 82 | var angle = angleRad * dir * intensiy; |
| 83 | |
| 84 | angles.push(angle); |
| 85 | } |
| 86 | |
| 87 | angles.push(0); |
| 88 | |
| 89 | shakeTween.to({rotation: angles}, this.shakeLength); |
| 90 | |
| 91 | shakeTween.easing(TWEEN.Easing.Quadratic.InOut); |
| 92 | |
| 93 | if (callback) |
| 94 | { |
| 95 | shakeTween.onComplete.addOnce(callback, context); |
| 96 | } |
| 97 | |
| 98 | shakeTween.start(); |
| 99 | } |
| 100 | } |