Skip to content

Commit 11e1f71

Browse files
author
David Liu
committed
fix camera mount
1 parent 59a2c02 commit 11e1f71

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

web/src/components/stacker/StackerGame.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { CameraControls, Stats } from '@react-three/drei'
22
import { Canvas } from '@react-three/fiber'
33
import MouseButtons from 'camera-controls'
4-
import { useEffect, useRef } from 'react'
4+
import { useCallback, useEffect } from 'react'
55
import { Vector3 } from 'three'
66

77
import { useGameStore } from '../../stores/stacker/gameStore'
88
import { Tile } from './Tile'
99

1010
export function StackerGame() {
11-
const cameraControlsRef = useRef<CameraControls>(null)
1211
const initializeBoard = useGameStore((state) => state.initializeBoard)
1312
const board = useGameStore((state) => state.board)
1413
const boardSize = useGameStore((state) => state.boardSize)
@@ -24,20 +23,22 @@ export function StackerGame() {
2423
const centerPoint = (boardSize - 1) / 2.0
2524
const distance = boardSize * 2
2625

27-
// Set camera position once on mount
28-
useEffect(() => {
29-
if (cameraControlsRef.current) {
30-
cameraControlsRef.current.setLookAt(
31-
centerPoint + distance,
32-
distance,
33-
centerPoint + distance,
34-
centerPoint,
35-
0,
36-
centerPoint,
37-
true,
38-
)
39-
}
40-
}, []) // Empty dependency array - only run once on mount
26+
const cameraControlsRef = useCallback(
27+
(controls: CameraControls | null) => {
28+
if (controls && board.length > 0) {
29+
controls.setLookAt(
30+
centerPoint + distance,
31+
distance,
32+
centerPoint + distance,
33+
centerPoint,
34+
0,
35+
centerPoint,
36+
true,
37+
)
38+
}
39+
},
40+
[board.length, centerPoint, distance],
41+
)
4142

4243
const getPlayerName = (player: number) => {
4344
return player === 1 ? 'Red' : 'Blue'

0 commit comments

Comments
 (0)