From a70fdb20c3db063d0e4bf497ff3e95b82abdbce7 Mon Sep 17 00:00:00 2001 From: Josh Curtiss Date: Thu, 30 May 2024 23:10:24 -0700 Subject: [PATCH] feat: Add powerup component --- public/sprites/powerups.png | Bin 0 -> 1777 bytes src/com/Powerup.ts | 37 ++++++++++++++++++++++++++++++++++++ src/main.ts | 1 + src/scenes/GameScene.ts | 17 +++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 public/sprites/powerups.png create mode 100644 src/com/Powerup.ts diff --git a/public/sprites/powerups.png b/public/sprites/powerups.png new file mode 100644 index 0000000000000000000000000000000000000000..5e9df4d51f5a8f8a3b8ec9dc45f4facf9cb5dec5 GIT binary patch literal 1777 zcmeAS@N?(olHy`uVBq!ia0vp^20$#p!3HD)*8SNCq!^2X+?^QKos)S9knVgAfoGF&cr{B0zRa zdnN-5RPPKR4FVoO3^Wx&2V_>H=Hw=psjg<8ZsCe7@1fZm|7VdC>WZ77>1S% z49p7{5$;*Q1UJ)Q0W+K}05bg8F46ZuinG8YvY3H^TNs2H8D`Cq0GhEfGbExU!q>+t zIX_n~F(p4KRj(qq0Hlk-rosxy%uOvxRH(?!$t$+1uvG%9umZ9{!um=IU?nBlwn`Dc z0SeCfMX3sAdIow1N_Jcd3JNwwDQQ+gE^bimK%T8qMoCG5mA-y?dAVM>v0i>ry1t>M zrKP@sk-m|UE>MMTab;dfVufyAu`tKo-FP z#GNIXX$YJ0ilN>DdQcxEqi?8ZppQ*kYDFdvZ6Fz(+JIpM_ZEs}6^R89P2j+QxX}jW z0xReI+=8Oi;$om*>`ZOIu0jz*_z9vuD7830rxfJ%(7enNJ0qwTG+}gI5eRuZbC@1v zL39m~*fbyuqHFLk%1i~u7RX_ChEPW#iy^581)&u{cd7mqmNYrU88e; zPJU5vL1J<$$R4m3Bq4OwU~3`~))<;0TZ1Hlq!Ad4R?bDKpiCE%pPyrgkq2$`(UqX- z_RUYpOsjNBEJ?KkWmjDTQ(a?&5LkAFXh%{5)or5>O3X-!8`m9sPuyG6@ceHne_iyK)1LJb8`k}1^6S>p;h!Qnzj?wVsiWJ< zPIRwPkq~{Fs~Wa^%iQnPFI6?PyHr1ZIJv+qta_r>B&9D;3bj~w+@9&h)U4mHdFK4* zi?eey`8c@$>dtdv){mEYWPXg&HC%mY?BG~vQsX9zC|44OuPDI`+|Bt)lL*L0N6@@IB#awuF zmBi80d4GMgs+l+bE_FH)I!SQh&()pZZhcw!=4v?i`je}}qpVJuFAzy^=RP`HiaR0Y z(~ct_1uNZe8u`r@RO(!psdDg*c>A;Fr78braZ@pCfB8@;C5^Ay6Z6 zjz0HuebLRl>{bxVkHxbC?!|lm*OO6g4v00e-0Qr@{pEb?n_6i$Sv`S0nQ5}SzgOBF zmSzz7Z)>}2@nzo=s^LPPjr-Ew(!=ll_5OY|Sbxj6^h@PCubxkewwIN+mHW+`B2%nA TKX0`bP0l+XkKP~TJG literal 0 HcmV?d00001 diff --git a/src/com/Powerup.ts b/src/com/Powerup.ts new file mode 100644 index 0000000..2060abc --- /dev/null +++ b/src/com/Powerup.ts @@ -0,0 +1,37 @@ +import { k } from '../kaboom'; +import { Comp } from 'kaboom'; + +const { + anchor, + area, + destroy, + lifespan, + rand, + sprite, +} = k; + +export interface PowerupComp extends Comp { + timeout: number, + type: 0 | 1 | 2; + points: ()=>number; +} + +export function powerup(): PowerupComp { + return { + id: "powerup", + require: ["pos"], + timeout: 20, + type: Math.floor(rand() * 3) as 0 | 1 | 2, // Random int between 0-2 + points: ()=>this.type*100 + Math.floor(rand()*99), + add() { + this.use(sprite('powerups', { frame: this.type })); + this.use(anchor('center')); + this.use(area({ scale: 0.6 })); + this.use(lifespan(this.timeout)); + this.onCollide('player', player=>{ + player.saltQty+=1; + destroy(this); + }); + }, + }; +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 24d5489..30d59bd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,6 +53,7 @@ loadSprite("floor-stair-green", "floor-stair-green.png", { sliceX: 2 }); loadSprite("head-h", "head-h.png"); loadSprite("head-p", "head-p.png"); loadSprite("plate", "plate.png", { sliceX: 2 }); +loadSprite("powerups", "powerups.png", { sliceX: 3 }); loadSprite("salt", "salt.png", { sliceX: 4 }); loadSprite("stair-blue", "stair-blue.png", { sliceX: 2 }); loadSprite("stair-green", "stair-green.png", { sliceX: 2 }); diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index 4a23af9..c499bf2 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -5,6 +5,7 @@ import { SpriteComp, } from 'kaboom'; import { k } from '../kaboom'; +import { powerup } from '../com/Powerup'; import { peter, PeterComp } from '../com/Peter'; import { canSalt, SaltComp } from '../com/Salt'; import { canWalk, WalkComp, WalkableObj } from '../com/Walk'; @@ -16,9 +17,11 @@ const { anchor, area, fixed, + rand, Rect, sprite, vec2, + wait, z, } = k; @@ -73,8 +76,13 @@ const levelConf: LevelOpt = { peter(), canSalt(), canWalk(), + z(10), "player", ], + '$': () => [ + powerup(), + z(5), + ], }, }; @@ -114,4 +122,13 @@ export default function(levelNumber = 0) { // Player setup const player: GameObj = level.spawn("p", 16, 21.625); player.setObjects({ floors, stairs, stairtops }); + + // Powerups + function waitSpawnPowerup() { + // Wait 20-60 seconds + wait(rand()*40+20, ()=>{ + level.spawn('$', 16, 13.5).onDestroy(waitSpawnPowerup) + }); + } + waitSpawnPowerup(); }