Points, life, difficulty and GAME OVER
This commit is contained in:
parent
87314b7b42
commit
23c6ce2a7e
4 changed files with 98 additions and 13 deletions
|
|
@ -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)
|
||||
|
|
|
|||
55
src/classes/DifficultyManager.ts
Normal file
55
src/classes/DifficultyManager.ts
Normal 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
|
||||
}
|
||||
}
|
||||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import Phaser from 'phaser'
|
||||
import Ship from '../classes/Ship'
|
||||
import Asteroid from '../classes/Asteroid'
|
||||
import DifficultyManager from '../classes/DifficultyManager'
|
||||
|
||||
export default class DefaultScene extends Phaser.Scene {
|
||||
readonly maxAsteroids = 70
|
||||
|
|
@ -8,6 +9,9 @@ export default class DefaultScene extends Phaser.Scene {
|
|||
player?: Ship
|
||||
rotFrames?: Phaser.Types.Animations.AnimationFrame[]
|
||||
asteroids?: Phaser.GameObjects.Group
|
||||
difficulty?: DifficultyManager
|
||||
points = 0
|
||||
progressLabel?: Phaser.GameObjects.Text
|
||||
|
||||
constructor() {
|
||||
super('default-scene')
|
||||
|
|
@ -23,27 +27,45 @@ export default class DefaultScene extends Phaser.Scene {
|
|||
}
|
||||
|
||||
create() {
|
||||
this.asteroids = this.add.group()
|
||||
|
||||
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.asteroids = this.add.group()
|
||||
this.player = new Ship(this)
|
||||
this.time.addEvent({
|
||||
delay: 1500, // ms
|
||||
callback: this.spawnAsteroids,
|
||||
callbackScope: this,
|
||||
repeat: -1
|
||||
});
|
||||
this.progressLabel = this.add.text(5, 5, "", {
|
||||
font: '32px Arial',
|
||||
color: 'cyan'
|
||||
})
|
||||
this.progressLabel.setDepth(1)
|
||||
|
||||
this.updateLabel()
|
||||
this.hookupCollisions()
|
||||
}
|
||||
|
||||
spawnAsteroids() {
|
||||
if (this.asteroids!.getLength() < this.maxAsteroids)
|
||||
gameOver() {
|
||||
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))
|
||||
}
|
||||
|
||||
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() {
|
||||
// player - asteroids
|
||||
this.physics.add.overlap(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue