class FrogScene extends Scene { fruitSpawnRateMin = 1; fruitSpawnRateMax = 2; fruitSpawnTimer = 0; fruitSpawnChance = .2; fruits = []; fruitPool = []; eatenFruit = []; gameSpeed = 1; start = false; posScaleY = 0; text_1_target_y; text_2_target_y; image_1_target_y; image_1_rotation = -1; spaceBarTween = null; screenTween = null; destructing = false; constructor (x, y, width, height, displayMode = 0) { super(x, y, width, height, displayMode); this.fruitSpawnTimer = this.fruitSpawnRateMax; this.text_1_target_y = this.real_size.y * .3; this.text_2_target_y = this.real_size.y * .62; this.image_1_target_y = this.real_size.y * .45; this.doMenuScreenTween(); this.doSpaceBarTweenIn(); } update (deltaTime) { super.update(deltaTime); if (this.start && !this.destructing) { this.spawnFruits(deltaTime); } else if (!this.start && !this.destructing) { this.checkSpacebar(); } if (this.destructing) { if (this.activeObjects.length > 0) { return; } this.destroy(); } } gameOver () { this.start = false; this.activeObjects.forEach(ao => ao.destroy()); // pass over fruits array to parent scene this.destructing = true; // finish animations first } increaseDifficulty () { this.gameSpeed += .1; if (this.fruitSpawnRateMax > .1) { this.fruitSpawnRateMax -= .2; } else if (this.fruitSpawnRateMax == .2) { this.fruitSpawnRateMax = .1; } if (this.fruitSpawnRateMin > 0) { this.fruitSpawnRateMin -= .2; } } startGame () { if (this.spaceBarTween) { this.spaceBarTween.stop(); } this.doAwayMenuScreenTween(); let frog = new PlayerFrog(60, this.real_size.y - 50, this.game.images.frog_basic, 0); let tileWidth = this.game.images.tile_floor_brown.width - 2; let tileWidthHalf = tileWidth / 2; for (let tx = tileWidthHalf - 4; tx < this.real_size.x; tx += tileWidth) { this.addObject(new FrogFloorTile(tx, this.real_size.y - 30, this.game.images.tile_floor_brown, 0)); } this.addObject(frog); this.start = true; } spawnFruits (deltaTime) { this.fruitSpawnTimer -= deltaTime; if (this.fruitSpawnTimer < this.fruitSpawnRateMin) { if (this.shouldSpawn() || this.fruitSpawnTimer < 0) { let fruit = this.getFromFruitPoolOrCreate(); fruit.collider.enabled = true; fruit.speed = Math.randomRange(10, 20); fruit.transform.position = this.getRandomVectorPosition(); this.setRandomSubImage(fruit); this.fruits.push(fruit); this.fruitSpawnTimer = this.fruitSpawnRateMax; } } } consumeFruit (fruit) { let currFruit = { scaleX: .5, scaleY: .5, image: fruit.renderer.subImage }; this.eatenFruit.push(currFruit); new TWEEN.Tween(currFruit) .to({ scaleX: 1, scaleY: 1 }, 400) .easing(TWEEN.Easing.Elastic.Out) .start(); this.destroyFruit(fruit); this.increaseDifficulty(); } 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 FrogFruit(0, 0, this.game.images.fruit, 0)); } setRandomSubImage (fruit) { fruit.renderer.subImage = Math.floor(Math.randomRange(0, fruit.renderer.sprite.subImages)); } draw () { super.draw(); this.drawExternalFruit(); this.drawMenu(); } getRandomVectorPosition () { return new vector ( Math.floor(Math.randomRange(0, this.real_size.x)), 0 ); } shouldSpawn () { return Math.random() < this.fruitSpawnChance; } checkSpacebar () { if (GameInput.isHeld(GameInput.keys[" "])) { this.startGame(); } } drawExternalFruit () { if (this.eatenFruit.length > 0) { let offX = this.game.images.fruit.width; let offY = this.game.images.fruit.height; let startingX = this.real_position.x - offX / 2; let startingY = this.real_position.y - offY / 2; let maxPerXAxis = Math.ceil(this.real_size.x / offX) + 1; let xAxisCount = 0; let maxPerYAxis = Math.ceil(this.real_size.y / offY) + 1; let yAxisCount = 0; let currentPos = new vector(startingX, startingY); for (let i = 0; i < this.eatenFruit.length; i++) { //draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage) this.game.images.fruit.draw(this.parentScene, currentPos.x, currentPos.y, offX * this.eatenFruit[i].scaleX, offY * this.eatenFruit[i].scaleY, 0, 0, 0, 1, this.eatenFruit[i].image); if (xAxisCount < maxPerXAxis) { xAxisCount++; currentPos = currentPos.add(new vector(offX, 0)); } else if (yAxisCount < maxPerYAxis) { yAxisCount++; currentPos = currentPos.add(new vector(0, offY)); } else if (xAxisCount < maxPerXAxis * 2) { xAxisCount++; currentPos = currentPos.subtract(new vector(offX, 0)); } else if (yAxisCount < maxPerYAxis * 2) { yAxisCount++; currentPos = currentPos.subtract(new vector(0, offY)); } } } } drawMenu () { if (this.posScaleY < 2) { this.context.font = "20px Monospace"; this.context.fillStyle = "black"; // sets the color to fill in the rectangle with let textData = this.context.measureText("Press"); this.context.fillText("Press", this.real_size.x * .5 - (textData.width / 2), this.text_1_target_y * this.posScaleY, 72); textData = this.context.measureText("To Start"); this.context.fillText("To Lick", this.real_size.x * .5 - (textData.width / 2) + 5, this.text_2_target_y * this.posScaleY, 72); let space = this.game.images.spacebar; 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); } } doMenuScreenTween () { this.screenTween = new TWEEN.Tween(this) .to({ posScaleY: 1 }, 800) .easing(TWEEN.Easing.Elastic.Out) .start(); } doSpaceBarTweenIn () { this.spaceBarTween = new TWEEN.Tween(this) .to({ image_1_rotation: 1 }, 500) .easing(TWEEN.Easing.Sinusoidal.InOut) .yoyo(true) .repeat(Infinity) .start(); } doAwayMenuScreenTween () { this.screenTween .to({ posScaleY: 2.5 }, 800) .easing(TWEEN.Easing.Elastic.InOut) .start(); } }