Skip to content

Commit 94b8d57

Browse files
committed
refactor: clean up move intent handling
1 parent a24d25b commit 94b8d57

7 files changed

Lines changed: 160 additions & 145 deletions

File tree

game/field.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
IStepper,
1111
LoadOptions,
1212
} from "../model/types.ts"
13-
import { spawnActor } from "../model/actor.ts"
13+
import { Actor } from "../model/actor.ts"
1414
import { Item } from "../model/item.ts"
1515
import { Prop } from "../model/prop.ts"
1616
import {
@@ -550,16 +550,7 @@ export class Field implements IField {
550550
let i = 0
551551
for (const spawn of newCharSpawns) {
552552
i++
553-
this.#actors.add(spawnActor(
554-
spawn.id,
555-
spawn.i,
556-
spawn.j,
557-
spawn.def,
558-
{
559-
dir: spawn.dir,
560-
speed: spawn.speed,
561-
},
562-
))
553+
this.#actors.add(Actor.fromSpawn(spawn))
563554
}
564555
if (i > 0) {
565556
console.log(`Spawning ${i} actors`)

game/main-character.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ import { DOWN, LEFT, RIGHT, UP } from "../util/dir.ts"
22
import { Input, inputQueue } from "./ui/input.ts"
33
import {
44
Actor,
5-
ActorMove,
65
type IdleDelegate,
76
type MoveEndDelegate,
87
} from "../model/actor.ts"
9-
import type { IField, MovePlan } from "../model/types.ts"
8+
import type { IField, Move } from "../model/types.ts"
109

1110
export class IdleMainActor implements IdleDelegate {
12-
onIdle(_actor: Actor, _field: IField): MovePlan | undefined {
11+
onIdle(actor: Actor, field: IField): void {
1312
if (Input.up) {
14-
return { type: "go", dir: UP }
13+
actor.tryMove("go", UP, field)
14+
return
1515
} else if (Input.down) {
16-
return { type: "go", dir: DOWN }
16+
actor.tryMove("go", DOWN, field)
17+
return
1718
} else if (Input.left) {
18-
return { type: "go", dir: LEFT }
19+
actor.tryMove("go", LEFT, field)
20+
return
1921
} else if (Input.right) {
20-
return { type: "go", dir: RIGHT }
22+
actor.tryMove("go", RIGHT, field)
23+
return
2124
}
2225

2326
const queueHead = inputQueue[0]
@@ -27,13 +30,13 @@ export class IdleMainActor implements IdleDelegate {
2730
queueHead === "touchendempty"
2831
) {
2932
inputQueue.shift()
30-
return { type: "jump" }
33+
actor.jump()
3134
}
3235
}
3336
}
3437

3538
export class MoveEndMainActor implements MoveEndDelegate {
36-
onMoveEnd(actor: Actor, field: IField, _move: ActorMove): void {
39+
onMoveEnd(actor: Actor, field: IField, _move: Move): void {
3740
field.peekItem(actor.i, actor.j)?.onCollect(actor, field)
3841
}
3942
}

model/action-queue.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Dir, IEntity, IField, MovePlan } from "./types.ts"
1+
import type { Dir, IEntity, IField, MoveAction } from "./types.ts"
22
import { splashColor } from "../game/field.ts"
33

44
type CommonAction = {
@@ -20,7 +20,7 @@ export type PropAction =
2020

2121
export type ActorAction =
2222
| CommonAction
23-
| MovePlan
23+
| MoveAction
2424
| { readonly type: "go-random" }
2525
| { readonly type: "speed"; readonly change: "2x" | "4x" | "reset" }
2626
| {
@@ -38,19 +38,18 @@ export type ActorAction =
3838
export class ActionQueue<
3939
T extends IEntity,
4040
A extends Record<string, unknown>,
41-
R = undefined,
4241
> {
4342
#queue: A[] = []
4443
#handler: (
4544
field: IField,
4645
action: Exclude<A, CommonAction>,
47-
) => R | undefined
46+
) => "next" | "end"
4847

4948
constructor(
5049
handler: (
5150
field: IField,
5251
action: Exclude<A, CommonAction>,
53-
) => R | undefined,
52+
) => "next" | "end",
5453
) {
5554
this.#handler = handler
5655
}
@@ -71,7 +70,7 @@ export class ActionQueue<
7170
this.#queue = []
7271
}
7372

74-
process(entity: T, field: IField): R | "idle" | "wait" {
73+
process(entity: T, field: IField): "idle" | undefined {
7574
while (true) {
7675
const action = this.#queue[0] as unknown as CommonAction
7776
if (!action) {
@@ -80,7 +79,7 @@ export class ActionQueue<
8079

8180
if (action.type === "wait") {
8281
if (field.time < action.until) {
83-
return "wait"
82+
return
8483
}
8584
this.#queue.shift()
8685
continue
@@ -106,8 +105,8 @@ export class ActionQueue<
106105
}
107106
default: {
108107
const result = this.#handler(field, action)
109-
if (result) {
110-
return result
108+
if (result === "end") {
109+
return
111110
}
112111
}
113112
}

model/actor.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import { MoveGo, RandomlyTurnNPC, StaticNPC } from "./actor.ts"
1+
import { Actor, MoveGo } from "./actor.ts"
22
import { ActorDefinition } from "./catalog.ts"
33
import { assert, assertEquals, assertFalse } from "@std/assert"
44

55
const actorDef: ActorDefinition = {
6-
src: "main/",
76
type: "main",
8-
main: "main",
9-
href: "main/",
10-
// other properties if needed
7+
src: "../main/",
8+
href: "./main/",
119
}
1210

1311
Deno.test("Actor", async (t) => {
1412
await t.step("physicalGridKey", () => {
15-
const mc = new StaticNPC(100, 100, actorDef, "main")
13+
const mc = new Actor(100, 100, actorDef, "main")
1614
assertEquals(mc.physicalGridKey, "100.100")
1715

18-
const npc = new RandomlyTurnNPC(200, 200, actorDef, "npc")
16+
const npc = new Actor(200, 200, actorDef, "npc")
1917
assertEquals(npc.physicalGridKey, "200.200")
2018
})
2119

2220
await t.step("frontGrid", () => {
23-
const c = new StaticNPC(100, 100, actorDef, "main")
21+
const c = new Actor(100, 100, actorDef, "main")
2422
assertEquals(c.frontGrid(), [100, 101])
2523

2624
c.setDir("right")

0 commit comments

Comments
 (0)