diff --git a/src/abilities/Score.ts b/src/abilities/Score.ts index 0fbdb23..50f7713 100644 --- a/src/abilities/Score.ts +++ b/src/abilities/Score.ts @@ -1,7 +1,12 @@ +import { k } from '../kaboom'; import { Comp } from 'kaboom'; +const { play } = k; + export const ON_SCORE_CHANGE = 'scoreChange'; +const NEW_LIFE_SCORE_THRESHOLD = 20000; + export interface ScoreComp extends Comp { get score(): number; set score(score: number); @@ -17,6 +22,10 @@ export function canScore(): ScoreComp { return score; }, set score(newScore) { + if (score % NEW_LIFE_SCORE_THRESHOLD > newScore % NEW_LIFE_SCORE_THRESHOLD) { + play('powerup'); + this.lives+=1; + } score = newScore; this.trigger(ON_SCORE_CHANGE, score); }, diff --git a/src/objects/Peter.ts b/src/objects/Peter.ts index 0669e14..7bc2977 100644 --- a/src/objects/Peter.ts +++ b/src/objects/Peter.ts @@ -31,6 +31,7 @@ const { export const ON_DIE = 'die'; export const ON_WIN = 'win'; +export const ON_LIVES_CHANGE = 'livesChange'; export interface PeterControls { keyboard: { @@ -55,11 +56,12 @@ export interface PeterComp extends Comp { isFrozen: boolean; isAlive: boolean; level: number; - lives: number; freeze: Function; action: Function; die: Function; win: Function; + get lives(): number; + set lives(num: number); setAnim: (dir: Vec2) => void; } @@ -102,6 +104,7 @@ export function addPeter(options: Partial<PeterCompOpt> = {}): PeterObj { export function peter(options: Partial<PeterCompOpt> = {}): PeterComp { const opt = Object.assign({}, PeterCompOptDefaults, options); + let lives = 4; return { id: "peter", require: ["area", "sprite", "can-salt", "can-walk"], @@ -125,7 +128,6 @@ export function peter(options: Partial<PeterCompOpt> = {}): PeterComp { isInitialized: false, isAlive: false, level: 0, - lives: 4, add() { this.onCollide("enemy", enemy=>{ if (enemy.isStunned) return; @@ -134,6 +136,13 @@ export function peter(options: Partial<PeterCompOpt> = {}): PeterComp { this.on(ON_DIR_CHANGE, this.setAnim); this.setObjects(opt.walkableObjects); }, + get lives() { + return lives; + }, + set lives(num: number) { + lives = num; + this.trigger(ON_LIVES_CHANGE, num); + }, action() { this.throwSalt(); }, diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index 45d8724..93c7895 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -2,7 +2,7 @@ import { LevelOpt } from 'kaboom'; import { k, BURGERTIME_BLUE } from '../kaboom'; import { waitSpawnPowerup } from '../objects/Powerup'; import { addEnemy } from '../objects/Enemy'; -import { ON_DIE, ON_WIN, PeterObj } from '../objects/Peter'; +import { ON_DIE, ON_WIN, ON_LIVES_CHANGE, PeterObj } from '../objects/Peter'; import { WalkableObj } from '../abilities/Walk'; import { ON_SALT_CHANGE } from '../abilities/Salt'; import { ON_SCORE_CHANGE } from '../abilities/Score'; @@ -107,10 +107,6 @@ export default function(options: Partial<GameSceneOpt>) { sprite('head-h'), pos(216, 8), ]); - ui.add([ - text(player.lives.toString(), { size: UI_FONT_SIZE }), - pos(230, 8), - ]); ui.add([ text(`${opt.currentPlayer+1} UP`, { size: UI_FONT_SIZE }), pos(16, 8), @@ -119,6 +115,10 @@ export default function(options: Partial<GameSceneOpt>) { text('HI', { size: UI_FONT_SIZE }), pos(112, 8), ]); + const txtScore = ui.add([ + text(player.score.toString(), { size: UI_FONT_SIZE, align: 'right', width: 50 }), + pos(48, 8), + ]); const txtHiScore = ui.add([ text('20000', { size: UI_FONT_SIZE, align: 'right', width: 50 }), pos(124, 8), @@ -127,10 +127,11 @@ export default function(options: Partial<GameSceneOpt>) { text(player.salt.toString(), { size: UI_FONT_SIZE }), pos(198, 8), ]); - const txtScore = ui.add([ - text(player.score.toString(), { size: UI_FONT_SIZE, align: 'right', width: 50 }), - pos(48, 8), + const txtLives = ui.add([ + text(player.lives.toString(), { size: UI_FONT_SIZE }), + pos(230, 8), ]); + player.on(ON_LIVES_CHANGE, (qty: number)=>txtLives.text = qty<0 ? '0' : qty.toString()); player.on(ON_SALT_CHANGE, (qty: number)=>txtPepper.text = qty.toString()); player.on(ON_SCORE_CHANGE, (score: number)=>{ txtScore.text = score.toString();