Skip to content

Commit fa7bd77

Browse files
committed
Implement unit build limits and UI improvements
Added logic to enforce maximum unit and building limits per team using GameStateManager, and integrated these checks into building and vehicle creation flows. Updated HUD controllers and UI templates to use progress bars for health and fuel, improved vehicle status reporting, and refactored code for clarity and maintainability. Also adjusted outline widths in prefabs and updated material sun direction vectors.
1 parent 51e231b commit fa7bd77

File tree

22 files changed

+320
-282
lines changed

22 files changed

+320
-282
lines changed

Red Strike/Assets/BuildingPlacement/Buildings/Building.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ public class Building : Unit.Unit
88
{
99
public BuildingPlacement.Building buildingData;
1010

11-
[Networked] protected float health { get; set; }
12-
public float Health { get { return health; } }
13-
14-
public string BuildingName => buildingData != null ? buildingData.name : gameObject.name;
11+
[Networked] public float health { get; set; }
12+
public float maxHealth;
13+
public string buildingName;
1514

1615
public override void Spawned()
1716
{
1817
if (Object.HasStateAuthority)
1918
{
2019
health = buildingData.maxHealth;
20+
maxHealth = buildingData.maxHealth;
21+
buildingName = buildingData.buildingName;
2122
}
2223
}
23-
24+
2425
public override void TakeDamage(float damage)
2526
{
2627
if (!Object.HasStateAuthority) return;

Red Strike/Assets/BuildingPlacement/Buildings/EnergyTower/EnergyTower.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,42 @@ namespace BuildingPlacement.Buildings
77
public class EnergyTower : Building
88
{
99
[Header("Energy Tower Settings")]
10-
public float maxCapacity = 500f;
11-
public float currentCapacity = 0f;
12-
public int maxDensity = 3;
10+
public float maxFuelCapacity = 500f;
11+
public float currentFuelCapacity = 0f;
12+
public int maxDensityCapacity = 3;
13+
public int currentDensity = 3;
1314
public float rechargeRate = 10f;
1415
public bool isActive = true;
1516

1617
private List<Vehicle> connectedVehicles = new List<Vehicle>();
1718

1819
private void Start()
1920
{
20-
currentCapacity = maxCapacity;
21+
currentFuelCapacity = maxFuelCapacity;
2122
}
2223

2324
private void Update()
2425
{
2526
if (!isActive) return;
2627

27-
if (currentCapacity < maxCapacity)
28+
if (currentFuelCapacity < maxFuelCapacity)
2829
{
29-
currentCapacity += rechargeRate * Time.deltaTime;
30-
if (currentCapacity > maxCapacity) currentCapacity = maxCapacity;
30+
currentFuelCapacity += rechargeRate * Time.deltaTime;
31+
if (currentFuelCapacity > maxFuelCapacity) currentFuelCapacity = maxFuelCapacity;
3132
}
3233
}
3334

3435
public bool IsAvailable()
3536
{
36-
return connectedVehicles.Count < maxDensity && isActive && currentCapacity > 5f;
37+
return connectedVehicles.Count < maxDensityCapacity && isActive && currentFuelCapacity > 5f;
3738
}
3839

3940
public float GiveEnergy(float requestedAmount)
4041
{
41-
if (!isActive || currentCapacity <= 0) return 0f;
42+
if (!isActive || currentFuelCapacity <= 0) return 0f;
4243

43-
float amountToGive = Mathf.Min(requestedAmount, currentCapacity);
44-
currentCapacity -= amountToGive;
44+
float amountToGive = Mathf.Min(requestedAmount, currentFuelCapacity);
45+
currentFuelCapacity -= amountToGive;
4546

4647
return amountToGive;
4748
}
@@ -53,7 +54,7 @@ public bool NewVehicleConnected(Vehicle vehicle)
5354
return true;
5455
}
5556

56-
if (connectedVehicles.Count < maxDensity)
57+
if (connectedVehicles.Count < maxDensityCapacity)
5758
{
5859
connectedVehicles.Add(vehicle);
5960
return true;
@@ -70,9 +71,11 @@ public void VehicleDisconnected(Vehicle vehicle)
7071
}
7172
}
7273

73-
public (float current, float max, int count, int limit) GetStatus()
74+
public (float currentFuelCapacity, float maxFuelCapacity,
75+
int connectedVehicles, int maxDensityCapacity
76+
) GetStatus()
7477
{
75-
return (currentCapacity, maxCapacity, connectedVehicles.Count, maxDensity);
78+
return (currentFuelCapacity, maxFuelCapacity, connectedVehicles.Count, maxDensityCapacity);
7679
}
7780
}
7881
}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using UnityEngine;
22
using VehicleSystem;
33
using NetworkingSystem;
4+
using GameStateSystem;
5+
using System.Linq;
46

57
namespace BuildingPlacement.Buildings
68
{
79
public class Hangar : Building
810
{
9-
// TEST
1011
public bool IsReady = true;
1112
public string InProductionUnitName = "None";
1213

1314
private Vector3 vehicleSpawnPoint;
14-
1515
public VehiclesDatabase vehiclesDatabase;
1616

1717
private AudioSource audioSource;
@@ -21,25 +21,42 @@ public class Hangar : Building
2121
private void Start()
2222
{
2323
audioSource = GetComponent<AudioSource>();
24-
2524
vehicleSpawnPoint = transform.position + transform.forward * 20f;
2625
vehicleSpawnPoint.y += 2f;
2726
}
2827

2928
public void CreateVehicle(VehicleTypes vehicleType)
3029
{
30+
var targetVehicleData = vehiclesDatabase.vehicles.FirstOrDefault(v => v.vehicleType == vehicleType);
31+
32+
if (targetVehicleData == null)
33+
{
34+
//Debug.LogError("Araç database'de bulunamadı!");
35+
return;
36+
}
37+
38+
bool limitReached = GameStateManager.Instance.HasReachedLimit(
39+
teamId,
40+
targetVehicleData.vehicleName,
41+
targetVehicleData.maxCreatableCount
42+
);
43+
44+
if (limitReached)
45+
{
46+
//Debug.LogWarning("Araç limitine ulaşıldı!");
47+
audioSource.PlayOneShot(errorSound);
48+
return;
49+
}
50+
3151
if (CanCreateVehicle())
3252
{
3353
InProductionUnitName = vehicleType.ToString();
3454
IsReady = false;
35-
36-
//Debug.Log($"Hangar started creating a new vehicle: {InProductionUnitName}");
3755
audioSource.PlayOneShot(vehicleCreateSound);
3856
CreateVehicleInstance(vehicleType);
3957
}
4058
else
4159
{
42-
//Debug.LogWarning("Hangar is not ready to create a new vehicle or already in production.");
4360
audioSource.PlayOneShot(errorSound);
4461
}
4562
}
@@ -51,13 +68,15 @@ private void CreateVehicleInstance(VehicleTypes vehicleType)
5168
if (item.vehicleType == vehicleType)
5269
{
5370
CommanderData.LocalCommander.RPC_SpawnVehicle(item.vehicleName, vehicleSpawnPoint);
71+
5472
InProductionUnitName = "None";
5573
IsReady = true;
74+
5675
return;
5776
}
5877
}
5978
}
6079

6180
public bool CanCreateVehicle() => IsReady && InProductionUnitName == "None";
6281
}
63-
}
82+
}

Red Strike/Assets/GameStateSystem/GameStateManager.cs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UISystem;
44
using BuildingPlacement.Buildings;
55
using VehicleSystem.Vehicles;
6+
using System.Collections.Generic;
67

78
namespace GameStateSystem
89
{
@@ -13,7 +14,7 @@ public class GameStateManager : NetworkBehaviour
1314
[Networked] public int WinningTeamId { get; set; } = -1;
1415
public int LocalPlayerTeamId = 0;
1516

16-
[Networked, Capacity(32)]
17+
[Networked, Capacity(32)]
1718
private NetworkDictionary<NetworkString<_32>, int> UnitCounts { get; }
1819

1920
private ChangeDetector _changes;
@@ -39,32 +40,37 @@ public override void Spawned()
3940
WinningTeamId = -1;
4041
UnitCounts.Clear();
4142
}
42-
43+
4344
UpdateAllUI();
4445
}
4546

4647
public override void Render()
4748
{
4849
foreach (var change in _changes.DetectChanges(this))
4950
{
50-
if (change == nameof(WinningTeamId))
51-
{
52-
HandleGameOverUI();
53-
}
54-
55-
if (change == nameof(UnitCounts))
56-
{
57-
UpdateAllUI();
58-
}
51+
if (change == nameof(WinningTeamId)) HandleGameOverUI();
52+
if (change == nameof(UnitCounts)) UpdateAllUI();
5953
}
6054
}
6155

56+
public bool HasReachedLimit(int teamId, string unitName, int maxCapacity)
57+
{
58+
string key = $"{teamId}_{unitName}";
59+
int currentCount = UnitCounts.ContainsKey(key) ? UnitCounts[key] : 0;
60+
return currentCount >= maxCapacity;
61+
}
62+
63+
public int GetCurrentUnitCount(int teamId, string unitName)
64+
{
65+
string key = $"{teamId}_{unitName}";
66+
return UnitCounts.ContainsKey(key) ? UnitCounts[key] : 0;
67+
}
68+
6269
public void ReportTeamLoss(int losingTeamId)
6370
{
6471
if (Object.HasStateAuthority)
6572
{
66-
int winnerId = (losingTeamId == 1) ? 2 : 1;
67-
WinningTeamId = winnerId;
73+
WinningTeamId = (losingTeamId == 1) ? 2 : 1;
6874
}
6975
else
7076
{
@@ -75,8 +81,7 @@ public void ReportTeamLoss(int losingTeamId)
7581
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
7682
private void RPC_ReportLoss(int losingTeamId)
7783
{
78-
int winnerId = (losingTeamId == 1) ? 2 : 1;
79-
WinningTeamId = winnerId;
84+
WinningTeamId = (losingTeamId == 1) ? 2 : 1;
8085
}
8186

8287
private void HandleGameOverUI()
@@ -97,7 +102,7 @@ public void ReportUnitConstructed(Unit.Unit unit)
97102
}
98103
else
99104
{
100-
int maxCap = (unit is Building) ? ((Building)unit).buildingData.maxCreatableCount : (unit is Vehicle) ? ((Vehicle)unit).vehicleData.maxCreatableCount : 0;
105+
int maxCap = GetMaxCapacity(unit);
101106
RPC_ModifyUnitCount(unit.teamId, GetUnitName(unit), maxCap, 1);
102107
}
103108
}
@@ -118,29 +123,19 @@ public void ReportUnitDestroyed(Unit.Unit unit)
118123
private void RPC_ModifyUnitCount(int teamId, string unitName, int maxCapacity, int changeAmount)
119124
{
120125
string key = $"{teamId}_{unitName}";
126+
int current = UnitCounts.ContainsKey(key) ? UnitCounts[key] : 0;
121127

122-
int current = 0;
123-
if (UnitCounts.ContainsKey(key))
124-
{
125-
current = UnitCounts[key];
126-
}
127-
128-
int newValue = Mathf.Clamp(current + changeAmount, 0, maxCapacity);
128+
int newValue = Mathf.Max(0, current + changeAmount);
129129
UnitCounts.Set(key, newValue);
130130
}
131131

132132
private void ModifyUnitCount(Unit.Unit unit, int amount)
133133
{
134134
string unitName = GetUnitName(unit);
135135
string key = $"{unit.teamId}_{unitName}";
136-
137-
int current = 0;
138-
if (UnitCounts.ContainsKey(key))
139-
{
140-
current = UnitCounts[key];
141-
}
142-
143-
int newValue = Mathf.Clamp(current + amount, 0, GetMaxCapacity(unit));
136+
int current = UnitCounts.ContainsKey(key) ? UnitCounts[key] : 0;
137+
138+
int newValue = Mathf.Max(0, current + amount);
144139
UnitCounts.Set(key, newValue);
145140
}
146141

@@ -154,13 +149,7 @@ private void UpdateAllUI()
154149
if (parts.Length < 2) continue;
155150

156151
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-
}
152+
deploymentMonitorHUDController.UpdateUnitSlots(teamId, parts[1], kvp.Value, 10); // 10 sayısı örnek max kapasite olarak kullanıldı.
164153
}
165154
}
166155

0 commit comments

Comments
 (0)