@@ -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
313348export 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
0 commit comments