Skip to content

Commit

Permalink
feat: Add Chase component to Enemy and add multiple enemies (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Jun 7, 2024
1 parent 8fd9f05 commit 3376040
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/objects/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
Vec2,
ZComp,
} from 'kaboom';
import { canAlive, AliveComp } from '../abilities/Alive';
import { canChase, ChaseComp } from '../abilities/Chase';
import { canFreeze, FreezeComp } from '../abilities/Freeze';
import { canWalk, WalkComp } from '../abilities/Walk';

const {
add,
Expand Down Expand Up @@ -41,7 +45,7 @@ const EnemyCompOptDefaults: EnemyCompOpt = {
type: 'hotdog',
};

export function addEnemy(options: Partial<EnemyCompOpt> = {}): GameObj<SpriteComp & AnchorComp & AreaComp & PosComp & ZComp & EnemyComp> {
export function addEnemy(options: Partial<EnemyCompOpt> = {}): GameObj<SpriteComp & AnchorComp & AreaComp & PosComp & ZComp & EnemyComp & AliveComp & ChaseComp & FreezeComp & WalkComp> {
const opt = Object.assign({}, EnemyCompOptDefaults, options);
return add([
sprite('enemies', { anim: `${opt.type}-walk` }),
Expand All @@ -50,6 +54,10 @@ export function addEnemy(options: Partial<EnemyCompOpt> = {}): GameObj<SpriteCom
area({ shape: new Rect(vec2(0), 7, 15), offset: vec2(0), collisionIgnore: ['powerup'] }),
z(20),
opt.type,
canAlive(),
canChase(),
canFreeze(),
canWalk(),
enemy(opt),
]);
}
Expand All @@ -68,13 +76,16 @@ export function enemy(options: Partial<EnemyCompOpt> = {}): EnemyComp {
stun() {
stunTimer?.cancel();
stunned = true;
this.freeze();
this.play(`${this.type}-stun`);
stunTimer = wait(this.stunDelay, () => {
stunned = false;
this.unfreeze();
this.play(`${this.type}-walk`);
});
},
add() {
this.speed = 28;
this.onCollide('salt', this.stun);
},
};
Expand Down
14 changes: 13 additions & 1 deletion src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,19 @@ export default function(options: Partial<GameSceneOpt>) {
waitSpawnPowerup();

// Enemy Setup
addEnemy({ type: 'hotdog', pos: vec2(224, 165) });
const enemies = [
addEnemy({ type: 'hotdog', pos: vec2(224, 165) }),
addEnemy({ type: 'egg', pos: vec2(32, 165) }),
addEnemy({ type: 'hotdog', pos: vec2(32, 21) }),
addEnemy({ type: 'hotdog', pos: vec2(224, 21) }),
addEnemy({ type: 'hotdog', pos: vec2(64, 21) }),
];
enemies.forEach(enemy=>{
enemy.freeze();
enemy.setObjects({ floors, stairs, stairtops });
enemy.target = player;
});
wait(8, ()=>enemies.forEach(enemy=>enemy.unfreeze()));

// Next Scene management (when player dies or wins)
function goNextScene(action: 'win' | 'die') {
Expand Down

0 comments on commit 3376040

Please sign in to comment.