Skip to content

Commit

Permalink
wip: AI
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Jun 6, 2024
1 parent 9b85f04 commit 46026ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/objects/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ import {
Vec2,
ZComp,
} from 'kaboom';
import { canAlive, AliveComp } from '../abilities/Alive';
import { canFreeze, FreezeComp } from '../abilities/Freeze';
import { canWalk, WalkComp } from '../abilities/Walk';

const {
add,
anchor,
area,
pos,
randi,
Rect,
sprite,
time,
vec2,
wait,
z,
Expand All @@ -41,7 +46,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 & FreezeComp & WalkComp> {
const opt = Object.assign({}, EnemyCompOptDefaults, options);
return add([
sprite('enemies', { anim: `${opt.type}-walk` }),
Expand All @@ -50,6 +55,9 @@ 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(),
canFreeze(),
canWalk(),
enemy(opt),
]);
}
Expand All @@ -58,6 +66,8 @@ export function enemy(options: Partial<EnemyCompOpt> = {}): EnemyComp {
const opt = Object.assign({}, EnemyCompOptDefaults, options);
let stunned = false;
let stunTimer;
let lastThinkPos = vec2(0);
let lastThinkTime = time();
return {
id: 'enemy',
type: opt.type,
Expand All @@ -68,14 +78,45 @@ 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);
},
update() {
if (this.isFrozen || !this.isAlive) return;
const longTimeNoThink = time()-lastThinkTime>2.5;
if (lastThinkPos.dist(this.pos)<10 && !longTimeNoThink) return;
const { floor, stair, stairtop } = this.calcWalkableStatus();
if (longTimeNoThink || (floor && (stair || stairtop))) {
console.log("Contemplating...", time(), this.pos.x, this.pos.y);
lastThinkPos = this.pos;
lastThinkTime = time();
let newdir = vec2(0);
if (randi(100)<75) {
console.log("Let's turn like usual!");
if (this.dir.x) {
newdir = this.restrictDir(vec2(0, -1))
if (newdir.isZero()) newdir = vec2(0, 1);
} else {
newdir = this.restrictDir(vec2(-1, 0))
if (newdir.isZero()) newdir = vec2(1, 0);
}
if (newdir.isZero()) {
console.log("I got stuck somehow.");
}
this.setIntendedDir(newdir);
} else {
console.log("I'll carry on.");
}
}
},
};
}
7 changes: 5 additions & 2 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export default function(options: Partial<GameSceneOpt>) {
waitSpawnPowerup();

// Enemy Setup
addEnemy({ type: 'hotdog', pos: vec2(224, 165) });
const enemy = addEnemy({ type: 'hotdog', pos: vec2(224, 165) });
enemy.setObjects({ floors, stairs, stairtops });
enemy.freeze();
wait(8, ()=>enemy.unfreeze());

// Next Scene management (when player dies or wins)
function goNextScene(action: 'win' | 'die') {
Expand Down Expand Up @@ -240,7 +243,7 @@ export default function(options: Partial<GameSceneOpt>) {
dlg.destroy();
wait(player.isInitialized ? 0.25 : 3, ()=>{
player.isFrozen = false;
music.play();
// music.play();
});
if (!player.isInitialized) {
play('start');
Expand Down

0 comments on commit 46026ad

Please sign in to comment.