Skip to content

Commit 89119ff

Browse files
committed
Refactor building destruction and game over logic
Moved game over handling from MainStation to Building and GameStateManager. GameStateManager now tracks the winning team using a networked property and updates the UI accordingly. Updated health handling in Building to be networked, and refactored related UI and team ID code. Minor material and scene updates to support new logic.
1 parent 9d7b634 commit 89119ff

File tree

11 files changed

+126
-65
lines changed

11 files changed

+126
-65
lines changed
Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
using UnityEngine;
2-
using Unit;
2+
using GameStateSystem;
3+
using Fusion;
4+
using Unity.VisualScripting;
35

46
namespace BuildingPlacement.Buildings
57
{
68
public class Building : Unit.Unit
79
{
810
public BuildingPlacement.Building buildingData;
9-
public ParticleSystem[] buildEffects;
1011

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

16-
private void Start()
17+
public override void Spawned()
18+
{
19+
if (Object.HasStateAuthority)
20+
{
21+
health = buildingData.maxHealth;
22+
Invoke(nameof(Test_Damage), 5f);
23+
}
24+
}
25+
26+
private void Test_Damage()
1727
{
18-
maxHealth = buildingData.maxHealth;
19-
health = maxHealth;
28+
if (Object.HasStateAuthority)
29+
{
30+
TakeDamage(health + 1);
31+
}
2032
}
2133

2234
public override void TakeDamage(float damage)
2335
{
36+
if (!Object.HasStateAuthority) return;
37+
2438
base.TakeDamage(damage);
2539

2640
health -= damage;
27-
health = Mathf.Max(0, health);
28-
29-
Debug.Log($"Building {BuildingName} took {damage} damage. Remaining health: {health}");
3041

3142
if (health <= 0)
3243
{
3344
Debug.Log($"Building {BuildingName} destroyed.");
45+
3446
ParticleSystem exp = Instantiate(buildingData.explosionEffect, transform.position, Quaternion.identity);
35-
OnDestroy();
47+
Destroy(exp.gameObject, exp.main.duration);
48+
49+
if (this is MainStation)
50+
{
51+
if (GameStateManager.Instance != null)
52+
{
53+
GameStateManager.Instance.ReportTeamLoss(teamId);
54+
}
55+
}
56+
57+
Runner.Despawn(Object);
3658
}
3759
}
3860

@@ -47,12 +69,5 @@ private void OnCollisionEnter(Collision collision)
4769

4870
Debug.Log($"Building {BuildingName} collided with unit: {collision.gameObject.name}");
4971
}
50-
51-
[ContextMenu("Test Delete Main Station")]
52-
public void TestDelete() // TEST METHOD
53-
{
54-
Debug.Log("Main Station Test Delete triggered.");
55-
TakeDamage(health + 1); // Ensure destruction
56-
}
5772
}
5873
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,5 @@ private void Update()
1717
{
1818
return (buildingData.name, health);
1919
}
20-
21-
protected override void OnDestroy()
22-
{
23-
base.OnDestroy();
24-
25-
if (GameStateSystem.GameStateManager.Instance != null)
26-
{
27-
GameStateSystem.GameStateManager.Instance.GameOver();
28-
}
29-
}
3020
}
3121
}
Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,83 @@
1-
using NetworkingSystem;
1+
using Fusion;
22
using UnityEngine;
3+
using UISystem;
34

45
namespace GameStateSystem
56
{
6-
public class GameStateManager : MonoBehaviour
7+
public class GameStateManager : NetworkBehaviour
78
{
89
public static GameStateManager Instance;
910

10-
public enum GameState { MainMenu, InGame, GameOver }
11-
public GameState CurrentState { get; private set; }
11+
[Networked] public int WinningTeamId { get; set; } = -1;
1212

13-
public int PlayerTeamId = 0;
13+
public int LocalPlayerTeamId = 0;
14+
15+
private ChangeDetector _changes;
1416

1517
private void Awake()
1618
{
1719
if (Instance == null) Instance = this;
1820
else Destroy(gameObject);
1921
}
2022

21-
private void Start()
23+
public override void Spawned()
24+
{
25+
_changes = GetChangeDetector(ChangeDetector.Source.SimulationState);
26+
27+
if (Object.HasStateAuthority)
28+
{
29+
WinningTeamId = -1;
30+
}
31+
}
32+
33+
public override void Render()
34+
{
35+
foreach (var change in _changes.DetectChanges(this))
36+
{
37+
if (change == nameof(WinningTeamId))
38+
{
39+
HandleGameOverUI();
40+
}
41+
}
42+
}
43+
44+
public void ReportTeamLoss(int losingTeamId)
2245
{
23-
CurrentState = GameState.InGame;
46+
if (Object.HasStateAuthority)
47+
{
48+
int winnerId = (losingTeamId == 1) ? 2 : 1;
49+
WinningTeamId = winnerId;
50+
}
51+
else
52+
{
53+
RPC_ReportLoss(losingTeamId);
54+
}
2455
}
2556

26-
public void GameOver()
57+
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
58+
private void RPC_ReportLoss(int losingTeamId)
2759
{
28-
Debug.Log("Oyun Bitti!");
29-
CurrentState = GameState.GameOver;
30-
FindAnyObjectByType<UISystem.GameStatusHUDController>().ShowGameOverPanel();
31-
CommanderData.LocalCommander?.OnDisconnect();
60+
int winnerId = (losingTeamId == 1) ? 2 : 1;
61+
WinningTeamId = winnerId;
62+
}
63+
64+
private void HandleGameOverUI()
65+
{
66+
if (WinningTeamId == -1) return;
67+
68+
var hud = FindAnyObjectByType<GameStatusHUDController>();
69+
if (hud == null) return;
70+
71+
Debug.Log($"Oyun Bitti! Kazanan: {WinningTeamId}, Benim Takımım: {LocalPlayerTeamId}");
72+
73+
if (WinningTeamId == LocalPlayerTeamId)
74+
{
75+
hud.ShowVictoryPanel();
76+
}
77+
else
78+
{
79+
hud.ShowGameOverPanel();
80+
}
3281
}
3382
}
34-
}
83+
}

Red Strike/Assets/NetworkingSystem/CommanderData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void Render()
4545
if (PlayerTeamID != 0 && InputController.InputController.Instance.teamId != PlayerTeamID)
4646
{
4747
InputController.InputController.Instance.teamId = PlayerTeamID;
48-
GameStateSystem.GameStateManager.Instance.PlayerTeamId = PlayerTeamID;
48+
GameStateSystem.GameStateManager.Instance.LocalPlayerTeamId = PlayerTeamID;
4949
Debug.Log($"<color=yellow>ZORLA EŞİTLEME:</color> InputController ve GameStateManager ID'leri {PlayerTeamID} olarak düzeltildi.");
5050
}
5151
}
@@ -59,7 +59,7 @@ private void OnTeamIdChanged()
5959
if (InputController.InputController.Instance != null && PlayerTeamID != 0)
6060
{
6161
InputController.InputController.Instance.teamId = PlayerTeamID;
62-
GameStateSystem.GameStateManager.Instance.PlayerTeamId = PlayerTeamID;
62+
GameStateSystem.GameStateManager.Instance.LocalPlayerTeamId = PlayerTeamID;
6363
Debug.Log($"<color=green>GÜNCELLEME:</color> Takım ID'si {PlayerTeamID} olarak değişti ve ayarlandı!");
6464
}
6565
}

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.09429583, g: -0.8149934, b: 0.5717467, a: 0}
50+
- _SunDirection: {r: 0.16091488, g: 0.37587303, b: -0.91259295, 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.09429583, g: -0.8149934, b: 0.5717467, a: 0}
46+
- _SunDirection: {r: 0.16091488, g: 0.37587303, b: -0.91259295, 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.09429583, g: -0.8149934, b: 0.5717467, a: 0}
57+
- _SunDirection: {r: 0.16091488, g: 0.37587303, b: -0.91259295, a: 0}
5858
m_BuildTextureStacks: []
5959
m_AllowLocking: 1

0 commit comments

Comments
 (0)