11using Fusion ;
22using UnityEngine ;
33using UISystem ;
4- using System . Collections . Generic ;
4+ using BuildingPlacement . Buildings ;
5+ using VehicleSystem . Vehicles ;
56
67namespace GameStateSystem
78{
@@ -10,16 +11,12 @@ public class GameStateManager : NetworkBehaviour
1011 public static GameStateManager Instance ;
1112
1213 [ Networked ] public int WinningTeamId { get ; set ; } = - 1 ;
13-
1414 public int LocalPlayerTeamId = 0 ;
1515
16- private ChangeDetector _changes ;
17-
18-
19- [ Header ( "Additional" ) ]
20- public Dictionary < string , int > additions = new Dictionary < string , int > ( ) ;
21- public int totalScore = 0 ;
16+ [ Networked , Capacity ( 32 ) ]
17+ private NetworkDictionary < NetworkString < _32 > , int > UnitCounts { get ; }
2218
19+ private ChangeDetector _changes ;
2320 private DeploymentMonitorHUDController deploymentMonitorHUDController ;
2421
2522 private void Awake ( )
@@ -40,7 +37,10 @@ public override void Spawned()
4037 if ( Object . HasStateAuthority )
4138 {
4239 WinningTeamId = - 1 ;
40+ UnitCounts . Clear ( ) ;
4341 }
42+
43+ UpdateAllUI ( ) ;
4444 }
4545
4646 public override void Render ( )
@@ -51,6 +51,11 @@ public override void Render()
5151 {
5252 HandleGameOverUI ( ) ;
5353 }
54+
55+ if ( change == nameof ( UnitCounts ) )
56+ {
57+ UpdateAllUI ( ) ;
58+ }
5459 }
5560 }
5661
@@ -77,92 +82,100 @@ private void RPC_ReportLoss(int losingTeamId)
7782 private void HandleGameOverUI ( )
7883 {
7984 if ( WinningTeamId == - 1 ) return ;
80-
8185 var hud = FindAnyObjectByType < GameStatusHUDController > ( ) ;
8286 if ( hud == null ) return ;
8387
84- //Debug.Log($"Oyun Bitti! Kazanan: {WinningTeamId}, Benim Takımım: {LocalPlayerTeamId}");
88+ if ( WinningTeamId == LocalPlayerTeamId ) hud . ShowVictoryPanel ( ) ;
89+ else hud . ShowGameOverPanel ( ) ;
90+ }
8591
86- if ( WinningTeamId == LocalPlayerTeamId )
92+ public void ReportUnitConstructed ( Unit . Unit unit )
93+ {
94+ if ( Object . HasStateAuthority )
8795 {
88- hud . ShowVictoryPanel ( ) ;
96+ ModifyUnitCount ( unit , 1 ) ;
8997 }
9098 else
9199 {
92- hud . ShowGameOverPanel ( ) ;
100+ int maxCap = ( unit is Building ) ? ( ( Building ) unit ) . buildingData . maxCreatableCount : ( unit is Vehicle ) ? ( ( Vehicle ) unit ) . vehicleData . maxCreatableCount : 0 ;
101+ RPC_ModifyUnitCount ( unit . teamId , GetUnitName ( unit ) , maxCap , 1 ) ;
93102 }
94103 }
95104
96- public void ReportUnitConstructed ( Unit . Unit unit )
105+ public void ReportUnitDestroyed ( Unit . Unit unit )
97106 {
98- if ( deploymentMonitorHUDController != null )
107+ if ( Object . HasStateAuthority )
99108 {
100- if ( unit . teamId == LocalPlayerTeamId )
101- {
102- string unitName = "" ;
103- int currentAddition = 0 ;
104- int maxCapacity = 0 ;
105-
106- if ( unit is BuildingPlacement . Buildings . Building building )
107- {
108- unitName = building . buildingData . buildingName ;
109- currentAddition = additions . ContainsKey ( unitName ) ? additions [ unitName ] + 1 : 1 ;
110- maxCapacity = building . buildingData . maxCreatableCount ;
111- }
112- else if ( unit is VehicleSystem . Vehicles . Vehicle vehicle )
113- {
114- unitName = vehicle . vehicleData . vehicleName ;
115- currentAddition = additions . ContainsKey ( unitName ) ? additions [ unitName ] + 1 : 1 ;
116- maxCapacity = vehicle . vehicleData . maxCreatableCount ;
117- }
118-
119- UpdateAdditions ( unitName , currentAddition ) ;
120- deploymentMonitorHUDController . UpdateUnitSlots ( LocalPlayerTeamId , unitName , additions [ unitName ] , maxCapacity ) ;
121- }
109+ ModifyUnitCount ( unit , - 1 ) ;
110+ }
111+ else
112+ {
113+ RPC_ModifyUnitCount ( unit . teamId , GetUnitName ( unit ) , GetMaxCapacity ( unit ) , - 1 ) ;
122114 }
123115 }
124116
125- public void ReportUnitDestroyed ( Unit . Unit unit )
117+ [ Rpc ( RpcSources . All , RpcTargets . StateAuthority ) ]
118+ private void RPC_ModifyUnitCount ( int teamId , string unitName , int maxCapacity , int changeAmount )
126119 {
127- if ( deploymentMonitorHUDController != null )
120+ string key = $ "{ teamId } _{ unitName } ";
121+
122+ int current = 0 ;
123+ if ( UnitCounts . ContainsKey ( key ) )
128124 {
129- if ( unit . teamId == LocalPlayerTeamId )
130- {
131- string unitName = "" ;
132- int currentAddition = 0 ;
133- int maxCapacity = 0 ;
134-
135- if ( unit is BuildingPlacement . Buildings . Building building )
136- {
137- unitName = building . buildingData . buildingName ;
138- currentAddition = additions . ContainsKey ( unitName ) ? additions [ unitName ] - 1 : 0 ;
139- if ( currentAddition < 0 ) currentAddition = 0 ;
140- maxCapacity = building . buildingData . maxCreatableCount ;
141- }
142- else if ( unit is VehicleSystem . Vehicles . Vehicle vehicle )
143- {
144- unitName = vehicle . vehicleData . vehicleName ;
145- currentAddition = additions . ContainsKey ( unitName ) ? additions [ unitName ] - 1 : 0 ;
146- if ( currentAddition < 0 ) currentAddition = 0 ;
147- maxCapacity = vehicle . vehicleData . maxCreatableCount ;
148- }
149-
150- UpdateAdditions ( unitName , currentAddition ) ;
151- deploymentMonitorHUDController . UpdateUnitSlots ( LocalPlayerTeamId , unitName , additions [ unitName ] , maxCapacity ) ;
152- }
125+ current = UnitCounts [ key ] ;
153126 }
127+
128+ int newValue = Mathf . Clamp ( current + changeAmount , 0 , maxCapacity ) ;
129+ UnitCounts . Set ( key , newValue ) ;
154130 }
155131
156- private void UpdateAdditions ( string key , int value )
132+ private void ModifyUnitCount ( Unit . Unit unit , int amount )
157133 {
158- if ( additions . ContainsKey ( key ) )
134+ string unitName = GetUnitName ( unit ) ;
135+ string key = $ "{ unit . teamId } _{ unitName } ";
136+
137+ int current = 0 ;
138+ if ( UnitCounts . ContainsKey ( key ) )
159139 {
160- additions [ key ] = value ;
140+ current = UnitCounts [ key ] ;
161141 }
162- else
142+
143+ int newValue = Mathf . Clamp ( current + amount , 0 , GetMaxCapacity ( unit ) ) ;
144+ UnitCounts . Set ( key , newValue ) ;
145+ }
146+
147+ private void UpdateAllUI ( )
148+ {
149+ if ( deploymentMonitorHUDController == null ) return ;
150+
151+ foreach ( var kvp in UnitCounts )
163152 {
164- additions . Add ( key , value ) ;
153+ string [ ] parts = kvp . Key . ToString ( ) . Split ( '_' ) ;
154+ if ( parts . Length < 2 ) continue ;
155+
156+ if ( int . TryParse ( parts [ 0 ] , out int teamId ) )
157+ {
158+ string unitName = parts [ 1 ] ;
159+ int count = kvp . Value ;
160+ int maxCap = 10 ; // Maksimum kapasiteyi bilmediğimiz için 10 olarak başlatıyoruz
161+
162+ deploymentMonitorHUDController . UpdateUnitSlots ( teamId , unitName , count , maxCap ) ;
163+ }
165164 }
166165 }
166+
167+ private string GetUnitName ( Unit . Unit unit )
168+ {
169+ if ( unit is Building b ) return b . buildingData . buildingName ;
170+ if ( unit is Vehicle v ) return v . vehicleData . vehicleName ;
171+ return "Unknown" ;
172+ }
173+
174+ private int GetMaxCapacity ( Unit . Unit unit )
175+ {
176+ if ( unit is Building b ) return b . buildingData . maxCreatableCount ;
177+ if ( unit is Vehicle v ) return v . vehicleData . maxCreatableCount ;
178+ return 0 ;
179+ }
167180 }
168181}
0 commit comments