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