move constants to const.tsx and setup game logic

This commit is contained in:
Ole Morud
2022-08-22 16:13:08 +02:00
parent ae0191daff
commit 6bd184ac5b
2 changed files with 74 additions and 40 deletions

View File

@@ -1,25 +1,34 @@
import React, { useState } from "react" import React, { useState } from "react"
import { Card, CardContent, Grid, Typography } from "@mui/material" import { Card, CardContent, Grid, Typography } from "@mui/material"
import instructions, { Instruction } from "../Instructions" import instructions, { Instruction } from "../Instructions"
import { BOARD_HEIGHT, BOARD_WIDTH, SELECTED_COLOR, SIDEBAR_WIDTH, TILE_SIZE } from "../const"
interface Tile { // considering renaming this to vector
pointedAt: boolean interface Point {
instruction: Instruction 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() { function Board() {
const WIDTH = 10
const HEIGHT = 10
let key = 0 let key = 0
const [board, setBoard] = useState<Tile[][]>( const [direction, setDirection] = useState<Point>(Direction.Right)
new Array(HEIGHT).fill(0).map(() => const [pointer, setPointer] = useState<Point>({ x: 0, y: 0 })
new Array(WIDTH).fill(0).map((): Tile => {
return { pointedAt: false, instruction: structuredClone(instructions[0]) } // this sure is a clean readable way to initialize a 2d array
}) const [board, setBoard] = useState<Instruction[][]>(
) new Array(BOARD_HEIGHT)
.fill(0)
.map(() => new Array(BOARD_WIDTH).fill(0).map(() => structuredClone(instructions[0])))
) )
//.map(() => ({pointedAt: false, value: " ")}
const handleDrop = ( const handleDrop = (
col: number, col: number,
row: number, row: number,
@@ -30,10 +39,8 @@ function Board() {
return return
} }
const data = event.dataTransfer.getData("text/plain")
console.log(data, row, col)
const x = [...board] const x = [...board]
x[row][col].instruction.symbol = data x[row][col].symbol = event.dataTransfer.getData("text/plain")
console.log(x) console.log(x)
setBoard(x) setBoard(x)
} }
@@ -43,32 +50,52 @@ function Board() {
return false return false
} }
const iterate = () => {
return null
}
return ( return (
<> <div style={{ marginLeft: `${SIDEBAR_WIDTH + 1}rem` }}>
{ {board.map((row, rowNo): JSX.Element => {
/* PRINT GRID */ return (
board.map((row, rowNo): JSX.Element => { <Grid key={key++} container columns={BOARD_WIDTH} sx={{ width: `${5 * 12}rem` }}>
return ( {row.map((element, colNo): JSX.Element => {
<Grid key={key++} container columns={WIDTH} spacing={1} rowSpacing={1}> return (
{row.map((element, colNo): JSX.Element => { <div
return ( style={{
<div textAlign: "center",
key={key++} margin: "auto",
onDragOver={handleOnDragOver} verticalAlign: "middle"
onDrop={(event) => handleDrop(colNo, rowNo, event)}> }}
<Card sx={{ height: "5rem", width: "5rem", margins: "10px" }}> key={key++}
<CardContent> onDragOver={handleOnDragOver}
<Typography variant="body1">{element.instruction.symbol}</Typography> onDrop={(event) => handleDrop(colNo, rowNo, event)}>
</CardContent> <Card
</Card> sx={{
</div> height: `${TILE_SIZE}rem`,
) width: `${TILE_SIZE}rem`,
})} margin: 1,
</Grid> color:
) rowNo === pointer.x && colNo === pointer.y ? SELECTED_COLOR : "0xFFFFFF"
}) }}>
} <CardContent
</> sx={{
display: "inline",
float: "none",
margin: "auto"
}}>
<Typography sx={{ mt: "10%" }} variant="body1">
{element.symbol}
</Typography>
</CardContent>
</Card>
</div>
)
})}
</Grid>
)
})}
</div>
) )
} }

7
src/const.tsx Normal file
View File

@@ -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 {}