Skip to content

Commit

Permalink
feat: Add bonus life at 20000 points
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Jun 5, 2024
1 parent 08249bb commit 3c249b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/abilities/Score.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
},
Expand Down
13 changes: 11 additions & 2 deletions src/objects/Peter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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"],
Expand All @@ -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;
Expand All @@ -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();
},
Expand Down
17 changes: 9 additions & 8 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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();
Expand Down

0 comments on commit 3c249b2

Please sign in to comment.