Skip to content

Commit 775a268

Browse files
committed
Refactor unit count tracking to use NetworkDictionary
Replaced the local additions dictionary with a networked UnitCounts dictionary for synchronized unit tracking across clients. Added methods for reporting unit construction and destruction, updating UI accordingly, and improved RPC handling for unit count changes. Also updated _SunDirection in atmosphere, cloud, and fog material files.
1 parent 33b2531 commit 775a268

File tree

4 files changed

+84
-71
lines changed

4 files changed

+84
-71
lines changed
Lines changed: 81 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Fusion;
22
using UnityEngine;
33
using UISystem;
4-
using System.Collections.Generic;
4+
using BuildingPlacement.Buildings;
5+
using VehicleSystem.Vehicles;
56

67
namespace 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
}

Red Strike/Assets/PlanetAtmosphereSystem/AtmosphereMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Material:
4747
- _GunesYonu: {r: -0.91, g: 0.4, b: 3.24, a: 0}
4848
- _RenkUzak: {r: 0, g: 0.10166844, b: 2.152374, a: 1}
4949
- _RenkYakin: {r: 0, g: 5.670685, b: 8, a: 1}
50-
- _SunDirection: {r: 0.17330775, g: 0.062598675, b: -0.9828763, a: 0}
50+
- _SunDirection: {r: 0.053725094, g: 0.9509352, b: -0.30468985, a: 0}
5151
m_BuildTextureStacks: []
5252
m_AllowLocking: 1
5353
--- !u!114 &1522106816477559216

Red Strike/Assets/PlanetAtmosphereSystem/CloudMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Material:
4343
m_Colors:
4444
- _Color: {r: 0, g: 3.8274493, b: 5.992155, a: 1}
4545
- _GunesYonu: {r: -1, g: 0, b: 0, a: 0}
46-
- _SunDirection: {r: 0.17330775, g: 0.062598675, b: -0.9828763, a: 0}
46+
- _SunDirection: {r: 0.053725094, g: 0.9509352, b: -0.30468985, a: 0}
4747
m_BuildTextureStacks: []
4848
m_AllowLocking: 1
4949
--- !u!114 &8499732345945835446

Red Strike/Assets/PlanetAtmosphereSystem/FogMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ Material:
5454
- _QueueControl: 0
5555
- _QueueOffset: 0
5656
m_Colors:
57-
- _SunDirection: {r: 0.17330775, g: 0.062598675, b: -0.9828763, a: 0}
57+
- _SunDirection: {r: 0.053725094, g: 0.9509352, b: -0.30468985, a: 0}
5858
m_BuildTextureStacks: []
5959
m_AllowLocking: 1

0 commit comments

Comments
 (0)