From 6bd184ac5bdc7a76263c2c3a7b68f8833bbc9993 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Mon, 22 Aug 2022 16:13:08 +0200 Subject: [PATCH] move constants to const.tsx and setup game logic --- src/components/Board.tsx | 107 ++++++++++++++++++++++++--------------- src/const.tsx | 7 +++ 2 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 src/const.tsx diff --git a/src/components/Board.tsx b/src/components/Board.tsx index be5a1fd..a19689e 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -1,25 +1,34 @@ import React, { useState } from "react" import { Card, CardContent, Grid, Typography } from "@mui/material" import instructions, { Instruction } from "../Instructions" +import { BOARD_HEIGHT, BOARD_WIDTH, SELECTED_COLOR, SIDEBAR_WIDTH, TILE_SIZE } from "../const" -interface Tile { - pointedAt: boolean - instruction: Instruction +// considering renaming this to vector +interface Point { + x: number + y: number +} + +const Direction = { + Up: { x: -1, y: 0 }, + Right: { x: 0, y: 1 }, + Left: { x: 0, y: -1 }, + Down: { x: 1, y: 0 } } function Board() { - const WIDTH = 10 - const HEIGHT = 10 let key = 0 - const [board, setBoard] = useState( - new Array(HEIGHT).fill(0).map(() => - new Array(WIDTH).fill(0).map((): Tile => { - return { pointedAt: false, instruction: structuredClone(instructions[0]) } - }) - ) + const [direction, setDirection] = useState(Direction.Right) + const [pointer, setPointer] = useState({ x: 0, y: 0 }) + + // this sure is a clean readable way to initialize a 2d array + const [board, setBoard] = useState( + new Array(BOARD_HEIGHT) + .fill(0) + .map(() => new Array(BOARD_WIDTH).fill(0).map(() => structuredClone(instructions[0]))) ) - //.map(() => ({pointedAt: false, value: " ")} + const handleDrop = ( col: number, row: number, @@ -30,10 +39,8 @@ function Board() { return } - const data = event.dataTransfer.getData("text/plain") - console.log(data, row, col) const x = [...board] - x[row][col].instruction.symbol = data + x[row][col].symbol = event.dataTransfer.getData("text/plain") console.log(x) setBoard(x) } @@ -43,32 +50,52 @@ function Board() { return false } + const iterate = () => { + return null + } + return ( - <> - { - /* PRINT GRID */ - board.map((row, rowNo): JSX.Element => { - return ( - - {row.map((element, colNo): JSX.Element => { - return ( -
handleDrop(colNo, rowNo, event)}> - - - {element.instruction.symbol} - - -
- ) - })} -
- ) - }) - } - +
+ {board.map((row, rowNo): JSX.Element => { + return ( + + {row.map((element, colNo): JSX.Element => { + return ( +
handleDrop(colNo, rowNo, event)}> + + + + {element.symbol} + + + +
+ ) + })} +
+ ) + })} +
) } diff --git a/src/const.tsx b/src/const.tsx new file mode 100644 index 0000000..ff9e124 --- /dev/null +++ b/src/const.tsx @@ -0,0 +1,7 @@ +export const SIDEBAR_WIDTH = 8 +export const SELECTED_COLOR = "0xFF0000" +export const BOARD_WIDTH = 10 +export const BOARD_HEIGHT = 10 +export const TILE_SIZE = 5 + +export default {}