| 1 | class FrogScene extends Scene |
| 2 | { |
| 3 | |
| 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 | constructor (x, y, width, height, displayMode = 0) |
| 17 | { |
| 18 | super(x, y, width, height, displayMode); |
| 19 | |
| 20 | this.fruitSpawnTimer = this.fruitSpawnRateMax; |
| 21 | } |
| 22 | |
| 23 | update (deltaTime) |
| 24 | { |
| 25 | super.update(deltaTime); |
| 26 | |
| 27 | this.spawnFruits(deltaTime); |
| 28 | } |
| 29 | |
| 30 | increaseDifficulty () |
| 31 | { |
| 32 | this.gameSpeed += .1; |
| 33 | |
| 34 | if (this.fruitSpawnRateMax > .1) |
| 35 | { |
| 36 | this.fruitSpawnRateMax -= .2; |
| 37 | } |
| 38 | else if (this.fruitSpawnRateMax == .2) |
| 39 | { |
| 40 | this.fruitSpawnRateMax = .1; |
| 41 | } |
| 42 | if (this.fruitSpawnRateMin > 0) |
| 43 | { |
| 44 | this.fruitSpawnRateMin -= .2; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | spawnFruits (deltaTime) |
| 50 | { |
| 51 | this.fruitSpawnTimer -= deltaTime; |
| 52 | if (this.fruitSpawnTimer < this.fruitSpawnRateMin) |
| 53 | { |
| 54 | if (this.shouldSpawn() || this.fruitSpawnTimer < 0) |
| 55 | { |
| 56 | let fruit = this.getFromFruitPoolOrCreate(); |
| 57 | fruit.collider.enabled = true; |
| 58 | fruit.speed = Math.randomRange(10, 20); |
| 59 | fruit.transform.position = this.getRandomVectorPosition(); |
| 60 | this.setRandomSubImage(fruit); |
| 61 | this.fruits.push(fruit); |
| 62 | |
| 63 | this.fruitSpawnTimer = this.fruitSpawnRateMax; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | consumeFruit (fruit) |
| 69 | { |
| 70 | this.eatenFruit.push(fruit.renderer.subImage); |
| 71 | |
| 72 | this.destroyFruit(fruit); |
| 73 | |
| 74 | this.increaseDifficulty(); |
| 75 | } |
| 76 | |
| 77 | destroyFruit (fruit) |
| 78 | { |
| 79 | if (this.fruits.contains(fruit)) |
| 80 | { |
| 81 | this.fruits.remove(fruit); |
| 82 | this.fruitPool.push(fruit); |
| 83 | fruit.transform.position.x = 50000; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | getFromFruitPoolOrCreate () |
| 88 | { |
| 89 | return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0)); |
| 90 | } |
| 91 | |
| 92 | setRandomSubImage (fruit) |
| 93 | { |
| 94 | fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages)); |
| 95 | } |
| 96 | |
| 97 | getRandomVectorPosition () |
| 98 | { |
| 99 | return new vector |
| 100 | ( |
| 101 | Math.floor(Math.randomRange(0, this.real_size.x)), |
| 102 | 0 |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | shouldSpawn () |
| 107 | { |
| 108 | return Math.random() < this.fruitSpawnChance; |
| 109 | } |
| 110 | } |