Skip to content

Commit

Permalink
wip: AI
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Jun 5, 2024
1 parent 9b85f04 commit 8d580ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
41 changes: 40 additions & 1 deletion src/objects/Enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ 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,
vec2,
Expand All @@ -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 & FreezeComp & WalkComp> {
const opt = Object.assign({}, EnemyCompOptDefaults, options);
return add([
sprite('enemies', { anim: `${opt.type}-walk` }),
Expand All @@ -50,6 +54,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 +65,7 @@ export function enemy(options: Partial<EnemyCompOpt> = {}): EnemyComp {
const opt = Object.assign({}, EnemyCompOptDefaults, options);
let stunned = false;
let stunTimer;
let lastThinkPos = vec2(0);
return {
id: 'enemy',
type: opt.type,
Expand All @@ -68,14 +76,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;
if (lastThinkPos.dist(this.pos)<10) return;
const { floor, stair, stairtop } = this.calcWalkableStatus();
if (stairtop || (floor && stair)) {
console.log("Contemplating...", this.pos.x, this.pos.y);
lastThinkPos = this.pos;
let newdir = vec2(0);
if (randi(100)<75) {
console.log("Let's turn like usual!");
if (this.dir.x && stairtop) {
newdir = vec2(0, 1);
} else 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 8d580ab

Please sign in to comment.