Skip to content

Commit cc513ff

Browse files
committed
コンフリクトの解消
1 parent da0b9d4 commit cc513ff

2 files changed

Lines changed: 84 additions & 24 deletions

File tree

src/iframe/life-game.js

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ let generationFigure = 0;
66
let timerTime = 1000;
77
let isDragging = false;
88
let dragMode = false; // true: 黒にする, false: 白にする
9+
let isPlacingTemplate = false;
10+
let patternShape = [];
11+
let patternHeight = 0;
12+
let patternWidth = 0;
13+
let previewCells = [];
914

1015
//変数設定
1116
let boardSize = 20; //盤面の大きさ(20x20)
@@ -51,20 +56,50 @@ function renderBoard() {
5156
button.style.height = `${cellSize}px`;
5257
button.style.padding = "0"; //cellSizeが小さいとき、セルが横長になることを防ぐ
5358
button.style.display = "block"; //cellSizeが小さいとき、行間が空きすぎるのを防ぐ
59+
button.onclick = () => {
60+
if (isPlacingTemplate) {
61+
clearPreview();
62+
isPlacingTemplate = false;
63+
if (i + patternHeight < boardSize + 1 && j + patternWidth < boardSize + 1) {
64+
for (let r = 0; r < patternHeight; r++) {
65+
for (let c = 0; c < patternWidth; c++) {
66+
const boardRow = i + r;
67+
const boardCol = j + c;
68+
board[boardRow][boardCol] = patternShape[r][c] === 1;
69+
}
70+
}
71+
rerender();
72+
generationChange(0);
73+
resetTimer();
74+
stop();
75+
} else {
76+
window.parent.postMessage(
77+
{
78+
type: "Size shortage",
79+
data: {},
80+
},
81+
"*",
82+
);
83+
}
84+
}
85+
};
5486
button.onmousedown = (e) => {
5587
e.preventDefault();
56-
if (timer === "stop") {
88+
if (timer === "stop" && !isPlacingTemplate) {
5789
isDragging = true;
5890
board[i][j] = !board[i][j];
5991
dragMode = board[i][j];
6092
button.style.backgroundColor = board[i][j] ? "black" : "white";
6193
}
6294
};
6395
button.onmouseenter = () => {
64-
if (isDragging && timer === "stop" && board[i][j] !== dragMode) {
96+
if (isDragging && timer === "stop" && board[i][j] !== dragMode && !isPlacingTemplate) {
6597
board[i][j] = dragMode;
6698
button.style.backgroundColor = board[i][j] ? "black" : "white";
6799
}
100+
if (isPlacingTemplate) {
101+
drawPreview(i, j);
102+
}
68103
};
69104
td.appendChild(button);
70105
tr.appendChild(td);
@@ -73,6 +108,37 @@ function renderBoard() {
73108
}
74109
}
75110

111+
table.onmouseleave = () => {
112+
if (isPlacingTemplate) {
113+
clearPreview();
114+
}
115+
};
116+
117+
function drawPreview(row, col) {
118+
clearPreview();
119+
for (let r = 0; r < patternHeight; r++) {
120+
for (let c = 0; c < patternWidth; c++) {
121+
if (patternShape[r][c] === 1) {
122+
const boardRow = row + r;
123+
const boardCol = col + c;
124+
if (boardRow < boardSize && boardCol < boardSize) {
125+
const cell = table.rows[boardRow].cells[boardCol].firstChild;
126+
cell.style.backgroundColor = "gray";
127+
previewCells.push({ row: boardRow, col: boardCol });
128+
}
129+
}
130+
}
131+
}
132+
}
133+
134+
function clearPreview() {
135+
previewCells.forEach((cellPos) => {
136+
const cell = table.rows[cellPos.row].cells[cellPos.col].firstChild;
137+
cell.style.backgroundColor = board[cellPos.row][cellPos.col] ? "black" : "white";
138+
});
139+
previewCells = [];
140+
}
141+
76142
function rerender() {
77143
// 2回目以降の盤面生成
78144
for (let i = 0; i < boardSize; i++) {
@@ -216,11 +282,12 @@ on.request_sync = () => {
216282
console.log("generationFigure:", generationFigure, "boardSize:", boardSize);
217283
};
218284

219-
on.place_template = (newBoard) => {
220-
board = newBoard;
221-
renderBoard();
222-
generationChange(0);
223-
resetTimer();
285+
on.place_template = (template) => {
286+
patternShape = template;
287+
patternHeight = patternShape.length;
288+
patternWidth = patternShape[0].length;
289+
isPlacingTemplate = true;
290+
table.style.cursor = "crosshair";
224291
stop();
225292
};
226293

src/routes/+page.svelte

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| "apply_board"
4848
| "request_sync";
4949
50-
type IncomingEvent = "generation_change" | "sync" | "save_board";
50+
type IncomingEvent = "generation_change" | "sync" | "Size shortage" | "save_board";
5151
5252
function handleMessage(event: MessageEvent<{ type: IncomingEvent; data: unknown }>) {
5353
switch (event.data.type) {
@@ -61,6 +61,14 @@
6161
sizeValue = data.boardSize;
6262
break;
6363
}
64+
case "Size shortage": {
65+
alert(
66+
isJapanese
67+
? "盤面からはみ出してしまうため、キャンセルしました"
68+
: "This action was canceled because it would have extended beyond the board.",
69+
);
70+
break;
71+
}
6472
case "save_board": {
6573
const board = event.data.data as boolean[][];
6674
const preview = createPreview(board);
@@ -168,13 +176,8 @@
168176
onclick={() => {
169177
sendEvent("request_sync");
170178

171-
const newBoard = Array.from({ length: sizeValue }, () =>
172-
Array.from({ length: sizeValue }, () => false),
173-
);
174179
const patternData = patterns[patternName];
175180
const patternShape = patternData.shape;
176-
const patternHeight = patternShape.length;
177-
const patternWidth = patternShape[0].length;
178181

179182
if (sizeValue < (patternData.minBoardSize || 0)) {
180183
if (isJapanese) {
@@ -190,18 +193,8 @@
190193
return;
191194
}
192195
// パターンがボードの中央に来るよう、パターンの左上のセルの位置(startRow, startCol)を調整
193-
const startRow = Math.floor((sizeValue - patternHeight) / 2);
194-
const startCol = Math.floor((sizeValue - patternWidth) / 2);
195-
196-
for (let r = 0; r < patternHeight; r++) {
197-
for (let c = 0; c < patternWidth; c++) {
198-
const boardRow = startRow + r;
199-
const boardCol = startCol + c;
200-
newBoard[boardRow][boardCol] = patternShape[r][c] === 1;
201-
}
202-
}
203196
bottomDrawerOpen = false;
204-
sendEvent("place_template", newBoard);
197+
sendEvent("place_template", patternShape);
205198
}}
206199
>
207200
<img

0 commit comments

Comments
 (0)