Skip to content

Commit 1de4943

Browse files
committed
fix
1 parent 1169dc3 commit 1de4943

6 files changed

Lines changed: 87 additions & 15 deletions

File tree

game/field.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ export class FieldItems implements IStepper, ILoader {
8282
return this.#coordMap[`${i}.${j}`]
8383
}
8484

85+
debug() {
86+
console.log(
87+
Object.entries(this.#coordMap).filter(([_, item]: any) =>
88+
item.type === "fish"
89+
),
90+
)
91+
}
92+
8593
/** Collects an item from the field. */
8694
collect(i: number, j: number) {
8795
const item = this.#deactivate(i, j)
@@ -107,16 +115,24 @@ export class FieldItems implements IStepper, ILoader {
107115
}
108116

109117
step(field: IField) {
118+
const changed = new Set<[string, string, IItem]>()
110119
for (const item of this.#items) {
111120
const key = `${item.i}.${item.j}`
112121
item.step(field)
113122
const keyAfter = `${item.i}.${item.j}`
114123
if (key !== keyAfter) {
124+
changed.add([key, keyAfter, item])
115125
// The item has moved
116126
delete this.#coordMap[key]
117127
this.#coordMap[keyAfter] = item
118128
}
119129
}
130+
for (const [key] of changed) {
131+
delete this.#coordMap[key]
132+
}
133+
for (const [_, keyAfter, item] of changed) {
134+
this.#coordMap[keyAfter] = item
135+
}
120136
}
121137

122138
async loadAssets(options: LoadOptions): Promise<void> {

game/main-character.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export class IdleMainActor implements IdleDelegate {
6161

6262
export class MoveEndMainActor implements MoveEndDelegate {
6363
onMoveEnd(actor: Actor, field: IField, _move: Move): void {
64+
//console.log("peekItem", actor.i, actor.j, field.peekItem(actor.i, actor.j))
65+
;(field as any).items.debug()
6466
field.peekItem(actor.i, actor.j)?.onCollect(actor, field)
6567
}
6668
}

model/actor.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,8 @@ export class Actor implements IActor {
249249
if (type === "go") this.setDir(dir)
250250
if (this.canGo(dir, field)) {
251251
this.#move = new MoveGo(this.#speed, dir)
252-
if (
253-
this.#follower && this.#lastMoveDir &&
254-
(this.#i !== this.#follower.i || this.#j !== this.#follower.j)
255-
) {
256-
this.#follower.enqueueActions({
257-
type: "go",
258-
dir: this.#lastMoveDir,
259-
speed: this.#speed,
260-
})
252+
if (this.#follower && this.#lastMoveDir) {
253+
this.#follower.follow(this.#i, this.#j, this.#lastMoveDir, this.#speed)
261254
}
262255

263256
// actor position moves to the next grid immediately (the animation catches it up in 16 frames)
@@ -288,6 +281,7 @@ export class Actor implements IActor {
288281
this.#move = new MoveJump()
289282
this.#move.cb = cb
290283
this.#idleCounter = 0
284+
this.unsetFollower()
291285
}
292286

293287
setDir(state: Dir) {
@@ -526,7 +520,18 @@ export class Actor implements IActor {
526520
}
527521

528522
setFollower(follower: IFollower) {
529-
this.#follower = follower
523+
if (this.#follower) {
524+
this.#follower.setFollower(follower)
525+
} else {
526+
this.#follower = follower
527+
}
528+
}
529+
530+
unsetFollower() {
531+
if (this.#follower) {
532+
this.#follower.unfollow()
533+
this.#follower = null
534+
}
530535
}
531536
}
532537

model/item.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export class Item implements IItem {
3434
#image: ImageBitmap | undefined
3535
#move: Move | null = null
3636
#lastMoveDir: Dir | null = null
37-
#follower: Item | null = null
37+
#follower: IFollower | null = null
38+
#followState: "following" | "waiting" | null = null
3839
readonly #actionQueue = new ActionQueue<Item, ItemAction>(
3940
(_field, action) => {
4041
switch (action.type) {
@@ -184,7 +185,41 @@ export class Item implements IItem {
184185
}
185186

186187
setFollower(follower: IFollower): void {
187-
this.#follower = follower as Item
188+
if (this.#follower) {
189+
this.#follower.setFollower(follower)
190+
} else {
191+
this.#follower = follower
192+
}
193+
}
194+
195+
follow(i: number, j: number, dir: Dir, speed: 1 | 2 | 4 | 8 | 16) {
196+
if (this.#followState === "following") {
197+
this.enqueueActions({ type: "go", dir, speed })
198+
if (this.#follower && this.#lastMoveDir) {
199+
this.#follower.follow(this.i, this.j, this.#lastMoveDir, speed)
200+
}
201+
} else if (this.#followState === "waiting") {
202+
if (this.i === i && this.j === j) {
203+
this.#followState = "following"
204+
}
205+
}
206+
}
207+
208+
unfollow() {
209+
console.log("unfollow", this.id, this.i, this.j)
210+
this.#followState = null
211+
if (this.#follower) {
212+
this.#follower.unfollow()
213+
this.#follower = null
214+
}
215+
}
216+
217+
get isFollowing() {
218+
return this.#followState !== null
219+
}
220+
221+
startFollowing() {
222+
this.#followState = "waiting"
188223
}
189224
}
190225

@@ -312,7 +347,11 @@ export class CollectPurpleMushroom implements CollectDelegate {
312347

313348
export class CollectFish implements CollectDelegate {
314349
onCollect(actor: IActor, field: IField, item: Item): void {
350+
if (item.isFollowing) {
351+
return
352+
}
315353
actor.setFollower(item)
354+
item.startFollowing()
316355

317356
for (
318357
const effect of linePattern0(DIRS, actor.i, actor.j, 1, 0.7, 3, "#006e8a")
@@ -321,5 +360,3 @@ export class CollectFish implements CollectDelegate {
321360
}
322361
}
323362
}
324-
325-
// implement following item delegate

model/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ export type IFinishable = {
8080
readonly finished: boolean
8181
}
8282

83-
export type IFollower = {
83+
export type IFollower = IFollowable & {
8484
i: number
8585
j: number
8686
enqueueActions(
8787
...actions: { type: "go"; dir: Dir; speed?: 1 | 2 | 4 | 8 | 16 }[]
8888
): void
89+
follow(i: number, j: number, dir: Dir, speed: 1 | 2 | 4 | 8 | 16): void
90+
unfollow(): void
8991
}
9092

9193
export type IFollowable = {

static/map/block_-200.-200.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,6 +4046,16 @@
40464046
"i": -54,
40474047
"j": -46,
40484048
"type": "fish"
4049+
},
4050+
{
4051+
"i": -79,
4052+
"j": -65,
4053+
"type": "fish"
4054+
},
4055+
{
4056+
"i": -76,
4057+
"j": -65,
4058+
"type": "fish"
40494059
}
40504060
],
40514061
"props": [

0 commit comments

Comments
 (0)