From 51d599a1606215cd4224f8b19008327f52b40bc5 Mon Sep 17 00:00:00 2001 From: Kento Sato Date: Thu, 18 Jan 2024 13:47:56 +0900 Subject: [PATCH] Add TurtleGraphicsCell classe --- src/app/lib/Character.ts | 15 +++++++- src/app/lib/TurtleGraphicsCell.ts | 21 +++++++++++ src/components/organisms/TurtleGraphics.tsx | 39 +++++++++------------ 3 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/app/lib/TurtleGraphicsCell.ts diff --git a/src/app/lib/Character.ts b/src/app/lib/Character.ts index 7e95eab6..07bdca9d 100644 --- a/src/app/lib/Character.ts +++ b/src/app/lib/Character.ts @@ -1,10 +1,19 @@ +export const CharacterColor = { + Red: 'red', + Blue: 'blue', + Green: 'green', + Yellow: 'yellow', + Purple: 'purple', +}; +type Color = (typeof CharacterColor)[keyof typeof CharacterColor]; + export class Character { id: number; name: string; x: number; y: number; direction: string; - color: string; + color: Color; penDown: boolean; path: string[]; @@ -136,6 +145,10 @@ export class Character { } } + setColor(color: Color): void { + this.color = color; + } + putPen(): void { this.penDown = true; } diff --git a/src/app/lib/TurtleGraphicsCell.ts b/src/app/lib/TurtleGraphicsCell.ts new file mode 100644 index 00000000..cff9cbd0 --- /dev/null +++ b/src/app/lib/TurtleGraphicsCell.ts @@ -0,0 +1,21 @@ +import type { CharacterColor } from './Character'; + +type Color = (typeof CharacterColor)[keyof typeof CharacterColor]; + +export class TurtleGraphicsCell { + id: number; + x: number; + y: number; + backgroundColor: string; + + constructor(id: number, x: number, y: number, backgroundColor: string) { + this.id = id; + this.x = x; + this.y = y; + this.backgroundColor = backgroundColor; + } + + setBackgroundColor(color: Color): void { + this.backgroundColor = color; + } +} diff --git a/src/components/organisms/TurtleGraphics.tsx b/src/components/organisms/TurtleGraphics.tsx index 1691bf0b..2323fcc3 100644 --- a/src/components/organisms/TurtleGraphics.tsx +++ b/src/components/organisms/TurtleGraphics.tsx @@ -2,9 +2,10 @@ import { Box, Grid, GridItem } from '@chakra-ui/react'; import Image from 'next/image'; -import React, { useState, useEffect } from 'react'; +import React, { useState } from 'react'; import { Character } from '../../app/lib/Character'; +import { TurtleGraphicsCell } from '../../app/lib/TurtleGraphicsCell'; // 原点(左上隅)の座標 const ORIGIN_X = 1; @@ -26,25 +27,13 @@ export const TurtleGraphics: React.FC = ({ isEnableOperation: isEnableOperation = false, }) => { const [characters, setCharacters] = useState(initialCharacters); - - useEffect(() => { - // 軌跡の描画 - for (const character of characters) { - if (!character.penDown) continue; - - for (const position of character.path) { - const [x, y] = position.split(',').map(Number); - const gridCell = document.querySelectorAll('.grid-cell')[ - x - ORIGIN_X + (y - ORIGIN_Y) * gridColumns - ] as HTMLElement; - - if (!gridCell) return; - - gridCell.style.backgroundColor = character.color; - gridCell.style.opacity = '0.5'; - } - } - }, [characters, gridColumns]); + const [cells] = useState( + Array.from({ length: gridColumns * gridRows }).map((_, index) => { + const x = (index % gridColumns) + ORIGIN_X; + const y = Math.floor(index / gridColumns) + ORIGIN_Y; + return new TurtleGraphicsCell(index, x, y, ''); + }) + ); const updateCharacter = (character: Character, updater: (char: Character) => void): void => { setCharacters((prevCharacters) => @@ -111,8 +100,14 @@ export const TurtleGraphics: React.FC = ({ templateColumns={`repeat(${gridColumns}, ${gridSize}px)`} templateRows={`repeat(${gridRows}, ${gridSize}px)`} > - {Array.from({ length: gridColumns * gridRows }).map((_, index) => ( - + {cells.map((cell) => ( + ))} {characters.map((character) => (