-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
308 lines (281 loc) · 11.3 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import './App.css'
import { pieces } from './pieces'
import type { IBoard, IPiece, IState } from './model'
import { Square } from './components/Square'
import { checkIfPieceFitsAndUpdateBoard, clearFullRows, createEmptyBoard, generateFitTest, getPieceHeight, getPieceWidth, snapPositionToBoard, drawN, calculateLocationFromIndex, createRainbowBoard, checkIfPieceCanBePlaced } from './util'
import { boardSize, getSquareSizePixels, highscoreLocalStorageKey, undos } from './constants'
import { usePointerExit } from './hooks'
export default function App() {
const [state, setState] = useState<IState>(getInitialState)
const [prevState, setPrevState] = useState<IState>(state)
const [undosLeft, setUndosLeft] = useState<number>(undos)
const [queue, setQueue] = useState<(IPiece | null)[] | null>(null)
const undoRef = useRef<HTMLButtonElement>(null!)
const boardRef = useRef<HTMLDivElement>(null)
const [pointer, setPointer] = useState<{
offset: [number, number],
pos: [number, number],
type: string
}>({
offset: [0, 0],
pos: [0, 0],
type: "touch"
})
const mousePosWithOffset = useMemo<[number, number]>(() => {
const [selectedPiece] = tryGetPieceFromState(state)
const offsets = selectedPiece ? getOffsetFromPieceInPixels(selectedPiece, pointer.type) : [0, 0]
return ([
pointer.pos[0] - offsets[0],
pointer.pos[1] - offsets[1]
])
}, [pointer.pos, pointer.type, state])
const { pieceCanBePlacedValues, gameOver } = useMemo(() => {
const values = state.userPieces.map((p) =>
!p ? false : checkIfPieceCanBePlaced(state.board, p)
)
const gameOver = values.every((v) => !v)
return { pieceCanBePlacedValues: values, gameOver }
}, [state.board, state.userPieces])
const [fit, boardWithPreview] = useMemo<[boolean, IBoard]>(() => {
if (gameOver) return [false, createRainbowBoard(boardSize)]
const [selectedPiece] = tryGetPieceFromState(state)
if (!selectedPiece) return [false, state.board]
if (!boardRef.current) {
throw Error("Cannot snap position when board does not exist")
}
const { width, height, x, y } = boardRef.current.getBoundingClientRect()
const { colIndex, rowIndex } = snapPositionToBoard({ boardSize, width, height, x, y, pageX: mousePosWithOffset[0], pageY: mousePosWithOffset[1] })
const [fit, board] = checkIfPieceFitsAndUpdateBoard(
{ board: state.board, piece: selectedPiece, squareLocation: [colIndex, rowIndex], boardSize })
return [fit, fit ? board : state.board]
}, [gameOver, mousePosWithOffset, state])
const { rowsToClear, colsToClear } = useMemo<{
boardWithClearedRows: IBoard
rowsToClear: number[]
colsToClear: number[]
}>(() => {
const { newBoard: boardWithClearedRows, rowsToClear, colsToClear } = fit
? clearFullRows(boardWithPreview, boardSize)
: { newBoard: state.board, rowsToClear: [], colsToClear: [] }
return { boardWithClearedRows, rowsToClear, colsToClear}
}, [boardWithPreview, fit, state.board])
// set this initial to disabled. So we don't mess with the 'undosLeft <= 0' on the button tag
useEffect(() => {undoRef.current.disabled = true}, [undoRef])
useEffect(() => {
const highscore = Math.max(state.highscore, state.score).toString()
localStorage.setItem(highscoreLocalStorageKey, highscore)
}, [state.score, state.highscore])
const resetGame = () => {
setState(prevState => ({
...getInitialState(),
highscore: Math.max(prevState.highscore, prevState.score)
}))
setUndosLeft(undos)
setPrevState(getInitialState())
// disable on reset
undoRef.current.disabled = true
}
const undo = () => {
// console.log(Object.is(prevState, state)) // This gives false..
// this will just exclude userPieces from both objects, because they (almost) never match.
// this wil prevent undo when reset has been done
const state_ = JSON.stringify(state, (k, v) => k === "userPieces" ? null : v)
const getInitialState_ = JSON.stringify(getInitialState(), (k, v) => k === "userPieces" ? null : v)
if(
JSON.stringify(prevState.userPieces) !== JSON.stringify(state.userPieces) &&
state_ !== getInitialState_
){
setState(prevState)
setQueue(state.userPieces)
setUndosLeft(undosLeft - 1)
// disable after undo
undoRef.current.disabled = true
// Cancel that the brick was picked up, because we set the prevState on the pick-up
pointerUpHandler()
}
}
const pointerUpHandler = useCallback(function pointerUpHandler() {
if (!boardRef.current) return
if (!fit) {
setState(p => ({...p, selectedPieceIndex: null}))
return
}
// const squareLocation: [number, number] = [0, 0]
// const [shouldSucceed, newTest] = generateFitTest(state, squareLocation)
const [selectedPiece, selectedPieceIndex] = tryGetPieceFromState(state)
if (!selectedPiece) return
const { newBoard, rowsAndColsCleared } = clearFullRows(boardWithPreview, boardSize)
setState(prevState => {
const userPieces = prevState.userPieces.map((piece, i) => i === selectedPieceIndex ? null : piece)
// only enable if user has any undos left
return ({
board: newBoard,
userPieces: userPieces.every(p => p == null)
? getNewPieces(queue, undoRef)
: userPieces,
selectedPieceIndex: null,
score: prevState.score + rowsAndColsCleared * boardSize,
highscore: prevState.highscore
})
})
undoRef.current.disabled = undosLeft <= 0
setPrevState(state)
setQueue(null)
// if (shouldSucceed !== fit) {
// prompt("red test", newTest)
// }
}, [boardWithPreview, fit, queue, state, undoRef, undosLeft])
usePointerExit(pointerUpHandler)
return (
<div className="app"
onPointerMove={e =>
{
if (state.selectedPieceIndex == null ) return
setPointer(p => ({
...p,
pos: [
e.pageX,
e.pageY
]
}))
}
}
onPointerUp={pointerUpHandler}
>
<div className="app-wrapper">
<header className="header">
<h1>Klods</h1>
<div className="options">
<div className="button">Score: {state.score}</div>
<div className="button">High Score: {state.highscore}</div>
<button onClick={resetGame}>Reset game</button>
<button ref={undoRef} disabled={undosLeft <= 0} className='undo' onClick={undo}>
Undo ({undosLeft})
</button>
</div>
</header>
<main className="game">
<div className="board-wrapper">
<div
className="board"
ref={boardRef}
>
{boardWithPreview.map((square, i) =>
{
const [colIndex, rowIndex] = calculateLocationFromIndex(i, boardSize)
const shouldBlink = rowsToClear.includes(rowIndex) || colsToClear.includes(colIndex)
return <Square key={i} square={square} blink={shouldBlink} hasTransition={gameOver} />
}
)}
</div>
</div>
{gameOver ? (
<div>
<button onClick={resetGame}>New game</button>
</div>
) : (
<div className="user-pieces">
{state.userPieces.map((piece, pieceIndex) => {
if (piece === null) {
return <div key={pieceIndex} />
}
return (
<div
className="piece"
key={pieceIndex}
onPointerDown={(e) => {
// Prevent drag behaviour
e.preventDefault()
if (gameOver) return
setPrevState(state)
// Select the piece that was dragged from
setState((prevState) => ({
...prevState,
selectedPieceIndex: pieceIndex,
}))
setPointer({
offset: [e.pageX, e.pageY],
pos: [e.pageX, e.pageY],
type: e.pointerType,
})
}}
>
<div
className={state.selectedPieceIndex === pieceIndex
? "piece-grid selected"
: "piece-grid"
}
style={{
gridTemplateColumns: `repeat(${piece[0].length}, 1fr)`,
gridTemplateRows: `repeat(${piece.length}, 1fr)`,
...(state.selectedPieceIndex === pieceIndex
? {
"--xt": `calc(${pointer.pos[0]}px - ${
getOffsetFromPiece(piece, pointer.type)[0]
} * var(--square-size))`,
"--yt": `calc(${pointer.pos[1]}px - ${
getOffsetFromPiece(piece, pointer.type)[1]
} * var(--square-size))`,
opacity: fit ? 0.3 : 1,
}
: {}),
}}
>
{piece.map((rows, j) => (
<>
{rows.map((fill, k) => (
<Square
key={`${j},${k}`}
square={
fill === 1
? pieceCanBePlacedValues[pieceIndex]
? { hue: 100 }
: { hue: 0 }
: null
}
hasTransition={true}
/>
))}
</>
))}
</div>
</div>
)
})}
</div>
)}
</main>
</div>
</div>
)
}
function tryGetPieceFromState(state: IState): [IPiece, number] | [null, null] {
const selectedPieceIndex = state.selectedPieceIndex
if(selectedPieceIndex === null) return [null, null]
const selectedPiece = state.userPieces[selectedPieceIndex]
if(!selectedPiece) return [null, null]
return [selectedPiece, selectedPieceIndex]
}
const getOffsetFromPiece: (p: IPiece, pointerType: string) => [number, number] = (p, pt) => ([
getPieceWidth(p) / 2,
getPieceHeight(p) + (pt === "mouse" ? 0 : 5)
])
const getOffsetFromPieceInPixels: (p: IPiece, pointerType: string) => [number, number] = (p, pt) =>
getOffsetFromPiece(p, pt).map(v => v * getSquareSizePixels()) as [number, number]
const getInitialState: () => IState = () => ({
highscore: Number(window.localStorage.getItem(highscoreLocalStorageKey)) ?? 0,
board: createEmptyBoard(boardSize),
userPieces: getNewPieces(null, null),
selectedPieceIndex: null,
score: 0
})
const getNewPieces = (queue: (IPiece | null)[] | null, undoRef: any) => {
if(queue !== null){
return queue
} else{
// disable undo when new round has been seen
undoRef && (undoRef.current.disabled = true)
return drawN(pieces, 3)
}
}