Skip to content

Commit 68c2f5f

Browse files
committed
historyの先頭に初期状態を追加
1 parent a0f7b9d commit 68c2f5f

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/ability.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ type History = {
2929
}[];
3030
};
3131

32-
function isMovableObject(obj: MovableObject | Block): obj is MovableObject {
33-
return (obj as MovableObject).objectId !== undefined;
34-
}
35-
3632
export class AbilityControl {
37-
// history: History[] = [];
38-
// historyIndex = 0;
3933
inventory: MovableObject | null = null;
4034
inventoryIsInfinite = false;
4135
enabled: AbilityEnableOptions;
@@ -74,10 +68,6 @@ export class AbilityControl {
7468
const movableObject = cx.grid.getMovableObject(x, y);
7569
if (!movableObject) return;
7670
this.inventory = movableObject;
77-
// cx.gridとinventryは重複しないように
78-
// cx.grid.movableBlocks = cx.grid.movableBlocks.filter(
79-
// (block) => block.objectId !== movableObject.objectId,
80-
// );
8171
}
8272
paste(cx: Context, facing: Facing) {
8373
if (!this.focused) return;
@@ -124,10 +114,7 @@ export class AbilityControl {
124114
// removable 以外はカットできない
125115
if (!target || target !== Block.movable) return;
126116
const movableObject = cx.grid.getMovableObject(x, y);
127-
if (!movableObject) {
128-
console.log("aaaaaa");
129-
return;
130-
}
117+
if (!movableObject) return;
131118
this.inventory = movableObject;
132119
// cx.gridとinventryは重複しないように
133120
// 取得したオブジェクトは削除する
@@ -142,18 +129,19 @@ export class AbilityControl {
142129
}
143130

144131
// History については、 `docs/history-stack.png` を参照のこと
132+
// すべての状態を保存
145133
pushHistory(
146134
h: History,
147135
history: {
148136
list: History[];
149137
index: number;
150138
},
151139
) {
152-
history.list = history.list.slice(0, history.index);
140+
// history.listの先頭(初期状態)は残す
141+
history.list = history.list.slice(0, Math.max(history.index, 1));
153142
history.list.push(JSON.parse(JSON.stringify(h)));
154143
history.index = history.list.length;
155144
console.log(`history: ${history.index} / ${history.list.length}`);
156-
console.log(history);
157145
}
158146
undo(
159147
cx: Context,
@@ -162,13 +150,10 @@ export class AbilityControl {
162150
index: number;
163151
},
164152
) {
165-
console.log(history.index);
166153
if (history.index <= 0) return;
167154
history.index--; // undo は、巻き戻し後の index で計算する
168155
const op = history.list[history.index];
169156

170-
console.log("bbb");
171-
172157
// すべてのオブジェクトを削除
173158
cx.grid.clearAllMovableBlocks();
174159

@@ -285,7 +270,6 @@ export class AbilityControl {
285270
},
286271
history,
287272
);
288-
console.log("x", history);
289273
}
290274
if (e.key === "z") {
291275
this.undo(cx, history);

src/player.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ export class Player {
6969
this.onGround = false;
7070
this.jumpingBegin = null;
7171
this.holdingKeys = {};
72+
this.history.list.push({
73+
playerX: this.x,
74+
playerY: this.y,
75+
playerFacing: this.facing,
76+
inventory: null,
77+
movableBlocks: cx.grid.movableBlocks,
78+
});
7279
}
7380
get x() {
7481
return this.sprite.x;

0 commit comments

Comments
 (0)