@@ -106,16 +106,24 @@ export class Grid {
106106 break ;
107107 }
108108 case Block . switch : {
109- const sprite = createSprite ( cellSize , block , x , y , this . marginY ) ;
109+ const switchId = (
110+ get ( cx . state ) . cells [ y ] [ x ] as {
111+ block : Block . switch ;
112+ switchId : string ;
113+ }
114+ ) . switchId ;
115+ const sprite = createSprite (
116+ cellSize ,
117+ block ,
118+ x ,
119+ y ,
120+ this . marginY ,
121+ switchColor ( switchId ) ,
122+ ) ;
110123 stage . addChild ( sprite ) ;
111124 spriteRow . push ( { sprite, block } ) ;
112125 get ( cx . state ) . switches . push ( {
113- id : (
114- get ( cx . state ) . cells [ y ] [ x ] as {
115- block : Block . switch ;
116- switchId : string ;
117- }
118- ) . switchId ,
126+ id : switchId ,
119127 x,
120128 y,
121129 pressedByPlayer : false ,
@@ -130,7 +138,20 @@ export class Grid {
130138 break ;
131139 }
132140 case Block . switchingBlockOFF : {
133- const sprite = createSprite ( cellSize , block , x , y , this . marginY ) ;
141+ const switchId = (
142+ get ( cx . state ) . cells [ y ] [ x ] as {
143+ block : Block . switch ;
144+ switchId : string ;
145+ }
146+ ) . switchId ;
147+ const sprite = createSprite (
148+ cellSize ,
149+ block ,
150+ x ,
151+ y ,
152+ this . marginY ,
153+ switchColor ( switchId ) ,
154+ ) ;
134155 stage . addChild ( sprite ) ;
135156 spriteRow . push ( { sprite, block } ) ;
136157 get ( cx . state ) . switchingBlocks . push ( {
@@ -345,38 +366,50 @@ export class Grid {
345366
346367 if ( prev . block === Block . switch && cell . block === Block . switchPressed ) {
347368 // switchが押されたとき
369+ assert (
370+ prevCell . block === Block . switch ||
371+ prevCell . block === Block . switchPressed ,
372+ "block is not switch or switchPressed" ,
373+ ) ;
374+ const switchId = prevCell . switchId ;
375+ if ( ! switchId ) throw new Error ( "switchId is undefined" ) ;
348376 const blockSprite = createSprite (
349377 blockSize ,
350378 Block . switchPressed ,
351379 x ,
352380 y ,
353381 marginY ,
382+ switchColor ( switchId ) ,
354383 ) ;
355384 stage . addChild ( blockSprite ) ;
356- assert (
357- prevCell . block === Block . switch ||
358- prevCell . block === Block . switchPressed ,
359- "block is not switch or switchPressed" ,
360- ) ;
361385 cells [ y ] [ x ] = {
362386 block : Block . switchPressed ,
363- switchId : prevCell . switchId ,
387+ switchId,
364388 objectId : undefined ,
365389 } ;
366390 prev . sprite = blockSprite ;
367391 prev . block = Block . switchPressed ;
368392 } else if ( prev . block === Block . switchPressed ) {
369393 // switchが押されているとき
370- const blockSprite = createSprite ( blockSize , Block . switch , x , y , marginY ) ;
371- stage . addChild ( blockSprite ) ;
372394 assert (
373395 prevCell . block === Block . switchPressed ||
374396 prevCell . block === Block . switch ,
375397 "block is not switch or switchPressed" ,
376398 ) ;
399+ const switchId = prevCell . switchId ;
400+ if ( ! switchId ) throw new Error ( "switchId is undefined" ) ;
401+ const blockSprite = createSprite (
402+ blockSize ,
403+ Block . switch ,
404+ x ,
405+ y ,
406+ marginY ,
407+ switchColor ( switchId ) ,
408+ ) ;
409+ stage . addChild ( blockSprite ) ;
377410 cells [ y ] [ x ] = {
378411 block : Block . switch ,
379- switchId : prevCell . switchId ,
412+ switchId,
380413 objectId : undefined ,
381414 } ;
382415 prev . sprite = blockSprite ;
@@ -435,16 +468,25 @@ export class Grid {
435468 console . log ( "prev.block" , prev . block ) ;
436469 return ;
437470 }
438- const blockSprite = createSprite ( blockSize , Block . switch , x , y , marginY ) ;
439- stage . addChild ( blockSprite ) ;
440471 assert (
441472 prevCell . block === Block . switchWithObject ||
442473 prevCell . block === Block . switch ,
443474 "block is not switchWithObject" ,
444475 ) ;
476+ const switchId = prevCell . switchId ;
477+ if ( ! switchId ) throw new Error ( "switchId is undefined" ) ;
478+ const blockSprite = createSprite (
479+ blockSize ,
480+ Block . switch ,
481+ x ,
482+ y ,
483+ marginY ,
484+ switchColor ( switchId ) ,
485+ ) ;
486+
445487 cells [ y ] [ x ] = {
446488 block : Block . switch ,
447- switchId : prevCell . switchId ,
489+ switchId,
448490 objectId : undefined ,
449491 } ;
450492 prev . sprite = blockSprite ;
@@ -464,22 +506,25 @@ export class Grid {
464506 ) ;
465507 return ;
466508 }
509+ assert (
510+ prevCell . block === Block . switchingBlockOFF ||
511+ prevCell . block === Block . switchingBlockON ,
512+ "block is not switchingBlock" ,
513+ ) ;
514+ const switchId = prevCell . switchId ;
515+ if ( ! switchId ) throw new Error ( "switchId is undefined" ) ;
467516 const blockSprite = createSprite (
468517 blockSize ,
469518 Block . switchingBlockON ,
470519 x ,
471520 y ,
472521 marginY ,
522+ switchColor ( switchId ) ,
473523 ) ;
474524 stage . addChild ( blockSprite ) ;
475- assert (
476- prevCell . block === Block . switchingBlockOFF ||
477- prevCell . block === Block . switchingBlockON ,
478- "block is not switchingBlock" ,
479- ) ;
480525 cells [ y ] [ x ] = {
481526 block : Block . switchingBlockON ,
482- switchId : prevCell . switchId ,
527+ switchId,
483528 objectId : undefined ,
484529 } ;
485530 prev . sprite = blockSprite ;
@@ -493,22 +538,25 @@ export class Grid {
493538 ) ;
494539 return ;
495540 }
541+ assert (
542+ prevCell . block === Block . switchingBlockOFF ||
543+ prevCell . block === Block . switchingBlockON ,
544+ "block is not switchingBlock" ,
545+ ) ;
546+ const switchId = prevCell . switchId ;
547+ if ( ! switchId ) throw new Error ( "switchId is undefined" ) ;
496548 const blockSprite = createSprite (
497549 blockSize ,
498550 Block . switchingBlockOFF ,
499551 x ,
500552 y ,
501553 marginY ,
554+ switchColor ( switchId ) ,
502555 ) ;
503556 stage . addChild ( blockSprite ) ;
504- assert (
505- prevCell . block === Block . switchingBlockOFF ||
506- prevCell . block === Block . switchingBlockON ,
507- "block is not switchingBlock" ,
508- ) ;
509557 cells [ y ] [ x ] = {
510558 block : Block . switchingBlockOFF ,
511- switchId : prevCell . switchId ,
559+ switchId,
512560 objectId : undefined ,
513561 } ;
514562 prev . sprite = blockSprite ;
@@ -674,6 +722,7 @@ function createSprite(
674722 x : number ,
675723 y : number ,
676724 marginY : number ,
725+ switchColor ?: number , // default: #ffa500
677726) {
678727 switch ( block ) {
679728 case Block . block : {
@@ -690,6 +739,7 @@ function createSprite(
690739 }
691740 case Block . switch : {
692741 const switchSprite = new Sprite ( switchTexture ) ;
742+ if ( switchColor ) switchSprite . tint = switchColor ;
693743 updateSprite ( switchSprite , blockSize , x , y , marginY ) ;
694744 return switchSprite ;
695745 }
@@ -706,20 +756,24 @@ function createSprite(
706756 }
707757 case Block . switchingBlockOFF : {
708758 const sprite = new Sprite ( rockTexture ) ;
709- sprite . tint = 0xffa500 ;
759+ console . log ( "switchColor" , switchColor ) ;
760+ if ( switchColor ) sprite . tint = switchColor ;
761+ else sprite . tint = 0xffa500 ;
710762 updateSprite ( sprite , blockSize , x , y , marginY ) ;
711763 return sprite ;
712764 }
713765 case Block . switchingBlockON : {
714766 const sprite = new Sprite ( rockTexture ) ;
715- sprite . tint = 0xffa500 ;
767+ if ( switchColor ) sprite . tint = switchColor ;
768+ else sprite . tint = 0xffa500 ;
716769 sprite . alpha = 0.3 ;
717770 updateSprite ( sprite , blockSize , x , y , marginY ) ;
718771 return sprite ;
719772 }
720773 case Block . switchPressed : {
721774 const sprite = new Sprite ( switchPressedTexture ) ;
722- // sprite.tint = 0x00ff00;
775+ if ( switchColor ) sprite . tint = switchColor ;
776+ else sprite . tint = 0xffa500 ;
723777 updateSprite ( sprite , blockSize , x , y , marginY ) ;
724778 return sprite ;
725779 }
@@ -775,3 +829,10 @@ export function printCells(cells: GridCell[][], context?: string) {
775829 . join ( "\n" ) } `,
776830 ) ;
777831}
832+
833+ function switchColor ( switchId : string ) : number | undefined {
834+ // TODO: 暫定的
835+ if ( switchId === "1" ) return 0xffa500 ;
836+ if ( switchId === "2" ) return 0x00ff00 ;
837+ return undefined ;
838+ }
0 commit comments