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) Phaser.Math.RND.realInRange(-100, 100)
) )
this.setRandomShade() this.setRandomShade()
this.anims.play(Asteroid.getRandomAnimationName()) this.anims.play(Asteroid.getRandomAnimationName())
this.anims.timeScale = 1 + (this.scaleMax - this.scale) * this.scaleRotationFactor this.anims.timeScale = 1 + (this.scaleMax - this.scale) * this.scaleRotationFactor
if (Phaser.Math.RND.integer() % 2 == 0) 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 instanceof Bullet)) return
if (bullet.active == false) return if (bullet.active == false) return
me.scene.events.emit("asteroid:destroy")
me.destroy() // TODO: use objects pool me.destroy() // TODO: use objects pool
bullet.setActive(false) bullet.setActive(false)
bullet.setVisible(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 bullets: Bullets
thruster: Thruster thruster: Thruster
life = 3
constructor(scene: Phaser.Scene) { constructor(scene: Phaser.Scene) {
super( super(
@ -72,8 +73,14 @@ export default class Ship extends Phaser.Physics.Arcade.Sprite {
if (!(asteroid instanceof Asteroid)) return if (!(asteroid instanceof Asteroid)) return
if (asteroid.age > Asteroid.unbornAge) { if (asteroid.age > Asteroid.unbornAge) {
console.log("%cU ded", "color:red") this.life -= 1
this.scene.cameras.main.shake(100, 0.02) 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")
} }
} }
} }

View file

@ -1,6 +1,7 @@
import Phaser from 'phaser' import Phaser from 'phaser'
import Ship from '../classes/Ship' import Ship from '../classes/Ship'
import Asteroid from '../classes/Asteroid' import Asteroid from '../classes/Asteroid'
import DifficultyManager from '../classes/DifficultyManager'
export default class DefaultScene extends Phaser.Scene { export default class DefaultScene extends Phaser.Scene {
readonly maxAsteroids = 70 readonly maxAsteroids = 70
@ -8,6 +9,9 @@ export default class DefaultScene extends Phaser.Scene {
player?: Ship player?: Ship
rotFrames?: Phaser.Types.Animations.AnimationFrame[] rotFrames?: Phaser.Types.Animations.AnimationFrame[]
asteroids?: Phaser.GameObjects.Group asteroids?: Phaser.GameObjects.Group
difficulty?: DifficultyManager
points = 0
progressLabel?: Phaser.GameObjects.Text
constructor() { constructor() {
super('default-scene') super('default-scene')
@ -23,27 +27,45 @@ export default class DefaultScene extends Phaser.Scene {
} }
create() { create() {
this.asteroids = this.add.group()
Asteroid.createAnimations(this) Asteroid.createAnimations(this)
this.difficulty = new DifficultyManager(this)
this.events.on('getpoint', this.updateLabel, this)
this.events.on('lvlup', this.updateLabel, this)
this.events.on('ship:gothit', this.updateLabel, this)
this.events.on('ship:destroyed', this.gameOver, this)
this.add.image(400, 300, 'sky') this.add.image(400, 300, 'sky')
this.asteroids = this.add.group()
this.player = new Ship(this) this.player = new Ship(this)
this.time.addEvent({ this.progressLabel = this.add.text(5, 5, "", {
delay: 1500, // ms font: '32px Arial',
callback: this.spawnAsteroids, color: 'cyan'
callbackScope: this, })
repeat: -1 this.progressLabel.setDepth(1)
});
this.updateLabel()
this.hookupCollisions() this.hookupCollisions()
} }
spawnAsteroids() { gameOver() {
if (this.asteroids!.getLength() < this.maxAsteroids) console.log("%cU ded", "color:red")
console.log("points: ", this.difficulty!.getPoints())
}
spawnAsteroid() {
if (this.asteroids!.getLength() < this.difficulty!.getMaxAsteroids())
this.asteroids!.add(new Asteroid(this)) this.asteroids!.add(new Asteroid(this))
} }
updateLabel() {
const lvl = this.difficulty!.getLevel()
const pts = this.difficulty!.getPoints()
const lives = this.player!.life
const str = `Level: ${lvl}\tPoints: ${pts}\tHP: ${lives}`
this.progressLabel!.text = str
}
hookupCollisions() { hookupCollisions() {
// player - asteroids // player - asteroids
this.physics.add.overlap( this.physics.add.overlap(