class FrogScene extends Scene { fruitMax = 10; fruitSpawnRateMin = 1; fruitSpawnRateMax = 2; fruitSpawnTimer = 0; fruitSpawnChance = .2; fruits = []; fruitPool = []; gameSpeed = 1; constructor (x, y, width, height, displayMode = 0) { super(x, y, width, height, displayMode); this.fruitSpawnTimer = this.fruitSpawnRateMax; } update (deltaTime) { super.update(deltaTime); this.spawnFruits(deltaTime); } spawnFruits (deltaTime) { this.fruitSpawnTimer -= deltaTime; if (this.fruitSpawnTimer < this.fruitSpawnRateMin) { if (this.shouldSpawn() || this.fruitSpawnTimer < 0) { let fruit = this.getFromFruitPoolOrCreate(); fruit.transform.position = this.getRandomVectorPosition(fruit); this.setRandomSubImage(fruit); this.fruits.push(fruit); this.fruitSpawnTimer = this.fruitSpawnRateMax; } } } destroyFruit (fruit) { if (this.fruits.contains(fruit)) { this.fruits.remove(fruit); this.fruitPool.push(fruit); fruit.transform.position.x = 50000; } } getFromFruitPoolOrCreate () { return this.fruitPool.length > 0 ? this.fruitPool.pop() : this.addObject(new Fruit(0, 0, this.game.images.fruit, 0)); } setRandomSubImage (fruit) { fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages)); } getRandomVectorPosition (fruit) { return new vector ( Math.floor(Math.randomRange(0, this.real_size.x)), 0 ); } shouldSpawn () { return this.fruits.length < this.fruitMax && Math.random() < this.fruitSpawnChance; } }