Points, life, difficulty and GAME OVER

This commit is contained in:
KGrzeg 2021-10-17 14:36:42 +02:00
parent 87314b7b42
commit 23c6ce2a7e
4 changed files with 98 additions and 13 deletions

View file

@ -31,7 +31,7 @@ export default class Asteroid extends Phaser.Physics.Arcade.Sprite {
Phaser.Math.RND.realInRange(-100, 100)
)
this.setRandomShade()
this.anims.play(Asteroid.getRandomAnimationName())
this.anims.timeScale = 1 + (this.scaleMax - this.scale) * this.scaleRotationFactor
if (Phaser.Math.RND.integer() % 2 == 0)
@ -49,6 +49,7 @@ export default class Asteroid extends Phaser.Physics.Arcade.Sprite {
if (!(bullet instanceof Bullet)) return
if (bullet.active == false) return
me.scene.events.emit("asteroid:destroy")
me.destroy() // TODO: use objects pool
bullet.setActive(false)
bullet.setVisible(false)

View file

@ -0,0 +1,55 @@
import DefaultScene from '../scenes/DefaultScene'
export default class DifficultyManager {
readonly spawnInterval = 1500 //ms
readonly maxAsteroids = 300
readonly intervalDecrementPerLevel = 30
private scene: DefaultScene
private difficultyLevel = 1
private points = 0
private spawnAtOnce = 1
constructor(scene: DefaultScene) {
this.scene = scene
scene.events.on('asteroid:destroy', () => {
this.points += 1
this.scene.events.emit("getpoint", this.difficultyLevel)
if (this.points % 10 == 0)
this.levelUp()
})
scene.time.addEvent({
delay: this.spawnInterval,
callback: this.spawnAsteroids,
callbackScope: this,
repeat: -1
});
}
levelUp() {
this.difficultyLevel += 1
this.spawnAtOnce += 1
this.scene.events.emit("lvlup", this.difficultyLevel)
}
spawnAsteroids() {
for (let i = 0; i < this.spawnAtOnce; ++i)
this.scene.spawnAsteroid()
}
getMaxAsteroids() {
return Math.min(this.difficultyLevel * 5, this.maxAsteroids)
}
getLevel() {
return this.difficultyLevel
}
getPoints() {
return this.points
}
}

View file

@ -15,6 +15,7 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
bullets: Bullets
thruster: Thruster
life = 3
constructor(scene: Phaser.Scene) {
super(
@ -72,8 +73,14 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
if (!(asteroid instanceof Asteroid)) return
if (asteroid.age > Asteroid.unbornAge) {
console.log("%cU ded", "color:red")
this.life -= 1
this.scene.cameras.main.shake(100, 0.02)
asteroid.destroy() //TODO: use objects pool
this.scene.events.emit("ship:gothit")
}
if (!this.life){
this.scene.events.emit("ship:destroyed")
}
}
}