11import { loadImage } from "../util/load.ts"
2- import type { Input } from "../util/dir.ts"
2+ import { Input } from "../util/dir.ts"
33import { type Dir , DOWN , LEFT , RIGHT , UP } from "../util/dir.ts"
44import { CELL_SIZE } from "../util/constants.ts"
55import { choice , randomInt } from "../util/random.ts"
@@ -172,6 +172,7 @@ export abstract class Character implements IChar {
172172 onMoveEnd (
173173 _fieldTester : IFieldTester ,
174174 _itemContainer : ItemContainer ,
175+ _moveType : "linear" | "bounce" ,
175176 ) : void { }
176177
177178 step (
@@ -212,7 +213,7 @@ export abstract class Character implements IChar {
212213 } else if ( this . #dir === RIGHT ) {
213214 this . #i += 1
214215 }
215- this . onMoveEnd ( fieldTester , itemContainer )
216+ this . onMoveEnd ( fieldTester , itemContainer , this . #moveType )
216217 }
217218 } else if ( this . #moveType === "bounce" ) {
218219 this . #movePhase += this . #speed
@@ -225,6 +226,7 @@ export abstract class Character implements IChar {
225226 this . #movePhase = 0
226227 this . #isMoving = false
227228 this . #d = 0
229+ this . onMoveEnd ( fieldTester , itemContainer , this . #moveType)
228230 }
229231 }
230232 } else {
@@ -378,6 +380,7 @@ export abstract class Character implements IChar {
378380}
379381
380382export class MainCharacter extends Character {
383+ #lastMoveTypes: ( "linear" | "bounce" ) [ ] = [ ]
381384 override getNextState (
382385 input : typeof Input ,
383386 _fieldTester : IFieldTester ,
@@ -398,13 +401,34 @@ export class MainCharacter extends Character {
398401 override onMoveEnd (
399402 _fieldTester : IFieldTester ,
400403 itemContainer : ItemContainer ,
404+ moveType : "linear" | "bounce" ,
401405 ) : void {
402406 const item = itemContainer . get ( this . i , this . j )
403407 if ( item ) {
404408 itemContainer . remove ( this . i , this . j )
405409 const count = signal . appleCount . get ( )
406410 signal . appleCount . update ( count + 1 )
407411 }
412+
413+ this . #lastMoveTypes. push ( moveType )
414+ if ( this . #lastMoveTypes. length > 4 ) {
415+ this . #lastMoveTypes. shift ( )
416+ }
417+ if ( this . #lastMoveTypes. length === 4 ) {
418+ if ( this . #lastMoveTypes. every ( ( t ) => t === "bounce" ) ) {
419+ document . removeEventListener ( "keyup" , toggleFullscreen )
420+ document . addEventListener ( "keyup" , toggleFullscreen , { once : true } )
421+ this . #lastMoveTypes = [ ]
422+ }
423+ }
424+ }
425+ }
426+
427+ function toggleFullscreen ( ) {
428+ if ( document . fullscreenElement ) {
429+ document . exitFullscreen ( )
430+ } else {
431+ document . documentElement . requestFullscreen ( )
408432 }
409433}
410434
0 commit comments