| 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 | } |
| 104 | |
| 105 | /** |
| 106 | * Gem Spawner spawns gems in the world |
| 107 | */ |
| 108 | var gemSpawner = |
| 109 | { |
| 110 | start: function() |
| 111 | { |
| 112 | gemSpawner.currentActive = 0; |
| 113 | |
| 114 | this.maxGems = 7; |
| 115 | |
| 116 | for (var i = 0; i < this.maxGems; i++) |
| 117 | { |
| 118 | this.spawnGem(i * 200, i == this.maxGems - 1); |
| 119 | } |
| 120 | |
| 121 | this.spawnInUpdate = false; |
| 122 | |
| 123 | this.allGems = []; |
| 124 | }, |
| 125 | |
| 126 | spawnGem: function(delay, enableSpawningAfter) |
| 127 | { |
| 128 | delay = delay || 0; |
| 129 | |
| 130 | if (gemSpawner.currentActive < this.maxGems) |
| 131 | { |
| 132 | gemSpawner.currentActive++; |
| 133 | |
| 134 | timingUtil.delay(delay, function() |
| 135 | { |
| 136 | var x = 100 + Math.random() * (GameWindow.width - 200); |
| 137 | var y = 100 + Math.random() * (GameWindow.height - 200); |
| 138 | |
| 139 | this.allGems.push(GameObject.create(gem, x, y, s_gem, 0)); |
| 140 | |
| 141 | this.allGems = this.allGems.filter(function(item) |
| 142 | { |
| 143 | return !item.destroyed; |
| 144 | }); |
| 145 | |
| 146 | if (enableSpawningAfter) |
| 147 | { |
| 148 | this.spawnInUpdate = true; |
| 149 | } |
| 150 | }, this); |
| 151 | } |
| 152 | }, |
| 153 | |
| 154 | onDestroy: function() |
| 155 | { |
| 156 | for (var i = 0; i < this.allGems.length; i++) |
| 157 | { |
| 158 | this.allGems[i].destroy(); |
| 159 | } |
| 160 | }, |
| 161 | |
| 162 | update: function(deltaTime) |
| 163 | { |
| 164 | if (this.spawnInUpdate) |
| 165 | { |
| 166 | this.spawnGem(500 + (Math.random() * 500)); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /*** |
| 172 | * All images used in the game are initialized here |
| 173 | */ |
| 174 | |
| 175 | var s_head = new image("images/head.png", 4, 4); |
| 176 | var s_orb = new image("images/orb.png", 4, 4); |
| 177 | var s_gem = new image("images/gems.png", 72, 6); |
| 178 | |
| 179 | /** |
| 180 | * Main script |
| 181 | */ |
| 182 | |
| 183 | window.onload = init; |
| 184 | |
| 185 | function DoInheritance() |
| 186 | { |
| 187 | //resultclass = GameObject.inherit(baseclass, childclass); |
| 188 | } |
| 189 | |
| 190 | function init() |
| 191 | { |
| 192 | DoInheritance(); |
| 193 | //Game.initialize(xpos, ypos, width, height, fps, callback) |
| 194 | // -2 to account for borders |
| 195 | var width = window.innerWidth - 2; |
| 196 | var height = window.innerHeight - 2; |
| 197 | |
| 198 | Game.initialize(0, 0, width, height, gameStart); |
| 199 | } |
| 200 | |
| 201 | function gameStart() |
| 202 | { |
| 203 | GameWindow.backgroundColor = {r:100, g:149, b:237}; |
| 204 | |
| 205 | Game.setUpdateFunction(update); // Let the game engine know we want to link our 'update()' to the game's update step. |
| 206 | |
| 207 | GameAudio.init(sounds, soundsFolder); |
| 208 | |
| 209 | p1 = GameObject.create(gemSpawner, GameWindow.width / 2, GameWindow.height / 2, null, 500); |
| 210 | } |
| 211 | |
| 212 | function update(deltaTime) |
| 213 | { |
| 214 | |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * All sounds in the game |
| 219 | */ |
| 220 | |
| 221 | var soundsFolder = "sounds"; |
| 222 | var sounds = |
| 223 | [ |
| 224 | { |
| 225 | name: "bubble", |
| 226 | volume: 1 |
| 227 | }, |
| 228 | { |
| 229 | name: "glass", |
| 230 | volume: 0.9 |
| 231 | }, |
| 232 | { |
| 233 | name: "tap", |
| 234 | volume: 1 |
| 235 | }, |
| 236 | { |
| 237 | name: "bite", |
| 238 | volume: 1 |
| 239 | }, |
| 240 | { |
| 241 | name: "pop", |
| 242 | volume: 0.1 |
| 243 | } |
| 244 | |
| 245 | ]; //List of all sound files as strings (do not include file extension) |
| 246 | |