@@ -9,6 +9,7 @@ import type {
99 MovableObject ,
1010} from "./public-types.ts" ;
1111import {
12+ fallableTexture ,
1213 rockTexture ,
1314 switchBaseTexture ,
1415 switchPressedTexture ,
@@ -30,6 +31,11 @@ export type GridCell =
3031 block : Block . movable ;
3132 objectId : string ;
3233 }
34+ | {
35+ block : Block . fallable ;
36+ objectId : string ;
37+ dy : number ; // in pixels
38+ }
3339 | {
3440 block : Block . air ;
3541 objectId ?: unknown ;
@@ -99,6 +105,12 @@ export class Grid {
99105 spriteRow . push ( { sprite, block } ) ;
100106 break ;
101107 }
108+ case Block . fallable : {
109+ const sprite = createSprite ( cellSize , block , x , y , this . marginY ) ;
110+ stage . addChild ( sprite ) ;
111+ spriteRow . push ( { sprite, block } ) ;
112+ break ;
113+ }
102114 case Block . block : {
103115 const sprite = createSprite ( cellSize , block , x , y , this . marginY ) ;
104116 stage . addChild ( sprite ) ;
@@ -289,7 +301,11 @@ export class Grid {
289301 const cells = get ( cx . state ) . cells ;
290302 const cell = cells [ y ] ?. [ x ] ;
291303 if ( ! cell ) return undefined ;
292- if ( cell . block !== Block . movable && cell . block !== Block . switchWithObject )
304+ if (
305+ cell . block !== Block . movable &&
306+ cell . block !== Block . switchWithObject &&
307+ cell . block !== Block . fallable
308+ )
293309 return undefined ;
294310 const objectId = cell . objectId ;
295311 const retrievedBlocks : { x : number ; y : number } [ ] = [ ] ;
@@ -308,7 +324,7 @@ export class Grid {
308324 . reduce ( ( a , b ) => Math . max ( a , b ) , 0 ) ;
309325
310326 const retrievedObject : MovableObject = {
311- block : Block . movable ,
327+ block : cell . block === Block . fallable ? Block . fallable : Block . movable ,
312328 objectId,
313329 relativePositions : retrievedBlocks . map ( ( block ) => ( {
314330 x : block . x - minX ,
@@ -357,6 +373,7 @@ export class Grid {
357373 if (
358374 cell . block !== Block . movable &&
359375 cell . block !== Block . switchWithObject &&
376+ cell . block !== Block . fallable &&
360377 cell . objectId
361378 ) {
362379 console . warn ( "Cell is not movable but has an objectId" ) ;
@@ -419,7 +436,8 @@ export class Grid {
419436 else if ( prev . block === Block . switch ) {
420437 if (
421438 cell . block !== Block . movable &&
422- cell . block !== Block . switchWithObject
439+ cell . block !== Block . switchWithObject &&
440+ cell . block !== Block . fallable
423441 ) {
424442 console . warn (
425443 "No block other than movable cannot be placed on the switch" ,
@@ -441,6 +459,9 @@ export class Grid {
441459 prevCell . block === Block . switchWithObject ,
442460 "block is not switch" ,
443461 ) ;
462+ if ( cell . block === Block . fallable ) {
463+ console . warn ( "TODO: switchWithFallable is not implemented" ) ;
464+ }
444465 cells [ y ] [ x ] = {
445466 block : Block . switchWithObject ,
446467 switchId : prevCell . switchId ,
@@ -609,6 +630,28 @@ export class Grid {
609630 prev . block = cell . block ;
610631 break ;
611632 }
633+ case Block . fallable : {
634+ const movableSprite = createSprite (
635+ blockSize ,
636+ cell . block ,
637+ x ,
638+ y ,
639+ marginY ,
640+ ) ;
641+ stage . addChild ( movableSprite ) ;
642+ assert (
643+ cell . objectId !== undefined ,
644+ "movable block must have objectId" ,
645+ ) ;
646+ cells [ y ] [ x ] = {
647+ block : cell . block ,
648+ objectId : cell . objectId ,
649+ dy : 0 ,
650+ } ;
651+ prev . sprite = movableSprite ;
652+ prev . block = cell . block ;
653+ break ;
654+ }
612655 default :
613656 // cell satisfies never;
614657 }
@@ -644,6 +687,19 @@ export function createCellsFromStageDefinition(
644687
645688 break ;
646689 }
690+ case Block . fallable : {
691+ const group = stageDefinition . blockGroups . find (
692+ ( b ) => b . x === x && b . y === y ,
693+ ) ;
694+ const objectId = group ? group . objectId : Math . random ( ) . toString ( ) ;
695+ const cell : GridCell = {
696+ block,
697+ objectId,
698+ dy : 0 ,
699+ } ;
700+ row . push ( cell ) ;
701+ break ;
702+ }
647703 case Block . block :
648704 case Block . air : {
649705 const cell : GridCell = {
@@ -706,6 +762,8 @@ export function blockFromDefinition(n: string) {
706762 return Block . block ;
707763 case "m" :
708764 return Block . movable ;
765+ case "f" :
766+ return Block . fallable ;
709767 case "s" :
710768 return Block . switch ;
711769 case "S" :
@@ -737,6 +795,12 @@ function createSprite(
737795 updateSprite ( movableSprite , blockSize , x , y , marginY ) ;
738796 return movableSprite ;
739797 }
798+ case Block . fallable : {
799+ const movableSprite = new Sprite ( fallableTexture ) ;
800+ // movableSprite.tint = 0xff0000;
801+ updateSprite ( movableSprite , blockSize , x , y , marginY ) ;
802+ return movableSprite ;
803+ }
740804 case Block . switch : {
741805 const switchSprite = new Sprite ( switchTexture ) ;
742806 if ( switchColor ) switchSprite . tint = switchColor ;
@@ -807,6 +871,8 @@ export function printCells(cells: GridCell[][], context?: string) {
807871 return "b" ;
808872 case Block . movable :
809873 return "m" ;
874+ case Block . fallable :
875+ return "m" ;
810876 case Block . switch :
811877 return "s" ;
812878 case Block . switchBase :
0 commit comments