Skip to content

Commit 93cbb93

Browse files
committed
Refactor teamId to networked property and improve building placement
Converted Unit.teamId to a [Networked] property and updated all prefabs to use the new backing field. Refactored InputController to use networked building placement via CommanderData.RPC_SpawnBuilding, enforcing main base placement rules and improving selection/deselection logic. Updated CommanderData to synchronize team IDs and handle networked building spawning. Prefabs updated to include Unit as a NetworkBehaviour and to reference the correct networked fields.
1 parent 0864bfe commit 93cbb93

File tree

15 files changed

+192
-151
lines changed

15 files changed

+192
-151
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ public class Building : Unit.Unit
1515

1616
private void Start()
1717
{
18-
foreach (ParticleSystem effect in buildEffects)
19-
{
20-
var main = effect.main;
21-
main.startColor = playerType == PlayerType.Red ? Color.red : Color.blue;
22-
}
23-
2418
maxHealth = buildingData.maxHealth;
2519
health = maxHealth;
2620
}
2721

2822
public override void TakeDamage(float damage)
2923
{
24+
if (!Object.HasStateAuthority) return;
25+
3026
health -= damage;
3127
health = Mathf.Max(0, health);
3228

@@ -36,7 +32,7 @@ public override void TakeDamage(float damage)
3632
{
3733
Debug.Log($"Building {BuildingName} destroyed.");
3834
Instantiate(buildingData.explosionEffect, transform.position, Quaternion.identity);
39-
Destroy(gameObject);
35+
Runner.Despawn(Object);
4036
}
4137
}
4238

Red Strike/Assets/InputController/InputController.cs

Lines changed: 91 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@
55
using UISystem;
66
using System.Linq;
77
using UnityEngine.UIElements;
8+
using NetworkingSystem;
89

910
namespace InputController
1011
{
1112
public class InputController : MonoBehaviour
1213
{
14+
public static InputController Instance;
15+
16+
[Header("UI & Database")]
1317
public UIDocument gameUIDocument;
18+
public BuildingsDatabase buildingsDatabase;
1419

20+
[Header("Layers & Camera")]
1521
public LayerMask terrainLayer;
1622
public LayerMask selectableLayer;
1723
private Camera mainCamera;
18-
public BuildingsDatabase buildingsDatabase;
19-
private Dictionary<string, int> buildingCounts = new Dictionary<string, int>();
24+
25+
[Header("Settings")]
26+
public float minDistanceBetweenObjects = 5f;
27+
public int teamId = 0;
28+
2029
private Building currentSelectedBuilding;
2130
private Vehicle currentSelectedVehicle;
22-
private List<GameObject> placedObjects = new List<GameObject>();
23-
public float minDistanceBetweenObjects = 5f;
31+
2432
private VehiclesHUDController vehiclesHUDController;
2533
private BuildingHUDController buildingHUDController;
2634

2735
private SelectionHighlighter vehicleHighlighter;
2836
private SelectionHighlighter targetHighlighter;
29-
3037
private SelectionHighlighter tempBuildingHighlighter;
3138

32-
public int teamId = 0;
33-
34-
public static InputController Instance;
39+
// Limitleme için sayaç (İsteğe bağlı, şimdilik basit tutuyoruz)
40+
private Dictionary<string, int> buildingCounts = new Dictionary<string, int>();
3541

3642
private void Awake()
3743
{
@@ -42,16 +48,23 @@ private void Awake()
4248
private void Start()
4349
{
4450
mainCamera = Camera.main;
45-
4651
vehiclesHUDController = GetComponent<VehiclesHUDController>();
4752
buildingHUDController = GetComponent<BuildingHUDController>();
4853
}
4954

5055
private void Update()
5156
{
52-
if (Input.GetMouseButtonDown(1) && currentSelectedBuilding != null)
57+
if (Input.GetMouseButtonDown(1))
5358
{
54-
DeselectAll();
59+
if (currentSelectedBuilding != null)
60+
{
61+
currentSelectedBuilding = null;
62+
Debug.Log("Bina yerleştirme iptal edildi.");
63+
}
64+
else
65+
{
66+
DeselectAll();
67+
}
5568
return;
5669
}
5770

@@ -65,10 +78,7 @@ private void Update()
6578

6679
if (Input.GetMouseButtonDown(0))
6780
{
68-
if (IsPointerOverUI())
69-
{
70-
return;
71-
}
81+
if (IsPointerOverUI()) return;
7282

7383
if (currentSelectedBuilding != null)
7484
{
@@ -81,20 +91,6 @@ private void Update()
8191
}
8292
}
8393

84-
private bool IsPointerOverUI()
85-
{
86-
if (gameUIDocument == null) return false;
87-
88-
Vector2 panelLocalPos = RuntimePanelUtils.ScreenToPanel(gameUIDocument.rootVisualElement.panel, Input.mousePosition);
89-
VisualElement pickedElement = gameUIDocument.rootVisualElement.panel.Pick(panelLocalPos);
90-
91-
if (pickedElement == null) return false;
92-
if (pickedElement.name == "root-container") return false;
93-
if (pickedElement == gameUIDocument.rootVisualElement) return false;
94-
95-
return true;
96-
}
97-
9894
private void PlaceBuilding()
9995
{
10096
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
@@ -106,61 +102,63 @@ private void PlaceBuilding()
106102

107103
if (IsPositionValid(spawnPosition))
108104
{
109-
bool isThereMainBuilding = placedObjects.Any(obj => obj.GetComponent<BuildingPlacement.Buildings.MainStation>() != null);
105+
bool isThereMainBuilding = FindObjectsByType<BuildingPlacement.Buildings.MainStation>(FindObjectsSortMode.None)
106+
.Any(station =>
107+
{
108+
var unit = station.GetComponent<Unit.Unit>();
109+
return unit != null && unit.teamId == teamId;
110+
});
111+
110112
if (currentSelectedBuilding.buildingName != "Main Station" && !isThereMainBuilding)
111113
{
112-
Debug.Log("Önce bir Ana Üs yerleştirmeniz gerekiyor. Ana Üs olmadan başka binalar yerleştiremezsiniz.");
113-
currentSelectedBuilding = null;
114+
Debug.LogWarning("Önce bir Ana Üs (Main Station) yerleştirmelisiniz!");
114115
return;
115116
}
117+
116118
if (currentSelectedBuilding.buildingName == "Main Station" && isThereMainBuilding)
117119
{
118-
Debug.Log("Zaten bir Ana Üs yerleştirildi.");
119-
currentSelectedBuilding = null;
120+
Debug.LogWarning("Zaten bir Ana Üs'sünüz var.");
120121
return;
121122
}
122-
if (buildingCounts.ContainsKey(currentSelectedBuilding.buildingName) &&
123-
buildingCounts[currentSelectedBuilding.buildingName] >= currentSelectedBuilding.maxCreatedUnits)
123+
124+
if (CommanderData.LocalCommander != null)
124125
{
125-
Debug.Log(currentSelectedBuilding.buildingName + " için maksimum yerleştirme limitine ulaşıldı.");
126-
return;
126+
CommanderData.LocalCommander.RPC_SpawnBuilding(currentSelectedBuilding.buildingName, spawnPosition);
127+
Debug.Log($"<color=green>Server'a İstek:</color> {currentSelectedBuilding.buildingName} kuruluyor...");
127128
}
128-
129-
GameObject placedObject = Instantiate(currentSelectedBuilding.buildingPrefab, spawnPosition, Quaternion.identity);
130-
131-
if (placedObject.GetComponent<SelectionHighlighter>() == null)
132-
placedObject.AddComponent<SelectionHighlighter>();
133-
134-
placedObjects.Add(placedObject);
135-
136-
if (buildingCounts.ContainsKey(currentSelectedBuilding.buildingName))
137-
buildingCounts[currentSelectedBuilding.buildingName]++;
138129
else
139-
buildingCounts[currentSelectedBuilding.buildingName] = 1;
130+
{
131+
Debug.LogError("HATA: LocalCommander (Network Bağlantısı) bulunamadı!");
132+
return;
133+
}
140134

141135
currentSelectedBuilding = null;
142-
143-
Debug.Log(placedObject.name + " yerleştirildi.");
144136
}
145137
else
146138
{
147-
Debug.Log("Bu pozisyon başka bir objeye çok yakın!");
139+
Debug.Log("Bu alan inşaat için uygun değil (Çok yakın veya engel var).");
148140
}
149141
}
150142
}
151143

144+
private bool IsPositionValid(Vector3 position)
145+
{
146+
bool hasObstacle = Physics.CheckSphere(position, minDistanceBetweenObjects, selectableLayer);
147+
return !hasObstacle;
148+
}
149+
152150
public void SelectBuildingToPlace(string buildingName)
153151
{
154152
Building buildingToSelect = buildingsDatabase.buildings.FirstOrDefault(b => b.buildingName == buildingName);
155153

156154
if (buildingToSelect != null)
157155
{
158156
currentSelectedBuilding = buildingToSelect;
159-
Debug.Log("Seçilen bina: " + currentSelectedBuilding.buildingName + ". Yerleştirmek için araziye tıklayın.");
157+
Debug.Log($"Seçilen: {currentSelectedBuilding.buildingName}. Yere tıklayın.");
160158
}
161159
else
162160
{
163-
Debug.LogError(buildingName + " isminde bir bina veritabanında bulunamadı!");
161+
Debug.LogError($"Database Hatası: {buildingName} bulunamadı!");
164162
}
165163
}
166164

@@ -178,32 +176,28 @@ private void HandleObjectSelection()
178176
var unit = hitInfo.collider.GetComponent<Unit.Unit>();
179177
if (unit == null)
180178
{
181-
DeselectAll();
182-
return;
179+
unit = hitInfo.collider.GetComponentInParent<Unit.Unit>();
180+
if(unit == null) { DeselectAll(); return; }
183181
}
184182

185183
bool isFriendly = unit.teamId == teamId;
186184
bool isEnemy = unit.teamId != teamId;
187185

188-
if (isEnemy && currentSelectedVehicle == null)
189-
{
190-
Debug.Log("Düşman hedef seçemezsin çünkü önce bir araç seçmen lazım.");
191-
return;
192-
}
193-
194186
if (isEnemy)
195187
{
196-
Debug.Log($"Araç {currentSelectedVehicle.name} hedefe kilitlendi: {unit.name}");
197-
198-
currentSelectedVehicle.SetTargetEnemy(unit.gameObject);
199-
200-
if (targetHighlighter != null)
201-
targetHighlighter.DisableHighlight();
202-
203-
targetHighlighter = GetHighlighter(unit.gameObject);
204-
if (targetHighlighter != null)
205-
targetHighlighter.EnableHighlight();
188+
if (currentSelectedVehicle != null)
189+
{
190+
Debug.Log($"Saldırı Emri: {currentSelectedVehicle.name} -> {unit.name}");
191+
currentSelectedVehicle.SetTargetEnemy(unit.gameObject);
206192

193+
if (targetHighlighter != null) targetHighlighter.DisableHighlight();
194+
targetHighlighter = GetHighlighter(unit.gameObject);
195+
targetHighlighter?.EnableHighlight();
196+
}
197+
else
198+
{
199+
Debug.Log("Düşmanı seçmek için önce kendi aracınızı seçin.");
200+
}
207201
return;
208202
}
209203

@@ -213,63 +207,37 @@ private void HandleObjectSelection()
213207
{
214208
case Unit.UnitType.Vehicle:
215209
currentSelectedVehicle = unit.GetComponent<Vehicle>();
216-
217210
if (currentSelectedVehicle != null)
218211
{
219212
vehicleHighlighter = GetHighlighter(unit.gameObject);
220-
if (vehicleHighlighter != null)
221-
vehicleHighlighter.EnableHighlight();
222-
223-
if (currentSelectedVehicle.targetObject != null)
224-
{
225-
targetHighlighter = GetHighlighter(currentSelectedVehicle.targetObject);
226-
if (targetHighlighter != null)
227-
targetHighlighter.EnableHighlight();
228-
}
229-
213+
vehicleHighlighter?.EnableHighlight();
230214
vehiclesHUDController?.ShowVehicleDetails(currentSelectedVehicle);
231215
}
232216
break;
233217

234218
case Unit.UnitType.Building:
235219
tempBuildingHighlighter = GetHighlighter(unit.gameObject);
236220
tempBuildingHighlighter?.EnableHighlight();
237-
221+
238222
var building = unit.GetComponent<BuildingPlacement.Buildings.Building>();
239-
if (building != null)
240-
buildingHUDController.ShowBuildingDetails(building);
241-
242-
break;
243-
244-
default:
245-
DeselectAll();
223+
if (building != null) buildingHUDController.ShowBuildingDetails(building);
246224
break;
247225
}
248226
}
249227

250228
private void DeselectAll()
251229
{
252-
if (vehiclesHUDController != null) vehiclesHUDController.HideVehicleDetails();
253-
if (buildingHUDController != null) buildingHUDController.HideBuildingDetails();
230+
vehiclesHUDController?.HideVehicleDetails();
231+
buildingHUDController?.HideBuildingDetails();
254232

255-
if (vehicleHighlighter != null)
256-
{
257-
vehicleHighlighter.DisableHighlight();
258-
vehicleHighlighter = null;
259-
}
260-
261-
if (targetHighlighter != null)
262-
{
263-
targetHighlighter.DisableHighlight();
264-
targetHighlighter = null;
265-
}
266-
267-
if (tempBuildingHighlighter != null)
268-
{
269-
tempBuildingHighlighter.DisableHighlight();
270-
tempBuildingHighlighter = null;
271-
}
233+
vehicleHighlighter?.DisableHighlight();
234+
targetHighlighter?.DisableHighlight();
235+
tempBuildingHighlighter?.DisableHighlight();
272236

237+
vehicleHighlighter = null;
238+
targetHighlighter = null;
239+
tempBuildingHighlighter = null;
240+
273241
currentSelectedVehicle = null;
274242
currentSelectedBuilding = null;
275243
}
@@ -281,15 +249,20 @@ private SelectionHighlighter GetHighlighter(GameObject obj)
281249
return hl;
282250
}
283251

284-
private bool IsPositionValid(Vector3 position)
252+
private bool IsPointerOverUI()
285253
{
286-
foreach (GameObject placedObject in placedObjects)
287-
{
288-
if (Vector3.Distance(position, placedObject.transform.position) < minDistanceBetweenObjects)
289-
{
290-
return false;
291-
}
292-
}
254+
if (gameUIDocument == null) return false;
255+
256+
if (UnityEngine.EventSystems.EventSystem.current != null && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
257+
return true;
258+
259+
Vector2 panelLocalPos = RuntimePanelUtils.ScreenToPanel(gameUIDocument.rootVisualElement.panel, Input.mousePosition);
260+
VisualElement pickedElement = gameUIDocument.rootVisualElement.panel.Pick(panelLocalPos);
261+
262+
if (pickedElement == null) return false;
263+
if (pickedElement.name == "root-container") return false;
264+
if (pickedElement == gameUIDocument.rootVisualElement) return false;
265+
293266
return true;
294267
}
295268
}

0 commit comments

Comments
 (0)