11import { CameraControls , Stats } from '@react-three/drei'
22import { Canvas } from '@react-three/fiber'
33import MouseButtons from 'camera-controls'
4- import { useEffect , useRef } from 'react'
4+ import { useCallback , useEffect } from 'react'
55import { Vector3 } from 'three'
66
77import { useGameStore } from '../../stores/stacker/gameStore'
88import { Tile } from './Tile'
99
1010export 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