@@ -15,6 +15,7 @@ import {
1515 MIN_GAME_WIDTH ,
1616 MIN_GAME_HEIGHT ,
1717 RESIZE_THRESHOLD ,
18+ RE_RESIZE_THRESHOLD ,
1819} from './constants' ;
1920
2021// Game Scenes
@@ -39,7 +40,7 @@ import GameText from './components/GameText';
3940import { selectDialogMessages } from './redux/selectors/selectDialog' ;
4041import { selectMenuItems } from './redux/selectors/selectMenu' ;
4142import { selectTexts } from './redux/selectors/selectText' ;
42- import { selectGameLocale } from './redux/selectors/selectGameData' ;
43+ import { selectGameCameraSizeUpdateCallback , selectGameLocale } from './redux/selectors/selectGameData' ;
4344
4445const Game = ( ) => {
4546 const isDevelopment = process ?. env ?. NODE_ENV !== 'production' ;
@@ -49,6 +50,7 @@ const Game = () => {
4950 const menuItems = useSelector ( selectMenuItems ) ;
5051 const gameTexts = useSelector ( selectTexts ) ;
5152 const locale = useSelector ( selectGameLocale ) ;
53+ const cameraSizeUpdateCallback = useSelector ( selectGameCameraSizeUpdateCallback ) ;
5254
5355 const [ messages , setMessages ] = useState ( { } ) ;
5456
@@ -127,40 +129,73 @@ const Game = () => {
127129 } ) ;
128130
129131 updateGameReduxState ( width , height , zoom ) ;
132+ setGame ( phaserGame ) ;
133+
134+ if ( isDevelopment ) {
135+ window . phaserGame = phaserGame ;
136+ }
137+ } , [
138+ game ,
139+ dispatch ,
140+ isDevelopment ,
141+ updateGameReduxState ,
142+ ] ) ;
143+
144+ useEffect ( ( ) => {
145+ if ( ! game ) {
146+ return ( ) => { } ;
147+ }
148+
149+ if ( game . canvas ) {
150+ dispatch ( setGameCanvasElementAction ( game . canvas ) ) ;
151+ }
130152
131153 // Create listener to resize the game
132154 // when the window is resized
133155 let timeOutFunctionId ;
134156 const workAfterResizeIsDone = ( ) => {
135- const gameSize = calculateGameSize (
136- MIN_GAME_WIDTH ,
137- MIN_GAME_HEIGHT ,
138- TILE_WIDTH ,
139- TILE_HEIGHT
140- ) ;
141-
142- // TODO needs to re-run this function to: handleConfigureCamera
143- phaserGame . scale . setZoom ( gameSize . zoom ) ;
144- phaserGame . scale . resize ( gameSize . width , gameSize . height ) ;
145- updateGameReduxState ( gameSize . width , gameSize . height , gameSize . zoom ) ;
157+ const scaleGame = ( ) => {
158+ const gameSize = calculateGameSize (
159+ MIN_GAME_WIDTH ,
160+ MIN_GAME_HEIGHT ,
161+ TILE_WIDTH ,
162+ TILE_HEIGHT
163+ ) ;
164+
165+ // console.log(JSON.stringify(gameSize));
166+ game . scale . setZoom ( gameSize . zoom ) ;
167+ game . scale . resize ( gameSize . width , gameSize . height ) ;
168+ // game.scale.setGameSize(gameSize.width, gameSize.height);
169+ // game.scale.displaySize.resize(gameSize.width, gameSize.height);
170+ // game.scale.resize(gameSize.width, gameSize.height).getParentBounds();
171+ updateGameReduxState ( gameSize . width , gameSize . height , gameSize . zoom ) ;
172+ // game.canvas.style.width = `${gameSize.width}px`;
173+ // game.canvas.style.height = `${gameSize.height}px`;
174+ cameraSizeUpdateCallback ?. ( ) ;
175+ } ;
176+
177+ scaleGame ( ) ;
178+
179+ // re-run function after resize is done to re-trigger css calculations
180+ setTimeout ( scaleGame , RE_RESIZE_THRESHOLD ) ;
146181 } ;
147182
148- // TODO move to the ResizeObserver https://jsfiddle.net/rudiedirkx/p0ckdcnv/
149- window . addEventListener ( 'resize' , ( ) => {
183+ const canvasResizeCallback = ( ) => {
150184 clearTimeout ( timeOutFunctionId ) ;
151185 timeOutFunctionId = setTimeout ( workAfterResizeIsDone , RESIZE_THRESHOLD ) ;
152- } ) ;
186+ } ;
153187
154- setGame ( phaserGame ) ;
155- dispatch ( setGameCanvasElementAction ( phaserGame . canvas ) ) ;
156- if ( isDevelopment ) {
157- window . phaserGame = phaserGame ;
158- }
188+ // TODO move to the ResizeObserver https://jsfiddle.net/rudiedirkx/p0ckdcnv/
189+ window . addEventListener ( 'resize' , canvasResizeCallback ) ;
190+
191+ return ( ) => {
192+ window . removeEventListener ( 'resize' , canvasResizeCallback ) ;
193+ } ;
159194 } , [
160195 game ,
161196 dispatch ,
162- isDevelopment ,
163197 updateGameReduxState ,
198+ cameraSizeUpdateCallback ,
164199 ] ) ;
165200
166201 return (
0 commit comments