1- import { Block , Facing } from "./constants.ts" ;
1+ import { Block , Facing , type MovableBlock } from "./constants.ts" ;
22import type { Context } from "./context.ts" ;
3+ import type { MovableBlocks , MovableObject } from "./grid.ts" ;
34
45export type Coords = {
56 x : number ;
@@ -15,17 +16,22 @@ export type AbilityEnableOptions = {
1516} ;
1617type History = {
1718 at : { x : number ; y : number } ;
18- from : Block ;
19- to : Block ;
19+ from : MovableObject | Block ;
20+ to : MovableObject | Block ;
2021 inventory : {
21- before : Block | null ;
22- after : Block | null ;
22+ before : MovableObject | null ;
23+ after : MovableObject | null ;
2324 } ;
2425} ;
26+
27+ function isMovableObject ( obj : MovableObject | Block ) : obj is MovableObject {
28+ return ( obj as MovableObject ) . objectId !== undefined ;
29+ }
30+
2531export class AbilityControl {
2632 history : History [ ] = [ ] ;
2733 historyIndex = 0 ;
28- inventory : Block | null = null ;
34+ inventory : MovableObject | null = null ;
2935 inventoryIsInfinite = false ;
3036 enabled : AbilityEnableOptions ;
3137 focused : Coords | undefined ;
@@ -56,17 +62,25 @@ export class AbilityControl {
5662 }
5763 copy ( cx : Context ) {
5864 if ( ! this . focused ) return ;
59- const target = cx . grid . getBlock ( this . focused . x , this . focused . y ) ;
65+ const x = this . focused . x ;
66+ const y = this . focused . y ;
67+ const target = cx . grid . getBlock ( x , y ) ;
6068 if ( ! target || target !== Block . movable ) return ;
61- this . inventory = target ;
69+ const movableObject = cx . grid . getMovableObject ( x , y ) ;
70+ if ( ! movableObject ) return ;
71+ this . inventory = movableObject ;
6272 }
6373 paste ( cx : Context ) {
6474 if ( ! this . focused ) return ;
65- if ( ! this . inventory || this . inventory === Block . air ) return ;
75+ if ( ! this . inventory /*|| this.inventory === Block.air*/ ) return ;
76+ const x = this . focused . x ;
77+ const y = this . focused . y ;
6678 const target = cx . grid . getBlock ( this . focused . x , this . focused . y ) ;
6779 if ( ! target || target !== Block . air ) return ;
6880 const prevInventory = this . inventory ;
69- cx . grid . setBlock ( cx , this . focused . x , this . focused . y , this . inventory ) ;
81+
82+ cx . grid . setMovableObject ( cx , x , y , this . inventory ) ;
83+
7084 if ( ! this . inventoryIsInfinite ) {
7185 this . inventory = null ;
7286 }
@@ -83,20 +97,31 @@ export class AbilityControl {
8397 }
8498 cut ( cx : Context ) {
8599 if ( ! this . focused ) return ;
86- const target = cx . grid . getBlock ( this . focused . x , this . focused . y ) ;
100+ const x = this . focused . x ;
101+ const y = this . focused . y ;
102+ const target = cx . grid . getBlock ( x , y ) ;
87103 // removable 以外はカットできない
88104 if ( ! target || target !== Block . movable ) return ;
105+ const movableObject = cx . grid . getMovableObject ( x , y ) ;
106+ if ( ! movableObject ) return ;
89107 const prevInventory = this . inventory ;
90- this . inventory = target ;
91- cx . grid . setBlock ( cx , this . focused . x , this . focused . y , Block . air ) ;
108+ this . inventory = movableObject ;
109+
110+ for ( const i of movableObject . relativePositions ) {
111+ const positionX = movableObject . x + i . x ;
112+ const positionY = movableObject . y + i . y ;
113+ cx . grid . setBlock ( cx , positionX , positionY , Block . air ) ;
114+ }
115+
116+ // cx.grid.setBlock(cx, this.focused.x, this.focused.y, Block.air);
92117
93118 this . pushHistory ( {
94119 at : { ...this . focused } ,
95120 from : target ,
96121 to : Block . air ,
97122 inventory : {
98123 before : prevInventory ,
99- after : target ,
124+ after : movableObject ,
100125 } ,
101126 } ) ;
102127 }
@@ -112,7 +137,11 @@ export class AbilityControl {
112137 if ( this . historyIndex <= 0 ) return ;
113138 this . historyIndex -- ; // undo は、巻き戻し後の index で計算する
114139 const op = this . history [ this . historyIndex ] ;
115- cx . grid . setBlock ( cx , op . at . x , op . at . y , op . from ) ;
140+ if ( ! isMovableObject ( op . from ) ) {
141+ cx . grid . setBlock ( cx , op . at . x , op . at . y , op . from ) ;
142+ } else {
143+ cx . grid . setMovableObject ( cx , op . at . x , op . at . y , op . from ) ;
144+ }
116145 this . inventory = op . inventory . before ;
117146 console . log ( `history: ${ this . historyIndex } / ${ this . history . length } ` ) ;
118147 }
@@ -121,7 +150,11 @@ export class AbilityControl {
121150 const op = this . history [ this . historyIndex ] ;
122151 this . historyIndex ++ ; // redo は、巻き戻し前の index
123152 this . inventory = op . inventory . after ;
124- cx . grid . setBlock ( cx , op . at . x , op . at . y , op . to ) ;
153+ if ( ! isMovableObject ( op . to ) ) {
154+ cx . grid . setBlock ( cx , op . at . x , op . at . y , op . to ) ;
155+ } else {
156+ cx . grid . setMovableObject ( cx , op . at . x , op . at . y , op . to ) ;
157+ }
125158 console . log ( `history: ${ this . historyIndex } / ${ this . history . length } ` ) ;
126159 }
127160 handleKeyDown ( cx : Context , e : KeyboardEvent , onGround : boolean ) {
0 commit comments