Skip to content

Commit 11aa4de

Browse files
committed
feat: add RandomWalkNPC
1 parent fa5a094 commit 11aa4de

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

model/character.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { MainCharacter, RandomWalkNPC } from "./character.ts"
1+
import { MainCharacter, RandomlyTurnNPC } from "./character.ts"
22
import { assertEquals } from "@std/assert"
33

44
Deno.test("Character", async (t) => {
55
await t.step("physicalGridKey", () => {
66
const mc = new MainCharacter(100, 100, "main/", "main")
77
assertEquals(mc.physicalGridKey, "100.100")
88

9-
const npc = new RandomWalkNPC(200, 200, "npc/", "npc")
9+
const npc = new RandomlyTurnNPC(200, 200, "npc/", "npc")
1010
assertEquals(npc.physicalGridKey, "200.200")
1111
})
1212

model/character.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,19 @@ export type ItemContainer = {
8383
remove(i: number, j: number): void
8484
}
8585

86+
type NPCType = "random" | "random-walk" | "static"
87+
8688
export function spawnCharacter(
8789
id: string,
88-
type: "random" | "static",
90+
type: NPCType,
8991
i: number,
9092
j: number,
9193
assetPrefix: string,
9294
{ dir = "down", speed = 1 }: { dir?: Dir; speed?: 1 | 2 | 4 | 8 | 16 } = {},
9395
): IChar {
9496
if (type === "random") {
97+
return new RandomlyTurnNPC(i, j, assetPrefix, id, dir, speed)
98+
} else if (type === "random-walk") {
9599
return new RandomWalkNPC(i, j, assetPrefix, id, dir, speed)
96100
} else if (type === "static") {
97101
return new StaticNPC(i, j, assetPrefix, id, dir, speed)
@@ -453,7 +457,7 @@ export abstract class Character implements IChar {
453457
}
454458
}
455459

456-
export class RandomWalkNPC extends Character {
460+
export class RandomlyTurnNPC extends Character {
457461
#counter = 32
458462

459463
override getNextAction(
@@ -483,5 +487,22 @@ export class RandomWalkNPC extends Character {
483487
}
484488
}
485489

490+
export class RandomWalkNPC extends Character {
491+
#counter = 32
492+
493+
override getNextAction(
494+
fieldTester: IFieldTester,
495+
collisionChecker: CollisionChecker,
496+
): NextAction {
497+
const dirs = ([UP, DOWN, LEFT, RIGHT] as const).filter((d) => {
498+
return this.canEnter(d, fieldTester, collisionChecker)
499+
})
500+
if (dirs.length === 0) {
501+
return undefined
502+
}
503+
return choice(dirs)
504+
}
505+
}
506+
486507
export class StaticNPC extends Character {
487508
}

0 commit comments

Comments
 (0)