Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions src/iframe/life-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ let timerTime = 1000;
let isDragging = false;
let dragMode = false; // true: 黒にする, false: 白にする

const DEFAULT_BOARD_SIZE = 20;
const DEFAULT_CELL_SIZE = 30;

//変数設定
let boardSize = 20;
let cellSize = 30;
let boardSize = 20; //盤面の大きさ(20x20)
const cellSize = 600 / boardSize; //セルの大きさ(px)

// around: 周囲の生きたセル数 self: 自身が生きているかどうか
function isNextAlive(around, self) {
Expand All @@ -27,11 +24,19 @@ function isNextAlive(around, self) {
return false;
}

// cellの状態に応じた色を返す関数
function getStyle(cell) {
// cellがtrueなら黒、falseなら白を返す
return cell ? "black" : "white";
}

//Boardの初期化
let board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
const table = document.getElementById("game-board");

//盤面をBoardに従って変更する関数達(Boardを変更したら実行する)
function renderBoard() {
//盤面をBoardに従って変更する関数(Boardを変更したら必ず実行する)
// 初回の盤面生成
table.innerHTML = "";
for (let i = 0; i < boardSize; i++) {
const tr = document.createElement("tr");
Expand Down Expand Up @@ -68,6 +73,30 @@ function renderBoard() {
}
}

function rerender() {
// 2回目以降の盤面生成
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
const button = table.children[i].children[j].children[0];

// 色の更新
const currentCellColor = button.style.backgroundColor;
const expectedCellColor = getStyle(board[i][j]);
if (currentCellColor !== expectedCellColor) {
button.style.backgroundColor = expectedCellColor;
}

// セルサイズの更新
const currentCellsize = button.style.width;
const expectedCellsize = `${cellSize}px`;
if (currentCellsize !== expectedCellsize) {
button.style.width = expectedCellsize;
button.style.height = expectedCellsize;
}
}
}
}

document.addEventListener("mouseup", () => {
isDragging = false;
});
Expand Down Expand Up @@ -126,7 +155,7 @@ function progressBoard() {
}
board = newBoard;
generationChange(generationFigure + 1);
renderBoard();
rerender();
}

function resetTimer() {
Expand All @@ -147,15 +176,6 @@ on.pause = () => {
resetTimer();
};

on.board_resize = (newSize) => {
boardSize = newSize;
cellSize = Math.floor(DEFAULT_CELL_SIZE * (DEFAULT_BOARD_SIZE / newSize));
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
renderBoard();
generationChange(0);
resetTimer();
};

on.board_reset = () => {
//すべて白にBoardを変更
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
Expand Down
8 changes: 3 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
| "place_template"
| "save_board"
| "apply_board"
| "request_sync"
// unused events
| "board_resize";
| "request_sync";

type IncomingEvent = "generation_change" | "sync" | "save_board";

Expand Down Expand Up @@ -148,7 +146,7 @@
</div>

<div
class="fixed inset-x-0 bottom-0 z-40 transition-transform duration-300 bg-black pb-16"
class="fixed inset-x-0 bottom-0 z-40 transition-transform duration-300 bg-black pb-12"
class:translate-y-full={!bottomDrawerOpen}
class:translate-y-0={bottomDrawerOpen}
>
Expand Down Expand Up @@ -329,7 +327,7 @@
srcdoc={previewDoc}
title="Preview"
sandbox="allow-scripts"
class="w-[80%] h-[90%] rounded-lg mx-auto my-5 shadow-lg"
class="w-[80%] h-[90%] rounded-lg mx-auto my-5 bg-white shadow-lg"
onload={() => {
setTimeout(() => {
sendEvent("state_update");
Expand Down