Skip to content

Commit

Permalink
Add TurtleGraphicsCell classe
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatehito committed Jan 18, 2024
1 parent b0f26d8 commit 51d599a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
15 changes: 14 additions & 1 deletion src/app/lib/Character.ts
Original file line number Diff line number Diff line change
@@ -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[];

Expand Down Expand Up @@ -136,6 +145,10 @@ export class Character {
}
}

setColor(color: Color): void {
this.color = color;
}

putPen(): void {
this.penDown = true;
}
Expand Down
21 changes: 21 additions & 0 deletions src/app/lib/TurtleGraphicsCell.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 17 additions & 22 deletions src/components/organisms/TurtleGraphics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,25 +27,13 @@ export const TurtleGraphics: React.FC<TurtleGraphicsProps> = ({
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<TurtleGraphicsCell[]>(
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) =>
Expand Down Expand Up @@ -111,8 +100,14 @@ export const TurtleGraphics: React.FC<TurtleGraphicsProps> = ({
templateColumns={`repeat(${gridColumns}, ${gridSize}px)`}
templateRows={`repeat(${gridRows}, ${gridSize}px)`}
>
{Array.from({ length: gridColumns * gridRows }).map((_, index) => (
<GridItem key={index} borderColor="black" borderWidth="1px" className="grid-cell" />
{cells.map((cell) => (
<GridItem
key={cell.id}
backgroundColor={cell.backgroundColor}
borderColor="black"
borderWidth="1px"
className="grid-cell"
/>
))}
{characters.map((character) => (
<Box
Expand Down

0 comments on commit 51d599a

Please sign in to comment.