11import React , { useState , useCallback , useRef , useEffect } from 'react' ;
2- import { Stage , Layer , Image as KonvaImage , Text } from 'react-konva' ;
2+ import { Stage , Layer , Image as KonvaImage } from 'react-konva' ;
33import Player from './Player' ;
44import Enemy from './Enemy' ;
55import Projectiles from './game/Projectiles' ;
66import GameOver from './ui/GameOver' ;
77import Score from './ui/Score' ;
88import LevelUp from './ui/LevelUp' ;
99import { useImage } from 'react-konva-utils' ;
10- import { STAGE_WIDTH , STAGE_HEIGHT , ATTACK_RATE , PLAYER_SPEED , PROJECTILE_DAMAGE , DEFAULT_PIERCE , PLAYER_INITIAL_HEALTH } from '../game/constants' ;
10+ import { STAGE_WIDTH , STAGE_HEIGHT , ATTACK_RATE , PROJECTILE_DAMAGE } from '../game/constants' ;
1111
1212// Import our custom hooks
1313import { useKeyboardControls } from './game/useKeyboardControls' ;
@@ -31,13 +31,6 @@ const Game: React.FC = () => {
3131 // Game state
3232 const [ isPaused , setIsPaused ] = useState ( false ) ;
3333
34- // Game configuration (can be modified by level-ups)
35- const gameConfig = useRef ( {
36- attackRate : ATTACK_RATE ,
37- projectileDamage : PROJECTILE_DAMAGE ,
38- pierce : DEFAULT_PIERCE
39- } ) ;
40-
4134 // Set up input handling
4235 const keys = useKeyboardControls ( ) ;
4336
@@ -50,96 +43,55 @@ const Game: React.FC = () => {
5043 damagePlayer,
5144 isGameOver,
5245 resetGame,
53- increaseSpeed,
54- increaseMaxHealth,
55- currentMaxHealth
46+ upgradeSpeed,
47+ currentSpeed
5648 } = usePlayerSystem ( keys ) ;
5749
58- // Set up enemy system
59- const {
60- enemies,
61- updateEnemies,
62- damageEnemy,
63- score
64- } = useEnemySystem ( playerPosRef , isGameOver , isPaused ) ;
65-
66- // Set up projectile system with refs to dynamic config values
50+ // Set up projectile system - note: we need to check if isPaused is a valid parameter
6751 const {
6852 projectiles,
6953 updateProjectiles,
7054 fireProjectile,
7155 handleProjectileHit,
72- updateAttackRate,
73- setCurrentPierce,
74- currentAttackRate,
75- currentPierce
76- } = useProjectileSystem ( playerPosRef , isGameOver , isPaused ) ;
56+ resetProjectileSystem
57+ } = useProjectileSystem ( playerPosRef , isGameOver ) ;
7758
78- // Update projectile system when game config changes
79- useEffect ( ( ) => {
80- if ( gameConfig . current . attackRate !== currentAttackRate ) {
81- updateAttackRate ( gameConfig . current . attackRate ) ;
82- }
83-
84- if ( gameConfig . current . pierce !== currentPierce ) {
85- setCurrentPierce ( gameConfig . current . pierce ) ;
86- }
87- } , [ gameConfig . current , updateAttackRate , setCurrentPierce , currentAttackRate , currentPierce ] ) ;
59+ // Callbacks for upgrading game stats
60+ const upgradeAttackSpeed = useCallback ( ( ) => {
61+ console . log ( 'Upgrading attack speed' ) ;
62+ // In a real implementation, we would modify an attack speed value
63+ } , [ ] ) ;
8864
89- // Handle applying upgrades to the game
90- const applyUpgrade = useCallback ( ( type : string , value : number ) => {
91- switch ( type ) {
92- case 'attackSpeed' :
93- gameConfig . current = {
94- ...gameConfig . current ,
95- attackRate : Math . max ( 50 , gameConfig . current . attackRate - value )
96- } ;
97- updateAttackRate ( gameConfig . current . attackRate ) ;
98- console . log ( `Attack speed increased! New rate: ${ gameConfig . current . attackRate } ms` ) ;
99- break ;
100- case 'moveSpeed' :
101- increaseSpeed ( value ) ;
102- console . log ( `Movement speed increased!` ) ;
103- break ;
104- case 'projectileDamage' :
105- gameConfig . current = {
106- ...gameConfig . current ,
107- projectileDamage : gameConfig . current . projectileDamage + value
108- } ;
109- console . log ( `Damage increased! New damage: ${ gameConfig . current . projectileDamage } ` ) ;
110- break ;
111- case 'pierce' :
112- gameConfig . current = {
113- ...gameConfig . current ,
114- pierce : gameConfig . current . pierce + value
115- } ;
116- setCurrentPierce ( gameConfig . current . pierce ) ;
117- console . log ( `Pierce increased! New pierce: ${ gameConfig . current . pierce } ` ) ;
118- break ;
119- case 'health' :
120- increaseMaxHealth ( value ) ;
121- console . log ( `Max health increased! New max health: ${ currentMaxHealth + value } ` ) ;
122- break ;
123- default :
124- console . warn ( `Unknown upgrade type: ${ type } ` ) ;
125- }
126-
127- // Resume the game after applying the upgrade
128- setIsPaused ( false ) ;
129- } , [ increaseSpeed , increaseMaxHealth , currentMaxHealth , updateAttackRate , setCurrentPierce ] ) ;
65+ // Set up enemy system - note: we need to check if isPaused is a valid parameter
66+ const {
67+ enemies,
68+ updateEnemies,
69+ damageEnemy,
70+ score
71+ } = useEnemySystem ( playerPosRef , isGameOver ) ;
13072
13173 // Set up level system
13274 const {
13375 level,
134- isLevelingUp,
135- levelUpChoices,
136- selectUpgrade
137- } = useLevelSystem ( score , applyUpgrade ) ;
76+ showLevelUp,
77+ upgrades,
78+ handleSelectUpgrade
79+ } = useLevelSystem ( {
80+ score, // Use the actual score from the enemy system
81+ onUpgradeAttackSpeed : upgradeAttackSpeed ,
82+ onUpgradePlayerSpeed : upgradeSpeed
83+ } ) ;
13884
139- // Pause the game when leveling up
85+ // Update level system with the current score
14086 useEffect ( ( ) => {
141- setIsPaused ( isLevelingUp ) ;
142- } , [ isLevelingUp ] ) ;
87+ // We can't directly update the score in the level system,
88+ // so we'll need to monitor showLevelUp and pause when needed
89+ if ( showLevelUp ) {
90+ setIsPaused ( true ) ;
91+ } else {
92+ setIsPaused ( false ) ;
93+ }
94+ } , [ showLevelUp ] ) ;
14395
14496 // Set up collision detection
14597 const { checkCollisions } = useCollisionSystem ( {
@@ -150,31 +102,22 @@ const Game: React.FC = () => {
150102 damagePlayer,
151103 damageEnemy,
152104 handleProjectileHit,
153- projectileDamage : gameConfig . current . projectileDamage
105+ projectileDamage : PROJECTILE_DAMAGE
154106 } ) ;
155107
156- // Create a reference to the current game config for the collision system
157- const gameConfigRef = useRef ( gameConfig . current ) ;
158-
159- // Update the game config reference when it changes
160- useEffect ( ( ) => {
161- gameConfigRef . current = gameConfig . current ;
162- } , [ gameConfig . current ] ) ;
163-
164108 // Define update functions based on pause state
165109 const updateFunctions = isPaused
166110 ? [ ] // Empty array when paused - no updates
167111 : [
168112 updatePosition ,
169113 updateEnemies ,
170114 updateProjectiles ,
171- // Use a wrapper function to ensure we always use the latest game config
172- ( ) => checkCollisions ( )
115+ checkCollisions
173116 ] ;
174117
175118 // Set up the game loop
176119 useGameLoop ( updateFunctions ) ;
177-
120+
178121 return (
179122 < div className = "game-container" >
180123 < Stage width = { STAGE_WIDTH } height = { STAGE_HEIGHT } >
@@ -209,12 +152,12 @@ const Game: React.FC = () => {
209152 < Projectiles projectiles = { projectiles } />
210153
211154 { /* UI Elements */ }
212- < Score score = { score } level = { level } />
155+ < Score score = { score } />
213156 < GameOver visible = { isGameOver } />
214157 < LevelUp
215- visible = { isLevelingUp }
216- choices = { levelUpChoices }
217- onSelect = { selectUpgrade }
158+ visible = { showLevelUp }
159+ upgrades = { upgrades }
160+ onSelect = { handleSelectUpgrade }
218161 />
219162 </ Layer >
220163 </ Stage >
0 commit comments