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; } onLoad() { while(this.fruitPool.length < this.fruitMax) { let fruit = new Fruit(0,0, this.game.images.fruit, 0); this.fruitPool.push(fruit); } } 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.destroyed = false; fruit.transform.position = this.getRandomVectorPosition(fruit); this.setRandomSubImage(fruit); this.addObject(fruit); this.fruits.push(fruit); this.fruitSpawnTimer = this.fruitSpawnRateMax; } } } destroyFruit(fruit) { this.fruits.remove(fruit); this.fruitPool.push(fruit); fruit.destroy(); } getFromFruitPoolOrCreate() { return this.fruitPool.length > 0 ? this.fruitPool.pop() : 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; } }