1- import { Block , Facing , type MovableBlock } from "./constants.ts" ;
2- import type { Context } from "./context.ts" ;
3- import type { MovableBlocks , MovableObject } from "./grid.ts" ;
4- import type { History } from "./player.ts" ;
5-
6- export type Coords = {
7- x : number ;
8- y : number ;
9- } ;
10- export type AbilityInit = {
11- enabled ?: AbilityEnableOptions ;
12- inventoryIsInfinite ?: boolean ;
13- } ;
14- export type AbilityEnableOptions = {
15- // 回数 or Number.POSITIVE_INFINITY
16- copy : number ;
17- paste : number ;
18- cut : number ;
19- } ;
1+ import { Block , Facing } from "./constants.ts" ;
2+ import type {
3+ AbilityInit ,
4+ AbilityUsage ,
5+ Context ,
6+ Coords ,
7+ MovableObject ,
8+ } from "./public-types.ts" ;
9+ import type { StateSnapshot } from "./public-types.ts" ;
2010
2111export class AbilityControl {
2212 inventory : MovableObject | null = null ;
2313 inventoryIsInfinite = false ;
24- enabledAbilities : AbilityEnableOptions ;
14+ usage : AbilityUsage ;
2515 focused : Coords | undefined ;
2616 constructor ( cx : Context , options ?: AbilityInit ) {
27- this . enabledAbilities = options ?. enabled ?? {
17+ this . usage = options ?. enabled ?? {
2818 copy : Number . POSITIVE_INFINITY ,
2919 paste : Number . POSITIVE_INFINITY ,
3020 cut : Number . POSITIVE_INFINITY ,
@@ -34,8 +24,8 @@ export class AbilityControl {
3424 ...prev ,
3525 inventory : this . inventory ,
3626 inventoryIsInfinite : this . inventoryIsInfinite ,
37- ...this . enabledAbilities ,
38- undo : 1 ,
27+ ...this . usage ,
28+ undo : 0 ,
3929 redo : 0 ,
4030 } ) ) ;
4131 // document.addEventListener("copy", (e) => {
@@ -78,8 +68,8 @@ export class AbilityControl {
7868 }
7969 copy (
8070 cx : Context ,
81- newHistory : History ,
82- history : { list : History [ ] ; index : number } ,
71+ newHistory : StateSnapshot ,
72+ history : { list : StateSnapshot [ ] ; index : number } ,
8373 ) {
8474 if ( ! this . focused ) return ;
8575 const x = this . focused . x ;
@@ -97,24 +87,24 @@ export class AbilityControl {
9787 this . setInventory ( cx , movableObject ) ;
9888 cx . uiContext . update ( ( prev ) => ( {
9989 ...prev ,
100- copy : -- this . enabledAbilities . copy ,
90+ copy : -- this . usage . copy ,
10191 } ) ) ;
10292
10393 this . pushHistory (
10494 cx ,
10595 {
10696 ...newHistory ,
10797 inventory : this . inventory ,
108- enabledAbilities : this . enabledAbilities ,
98+ abilities : this . usage ,
10999 } ,
110100 history ,
111101 ) ;
112102 }
113103 paste (
114104 cx : Context ,
115105 facing : Facing ,
116- newHistory : History ,
117- history : { list : History [ ] ; index : number } ,
106+ newHistory : StateSnapshot ,
107+ history : { list : StateSnapshot [ ] ; index : number } ,
118108 ) {
119109 if ( ! this . focused ) return ;
120110 if ( ! this . inventory /*|| this.inventory === Block.air*/ ) return ;
@@ -155,7 +145,7 @@ export class AbilityControl {
155145 // const prevEnabled = { ...this.enabledAbilities };
156146 cx . uiContext . update ( ( prev ) => ( {
157147 ...prev ,
158- paste : -- this . enabledAbilities . paste ,
148+ paste : -- this . usage . paste ,
159149 } ) ) ;
160150
161151 this . pushHistory (
@@ -164,15 +154,15 @@ export class AbilityControl {
164154 ...newHistory ,
165155 inventory : this . inventory ,
166156 movableBlocks : cx . grid . movableBlocks ,
167- enabledAbilities : this . enabledAbilities ,
157+ abilities : this . usage ,
168158 } ,
169159 history ,
170160 ) ;
171161 }
172162 cut (
173163 cx : Context ,
174- newHistory : History ,
175- history : { list : History [ ] ; index : number } ,
164+ newHistory : StateSnapshot ,
165+ history : { list : StateSnapshot [ ] ; index : number } ,
176166 ) {
177167 if ( ! this . focused ) return ;
178168
@@ -198,10 +188,10 @@ export class AbilityControl {
198188 const positionY = movableObject . y + i . y ;
199189 cx . grid . setBlock ( positionX , positionY , Block . air ) ;
200190 }
201- const prevEnabled = { ...this . enabledAbilities } ;
191+ const prevEnabled = { ...this . usage } ;
202192 cx . uiContext . update ( ( prev ) => ( {
203193 ...prev ,
204- cut : -- this . enabledAbilities . cut ,
194+ cut : -- this . usage . cut ,
205195 } ) ) ;
206196
207197 this . pushHistory (
@@ -210,7 +200,7 @@ export class AbilityControl {
210200 ...newHistory ,
211201 inventory : this . inventory ,
212202 movableBlocks : cx . grid . movableBlocks ,
213- enabledAbilities : this . enabledAbilities ,
203+ abilities : this . usage ,
214204 } ,
215205 history ,
216206 ) ;
@@ -219,9 +209,9 @@ export class AbilityControl {
219209 // History については、 `docs/history-stack.png` を参照のこと
220210 pushHistory (
221211 cx : Context ,
222- h : History ,
212+ h : StateSnapshot ,
223213 history : {
224- list : History [ ] ;
214+ list : StateSnapshot [ ] ;
225215 index : number ;
226216 } ,
227217 ) {
@@ -262,7 +252,7 @@ export class AbilityControl {
262252 undo (
263253 cx : Context ,
264254 history : {
265- list : History [ ] ;
255+ list : StateSnapshot [ ] ;
266256 index : number ;
267257 } ,
268258 ) {
@@ -277,10 +267,10 @@ export class AbilityControl {
277267 this . setInventory ( cx , op . inventory ) ;
278268 cx . grid . setAllMovableBlocks ( cx , op . movableBlocks ) ;
279269
280- this . enabledAbilities = op . enabledAbilities ;
270+ this . usage = op . abilities ;
281271 cx . uiContext . update ( ( prev ) => ( {
282272 ...prev ,
283- ...this . enabledAbilities ,
273+ ...this . usage ,
284274 undo : history . index ,
285275 redo : history . list . length - history . index ,
286276 } ) ) ;
@@ -290,7 +280,7 @@ export class AbilityControl {
290280 redo (
291281 cx : Context ,
292282 history : {
293- list : History [ ] ;
283+ list : StateSnapshot [ ] ;
294284 index : number ;
295285 } ,
296286 ) {
@@ -305,10 +295,10 @@ export class AbilityControl {
305295 this . setInventory ( cx , op . inventory ) ;
306296 cx . grid . setAllMovableBlocks ( cx , op . movableBlocks ) ;
307297
308- this . enabledAbilities = op . enabledAbilities ;
298+ this . usage = op . abilities ;
309299 cx . uiContext . update ( ( prev ) => ( {
310300 ...prev ,
311- ...this . enabledAbilities ,
301+ ...this . usage ,
312302 undo : history . index ,
313303 redo : history . list . length - history . index ,
314304 } ) ) ;
@@ -321,43 +311,43 @@ export class AbilityControl {
321311 onGround : boolean ,
322312 facing : Facing ,
323313 history : {
324- list : History [ ] ;
314+ list : StateSnapshot [ ] ;
325315 index : number ;
326316 } ,
327317 playerAt : Coords ,
328318 ) {
329319 if ( ! ( e . ctrlKey || e . metaKey ) ) return undefined ;
330320
331- if ( this . enabledAbilities . paste > 0 && onGround && e . key === "v" ) {
321+ if ( this . usage . paste > 0 && onGround && e . key === "v" ) {
332322 const newHistory = {
333323 playerX : playerAt . x ,
334324 playerY : playerAt . y ,
335325 inventory : this . inventory ? this . inventory : null ,
336326 playerFacing : facing ,
337327 movableBlocks : cx . grid . movableBlocks ,
338- enabledAbilities : this . enabledAbilities ,
328+ enabledAbilities : this . usage ,
339329 } ;
340330 this . paste ( cx , facing , newHistory , history ) ;
341331 }
342- if ( this . enabledAbilities . copy > 0 && onGround && e . key === "c" ) {
332+ if ( this . usage . copy > 0 && onGround && e . key === "c" ) {
343333 const newHistory = {
344334 playerX : playerAt . x ,
345335 playerY : playerAt . y ,
346336 inventory : this . inventory ? this . inventory : null ,
347337 playerFacing : facing ,
348338 movableBlocks : cx . grid . movableBlocks ,
349- enabledAbilities : this . enabledAbilities ,
339+ enabledAbilities : this . usage ,
350340 } ;
351341 this . copy ( cx , newHistory , history ) ;
352342 }
353- if ( this . enabledAbilities . cut > 0 && onGround && e . key === "x" ) {
343+ if ( this . usage . cut > 0 && onGround && e . key === "x" ) {
354344 const newHistory = {
355345 playerX : playerAt . x ,
356346 playerY : playerAt . y ,
357347 inventory : this . inventory ? this . inventory : null ,
358348 playerFacing : facing ,
359349 movableBlocks : cx . grid . movableBlocks ,
360- enabledAbilities : this . enabledAbilities ,
350+ enabledAbilities : this . usage ,
361351 } ;
362352 this . cut ( cx , newHistory , history ) ;
363353 }
0 commit comments