Skip to content

Commit 2476c10

Browse files
committed
左を向いたときの貼り付け位置を調整・貼り付け可否の修正
1 parent 7732e4f commit 2476c10

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/ability.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,34 @@ export class AbilityControl {
7070
if (!movableObject) return;
7171
this.inventory = movableObject;
7272
}
73-
paste(cx: Context) {
73+
paste(cx: Context, facing: Facing) {
7474
if (!this.focused) return;
7575
if (!this.inventory /*|| this.inventory === Block.air*/) return;
76-
const x = this.focused.x;
76+
const width =
77+
this.inventory.relativePositions.reduce(
78+
(acc, i) => Math.max(acc, i.x),
79+
0,
80+
) -
81+
this.inventory.relativePositions.reduce(
82+
(acc, i) => Math.min(acc, i.x),
83+
1000,
84+
) +
85+
1;
86+
87+
const x = this.focused.x - (facing === Facing.left ? width - 1 : 0);
7788
const y = this.focused.y;
78-
const target = cx.grid.getBlock(this.focused.x, this.focused.y);
79-
if (!target || target !== Block.air) return;
89+
90+
for (const i of this.inventory.relativePositions) {
91+
const positionX = x + i.x;
92+
const positionY = y + i.y;
93+
const target = cx.grid.getBlock(positionX, positionY);
94+
if (target !== Block.air) {
95+
// すでに何かある場合は、ペーストできない
96+
return;
97+
}
98+
}
99+
// const target = cx.grid.getBlock(this.focused.x, this.focused.y);
100+
// if (!target || target !== Block.air) return;
80101
const prevInventory = this.inventory;
81102

82103
cx.grid.setMovableObject(cx, x, y, this.inventory);
@@ -157,11 +178,16 @@ export class AbilityControl {
157178
}
158179
console.log(`history: ${this.historyIndex} / ${this.history.length}`);
159180
}
160-
handleKeyDown(cx: Context, e: KeyboardEvent, onGround: boolean) {
181+
handleKeyDown(
182+
cx: Context,
183+
e: KeyboardEvent,
184+
onGround: boolean,
185+
facing: Facing,
186+
) {
161187
if (!(e.ctrlKey || e.metaKey)) return;
162188

163189
if (this.enabled.paste && onGround && e.key === "v") {
164-
this.paste(cx);
190+
this.paste(cx, facing);
165191
}
166192
if (this.enabled.copy && onGround && e.key === "c") {
167193
this.copy(cx);

src/player.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class Player {
8484
}
8585
handleInput(cx: Context, event: KeyboardEvent, eventIsKeyDown: boolean) {
8686
if (eventIsKeyDown) {
87-
this.ability.handleKeyDown(cx, event, this.onGround);
87+
this.ability.handleKeyDown(cx, event, this.onGround, this.facing);
8888
}
8989
switch (event.key) {
9090
case "Control":

0 commit comments

Comments
 (0)