move constants to const.tsx and setup game logic
This commit is contained in:
@@ -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,22 +50,43 @@ 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 */
|
|
||||||
board.map((row, rowNo): JSX.Element => {
|
|
||||||
return (
|
return (
|
||||||
<Grid key={key++} container columns={WIDTH} spacing={1} rowSpacing={1}>
|
<Grid key={key++} container columns={BOARD_WIDTH} sx={{ width: `${5 * 12}rem` }}>
|
||||||
{row.map((element, colNo): JSX.Element => {
|
{row.map((element, colNo): JSX.Element => {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
style={{
|
||||||
|
textAlign: "center",
|
||||||
|
margin: "auto",
|
||||||
|
verticalAlign: "middle"
|
||||||
|
}}
|
||||||
key={key++}
|
key={key++}
|
||||||
onDragOver={handleOnDragOver}
|
onDragOver={handleOnDragOver}
|
||||||
onDrop={(event) => handleDrop(colNo, rowNo, event)}>
|
onDrop={(event) => handleDrop(colNo, rowNo, event)}>
|
||||||
<Card sx={{ height: "5rem", width: "5rem", margins: "10px" }}>
|
<Card
|
||||||
<CardContent>
|
sx={{
|
||||||
<Typography variant="body1">{element.instruction.symbol}</Typography>
|
height: `${TILE_SIZE}rem`,
|
||||||
|
width: `${TILE_SIZE}rem`,
|
||||||
|
margin: 1,
|
||||||
|
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>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
@@ -66,9 +94,8 @@ function Board() {
|
|||||||
})}
|
})}
|
||||||
</Grid>
|
</Grid>
|
||||||
)
|
)
|
||||||
})
|
})}
|
||||||
}
|
</div>
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7
src/const.tsx
Normal file
7
src/const.tsx
Normal 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 {}
|
||||||
Reference in New Issue
Block a user