| 1 | class FrogScene extends Scene |
| 2 | { |
| 3 | //something |
| 4 | fruitSpawnRateMin = 1; |
| 5 | fruitSpawnRateMax = 2; |
| 6 | fruitSpawnTimer = 0; |
| 7 | |
| 8 | fruitSpawnChance = .2; |
| 9 | |
| 10 | fruits = []; |
| 11 | fruitPool = []; |
| 12 | eatenFruit = []; |
| 13 | |
| 14 | gameSpeed = 1; |
| 15 | |
| 16 | start = false; |
| 17 | paused = false; |
| 18 | |
| 19 | closeButton = null; |
| 20 | |
| 21 | closeBtnX = 0; |
| 22 | closeBtnY = -140; |
| 23 | |
| 24 | posScaleY = 0; |
| 25 | text_1_target_y; |
| 26 | text_2_target_y; |
| 27 | |
| 28 | image_1_target_y; |
| 29 | image_1_rotation = -1; |
| 30 | |
| 31 | spaceBarTween = null; |
| 32 | screenTween = null; |
| 33 | |
| 34 | destructing = false; |
| 35 | |
| 36 | externalFruitCurrentPos = null; |
| 37 | |
| 38 | windowDestroyTweens = []; |
| 39 | |
| 40 | constructor (x, y, width, height, displayMode = 0) |
| 41 | { |
| 42 | super(x, y, width, height, displayMode); |
| 43 | |
| 44 | this.fruitSpawnTimer = this.fruitSpawnRateMax; |
| 45 | |
| 46 | this.text_1_target_y = this.real_size.y * .3; |
| 47 | this.text_2_target_y = this.real_size.y * .62; |
| 48 | this.image_1_target_y = this.real_size.y * .45; |
| 49 | this.closeBtnX = this.real_size.x - 50; |
| 50 | } |
| 51 | |
| 52 | onLoad () |
| 53 | { |
| 54 | let pos = new vector(this.closeBtnX, this.closeBtnY); |
| 55 | this.closeButton = this.addObject(new SubBtn(pos.x, pos.y, this.game.images.closeButton, 1)); |
| 56 | |
| 57 | this.closeButton.isClicked.add(function () |
| 58 | { |
| 59 | this.paused = false; |
| 60 | this.posScaleY = 2.5; |
| 61 | this.gameOver(); |
| 62 | }, this); |
| 63 | |
| 64 | this.externalFruitCurrentPos = new vector(this.real_position.x - this.game.images.fruit.width / 2, this.real_position.y - this.game.images.fruit.height / 2); |
| 65 | |
| 66 | this.maxPerXAxis = Math.ceil(this.real_size.x / this.game.images.fruit.width) + 2; |
| 67 | this.maxPerYAxis = Math.ceil(this.real_size.y / this.game.images.fruit.height) + 2; |
| 68 | |
| 69 | this.doMenuScreenTween(); |
| 70 | |
| 71 | this.doSpaceBarTweenIn(); |
| 72 | } |
| 73 | |
| 74 | update (deltaTime) |
| 75 | { |
| 76 | if (!this.paused) |
| 77 | { |
| 78 | super.update(deltaTime); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | this.closeButton.update(deltaTime); |
| 83 | } |
| 84 | |
| 85 | if (this.start && !this.destructing) |
| 86 | { |
| 87 | this.checkEscape(); |
| 88 | if (!this.paused) |
| 89 | this.spawnFruits(deltaTime); |
| 90 | |
| 91 | } |
| 92 | else if (!this.start && !this.destructing) |
| 93 | { |
| 94 | this.checkSpacebar(); |
| 95 | } |
| 96 | |
| 97 | if (this.destructing) |
| 98 | { |
| 99 | if (this.activeObjects.length > 0) |
| 100 | { |
| 101 | return; |
| 102 | } |
| 103 | if (this.windowDestroyTweens.length > 0) |
| 104 | { |
| 105 | let frogButton = MainMenu.Instance.menu.children.btnGames.menu.children.btnFrog; |
| 106 | frogButton.moveToActiveState(800, () => frogButton.moveToInactiveState(800)); |
| 107 | |
| 108 | this.windowDestroyTweens.forEach(t => t.start()); |
| 109 | this.windowDestroyTweens[0].onComplete(() => this.destroy()); |
| 110 | |
| 111 | this.windowDestroyTweens = []; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | gameOver () |
| 117 | { |
| 118 | if (this.spaceBarTween) |
| 119 | TWEEN.remove(this.spaceBarTween); |
| 120 | if (this.screenTween) |
| 121 | TWEEN.remove(this.screenTween); |
| 122 | this.activeObjects.forEach(ao => ao.destroy()); |
| 123 | |
| 124 | this.passEatenFruitsToSimpleImagesRenderer(); |
| 125 | |
| 126 | this.destructing = true; // finish animations first |
| 127 | } |
| 128 | |
| 129 | passEatenFruitsToSimpleImagesRenderer () |
| 130 | { |
| 131 | |
| 132 | let targetButton = MainMenu.Instance.menu.children.btnFruit; |
| 133 | let targetVector = targetButton.targetPosition; |
| 134 | |
| 135 | this.eatenFruit.forEach(e => |
| 136 | { |
| 137 | e.targetPosition = targetVector; |
| 138 | e.targetSize = new vector(.5, .5); |
| 139 | e.time = Math.floor(Math.randomRange(500, 700)); |
| 140 | }); |
| 141 | |
| 142 | if (this.eatenFruit.length > 0) |
| 143 | { |
| 144 | SimpleImagesRenderTween.Instance.add(this.eatenFruit, () => targetButton.moveToActiveState(800), () => targetButton.moveToInactiveState(800)); |
| 145 | } |
| 146 | |
| 147 | this.eatenFruit = []; |
| 148 | } |
| 149 | |
| 150 | increaseDifficulty () |
| 151 | { |
| 152 | this.gameSpeed += .1; |
| 153 | |
| 154 | if (this.fruitSpawnRateMax > .1) |
| 155 | { |
| 156 | this.fruitSpawnRateMax -= .2; |
| 157 | } |
| 158 | else if (this.fruitSpawnRateMax == .2) |
| 159 | { |
| 160 | this.fruitSpawnRateMax = .1; |
| 161 | } |
| 162 | if (this.fruitSpawnRateMin > 0) |
| 163 | { |
| 164 | this.fruitSpawnRateMin -= .2; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | startGame () |
| 169 | { |
| 170 | if (this.spaceBarTween) |
| 171 | { |
| 172 | this.spaceBarTween.stop(); |
| 173 | } |
| 174 | |
| 175 | this.doAwayMenuScreenTween(); |
| 176 | |
| 177 | let frog = new PlayerFrog(60, this.real_size.y - 50, this.game.images.frog_basic, 0); |
| 178 | |
| 179 | let tileWidth = this.game.images.tile_floor_brown.width - 2; |
| 180 | let tileWidthHalf = tileWidth / 2; |
| 181 | |
| 182 | for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth) |
| 183 | { |
| 184 | this.addObject(new FrogFloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0)); |
| 185 | } |
| 186 | this.addObject(frog); |
| 187 | |
| 188 | this.start = true; |
| 189 | } |
| 190 | |
| 191 | pauseGame () |
| 192 | { |
| 193 | this.paused = true; |
| 194 | |
| 195 | this.doMenuScreenTween(); |
| 196 | } |
| 197 | |
| 198 | unPauseGame () |
| 199 | { |
| 200 | this.doAwayMenuScreenTween(); |
| 201 | } |
| 202 | |
| 203 | spawnFruits (deltaTime) |
| 204 | { |
| 205 | this.fruitSpawnTimer -= deltaTime; |
| 206 | if (this.fruitSpawnTimer < this.fruitSpawnRateMin) |
| 207 | { |
| 208 | if (this.shouldSpawn() || this.fruitSpawnTimer < 0) |
| 209 | { |
| 210 | let fruit = this.getFromFruitPoolOrCreate(); |
| 211 | fruit.collider.enabled = true; |
| 212 | fruit.speed = Math.randomRange(10, 20); |
| 213 | fruit.transform.position = this.getRandomVectorPosition(); |
| 214 | this.setRandomSubImage(fruit); |
| 215 | this.fruits.push(fruit); |
| 216 | |
| 217 | this.fruitSpawnTimer = this.fruitSpawnRateMax; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | consumeFruit (fruit) |
| 223 | { |
| 224 | let currFruit = { position: this.getNextExternalFruitPosition(), size: new vector(.5, .5), image: game.images.fruit, subImage: fruit.renderer.subImage }; |
| 225 | this.eatenFruit.push(currFruit); |
| 226 | |
| 227 | new TWEEN.Tween(currFruit.size) |
| 228 | .to({ x: 1, y: 1 }, 400) |
| 229 | .easing(TWEEN.Easing.Elastic.Out) |
| 230 | .start(); |
| 231 | |
| 232 | this.destroyFruit(fruit); |
| 233 | |
| 234 | this.increaseDifficulty(); |
| 235 | |
| 236 | FruitInventory.Instance.addToInventory(currFruit.subImage); |
| 237 | } |
| 238 | |
| 239 | getNextExternalFruitPosition () |
| 240 | { |
| 241 | if (this.eatenFruit.length == 0) |
| 242 | { |
| 243 | return this.externalFruitCurrentPos; |
| 244 | } |
| 245 | |
| 246 | if (this.eatenFruit.length < this.maxPerXAxis) |
| 247 | { |
| 248 | return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(this.game.images.fruit.width, 0)); |
| 249 | } |
| 250 | else if (this.eatenFruit.length - this.maxPerXAxis < this.maxPerYAxis) |
| 251 | { |
| 252 | return this.externalFruitCurrentPos = this.externalFruitCurrentPos.add(new vector(0, this.game.images.fruit.height)); |
| 253 | } |
| 254 | else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerXAxis * 2) |
| 255 | { |
| 256 | return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(this.game.images.fruit.width, 0)); |
| 257 | } |
| 258 | else if (this.eatenFruit.length - this.maxPerYAxis < this.maxPerYAxis * 2) |
| 259 | { |
| 260 | return this.externalFruitCurrentPos = this.externalFruitCurrentPos.subtract(new vector(0, this.game.images.fruit.height)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | destroyFruit (fruit) |
| 265 | { |
| 266 | if (this.fruits.contains(fruit)) |
| 267 | { |
| 268 | this.fruits.remove(fruit); |
| 269 | this.fruitPool.push(fruit); |
| 270 | fruit.transform.position.x = 50000; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | getFromFruitPoolOrCreate () |
| 275 | { |
| 276 | return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new FrogFruit(0, 0, this.game.images.fruit, 0)); |
| 277 | } |
| 278 | |
| 279 | setRandomSubImage (fruit) |
| 280 | { |
| 281 | fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages)); |
| 282 | } |
| 283 | |
| 284 | draw () |
| 285 | { |
| 286 | super.draw(); |
| 287 | |
| 288 | this.drawExternalFruit(); |
| 289 | this.drawMenu(); |
| 290 | } |
| 291 | |
| 292 | getRandomVectorPosition () |
| 293 | { |
| 294 | return new vector |
| 295 | ( |
| 296 | Math.floor(Math.randomRange(0, this.real_size.x)), |
| 297 | 0 |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | shouldSpawn () |
| 302 | { |
| 303 | return Math.random() < this.fruitSpawnChance; |
| 304 | } |
| 305 | |
| 306 | checkSpacebar () |
| 307 | { |
| 308 | if (GameInput.isHeld(GameInput.keys[" "])) |
| 309 | { |
| 310 | this.startGame(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | checkEscape () |
| 315 | { |
| 316 | if (GameInput.isPressed(GameInput.keys.Escape)) |
| 317 | { |
| 318 | |
| 319 | this.paused ? this.unPauseGame() : this.pauseGame(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | drawExternalFruit () |
| 324 | { |
| 325 | this.eatenFruit.forEach(e => |
| 326 | this.game.images.fruit.draw(this.parentScene, |
| 327 | e.position.x, e.position.y, |
| 328 | this.game.images.fruit.width * e.size.x, this.game.images.fruit.height * e.size.y, |
| 329 | 0, 0, 0, 1, e.subImage)); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | drawMenu () |
| 334 | { |
| 335 | if (!this.paused) |
| 336 | { |
| 337 | if (this.posScaleY < 2) |
| 338 | { |
| 339 | // WebGL: Scene.context is no longer canvas2d — use GlText helper. |
| 340 | GlText.drawCentered(this, "Press", this.real_size.x * .5, this.text_1_target_y * this.posScaleY, { font: "20px Monospace", fill: "black", maxWidth: 72 }); |
| 341 | GlText.drawCentered(this, "To Lick", this.real_size.x * .5 + 5, this.text_2_target_y * this.posScaleY, { font: "20px Monospace", fill: "black", maxWidth: 72 }); |
| 342 | |
| 343 | let space = this.game.images.spacebar; |
| 344 | space.draw(this, this.real_size.x * .5, this.image_1_target_y * this.posScaleY, space.width, space.height, this.image_1_rotation * 3, 0, 0, 1, 0); |
| 345 | } |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | GlText.drawCentered(this, "Paused", this.real_size.x * .5, this.text_1_target_y * this.posScaleY * 2, { font: "20px Monospace", fill: "black", maxWidth: 72 }); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | doMenuScreenTween () |
| 354 | { |
| 355 | this.screenTween = new TWEEN.Tween(this) |
| 356 | .to({ posScaleY: 1 }, 800) |
| 357 | .easing(TWEEN.Easing.Elastic.Out) |
| 358 | .start(); |
| 359 | this.doCloseButtonTweenIn(); |
| 360 | } |
| 361 | |
| 362 | doCloseButtonTweenIn () |
| 363 | { |
| 364 | new TWEEN.Tween(this.closeButton.transform.position) |
| 365 | .to({ y: 40 }, 800) |
| 366 | .easing(TWEEN.Easing.Elastic.Out) |
| 367 | .start(); |
| 368 | } |
| 369 | |
| 370 | doCloseButtonTweenOut () |
| 371 | { |
| 372 | new TWEEN.Tween(this.closeButton.transform.position) |
| 373 | .to({ y: -40 }, 800) |
| 374 | .easing(TWEEN.Easing.Elastic.Out) |
| 375 | .start(); |
| 376 | } |
| 377 | |
| 378 | doSpaceBarTweenIn () |
| 379 | { |
| 380 | this.spaceBarTween = new TWEEN.Tween(this) |
| 381 | .to({ image_1_rotation: 1 }, 500) |
| 382 | .easing(TWEEN.Easing.Sinusoidal.InOut) |
| 383 | .yoyo(true) |
| 384 | .repeat(Infinity) |
| 385 | .start(); |
| 386 | } |
| 387 | |
| 388 | doAwayMenuScreenTween () |
| 389 | { |
| 390 | this.doCloseButtonTweenOut(); |
| 391 | |
| 392 | this.screenTween = new TWEEN.Tween(this) |
| 393 | .to({ posScaleY: 2.5 }, 800) |
| 394 | .easing(TWEEN.Easing.Elastic.InOut) |
| 395 | .start().onComplete(() => this.paused = false); |
| 396 | |
| 397 | } |
| 398 | } |