Skip to content

Commit 0938ca3

Browse files
committed
Refactor building selection and placement logic
Replaces 'currentSelectedBuilding' with 'currentSelectedActiveBuilding' for active selection and 'buildingDataToPlace' for placement data, clarifying responsibilities. Updates input handling, placement, and selection methods to use the new variables, and adds animator selection state management for vehicles and buildings. Removes commented debug code and improves audio feedback consistency.
1 parent da8e458 commit 0938ca3

File tree

1 file changed

+52
-58
lines changed

1 file changed

+52
-58
lines changed

Red Strike/Assets/InputController/InputController.cs

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class InputController : MonoBehaviour
3737
public AudioClip errorSound;
3838
private AudioSource audioSource;
3939

40-
private Building currentSelectedBuilding;
40+
private BuildingPlacement.Buildings.Building currentSelectedActiveBuilding;
41+
private Building buildingDataToPlace;
4142
private VehicleSystem.Vehicles.Vehicle currentSelectedVehicle;
4243

4344
private VehiclesHUDController vehiclesHUDController;
@@ -67,50 +68,33 @@ private void Update()
6768
{
6869
if (Input.GetMouseButtonDown(1))
6970
{
70-
if (currentSelectedBuilding != null)
71-
{
72-
currentSelectedBuilding = null;
73-
//Debug.Log("Bina yerleştirme iptal edildi.");
74-
}
75-
else
76-
{
77-
DeselectAll();
78-
}
71+
if (buildingDataToPlace != null) buildingDataToPlace = null;
72+
else DeselectAll();
73+
7974
return;
8075
}
8176

82-
if (tempBuildingHighlighter != null)
77+
if (currentSelectedActiveBuilding != null && tempBuildingHighlighter != null)
8378
{
8479
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.R))
8580
{
86-
var building = tempBuildingHighlighter.GetComponent<BuildingPlacement.Buildings.Building>();
87-
88-
if (building == null) building = tempBuildingHighlighter.GetComponentInParent<BuildingPlacement.Buildings.Building>();
89-
90-
if (building != null)
91-
{
92-
building.RPC_Rotate90();
93-
}
81+
currentSelectedActiveBuilding.RPC_Rotate90();
9482
}
9583
}
9684

9785
if (Input.GetMouseButtonDown(0))
9886
{
9987
if (IsPointerOverUI()) return;
10088

101-
if (currentSelectedBuilding != null)
102-
{
103-
PlaceBuilding();
104-
}
105-
else
106-
{
107-
HandleObjectSelection();
108-
}
89+
if (buildingDataToPlace != null) PlaceBuilding();
90+
else HandleObjectSelection();
10991
}
11092
}
11193

11294
private void PlaceBuilding()
11395
{
96+
if (buildingDataToPlace == null) return;
97+
11498
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
11599
RaycastHit hitInfo;
116100

@@ -120,7 +104,6 @@ private void PlaceBuilding()
120104

121105
if (!IsPositionValid(spawnPosition))
122106
{
123-
//Debug.LogWarning("Bu konumda başka bir nesne var. Lütfen başka bir yere yerleştirin.");
124107
audioSource.PlayOneShot(errorSound);
125108
return;
126109
}
@@ -132,46 +115,41 @@ private void PlaceBuilding()
132115
return unit != null && unit.teamId == teamId;
133116
});
134117

135-
if (currentSelectedBuilding.buildingName != "Main Station" && !isThereMainBuilding)
118+
if (buildingDataToPlace.buildingName != "Main Station" && !isThereMainBuilding)
136119
{
137-
//Debug.LogWarning("Önce bir Ana Üs (Main Station) yerleştirmelisiniz!");
138-
audioSource.PlayOneShot(errorSound);
120+
audioSource.PlayOneShot(errorSound); // "Önce Main Station kurmalısın"
139121
return;
140122
}
141123

142-
if (currentSelectedBuilding.buildingName == "Main Station" && isThereMainBuilding)
124+
if (buildingDataToPlace.buildingName == "Main Station" && isThereMainBuilding)
143125
{
144-
//Debug.LogWarning("Zaten bir Ana Üs'sünüz var.");
145-
audioSource.PlayOneShot(errorSound);
126+
audioSource.PlayOneShot(errorSound); // "Zaten Main Station var"
146127
return;
147128
}
148129

149130
if (CommanderData.LocalCommander != null)
150131
{
151132
bool isLimitReached = GameStateManager.Instance.HasReachedLimit(
152133
teamId,
153-
currentSelectedBuilding.buildingName,
154-
currentSelectedBuilding.maxCreatableCount
134+
buildingDataToPlace.buildingName,
135+
buildingDataToPlace.maxCreatableCount
155136
);
156137

157138
if (isLimitReached)
158139
{
159-
//Debug.LogWarning("Bu bina türünden maksimum sayıya ulaştınız.");
160-
audioSource.PlayOneShot(errorSound);
140+
audioSource.PlayOneShot(errorSound); // "Bu bina için limit doldu"
161141
return;
162142
}
163143

164-
spawnPosition = spawnPosition + new Vector3(0, currentSelectedBuilding.heightOffset, 0);
165-
CommanderData.LocalCommander.RPC_SpawnBuilding(currentSelectedBuilding.buildingName, spawnPosition);
144+
spawnPosition = spawnPosition + new Vector3(0, buildingDataToPlace.heightOffset, 0);
145+
CommanderData.LocalCommander.RPC_SpawnBuilding(buildingDataToPlace.buildingName, spawnPosition);
166146

167147
audioSource.PlayOneShot(placementSound);
168148
}
169-
else return;
170149

171-
currentSelectedBuilding = null;
150+
buildingDataToPlace = null;
172151
}
173152
}
174-
175153
private bool IsPositionValid(Vector3 position)
176154
{
177155
bool hasObstacle = Physics.CheckSphere(position, minDistanceBetweenObjects, selectableLayer);
@@ -180,18 +158,19 @@ private bool IsPositionValid(Vector3 position)
180158

181159
public void SelectBuildingToPlace(string buildingName)
182160
{
183-
Building buildingToSelect = buildingsDatabase.buildings.FirstOrDefault(b => b.buildingName == buildingName);
161+
Building buildingData = buildingsDatabase.buildings
162+
.FirstOrDefault(b => b.buildingName == buildingName);
184163

185-
if (buildingToSelect != null)
164+
if (buildingData != null)
186165
{
187-
currentSelectedBuilding = buildingToSelect;
166+
DeselectAll();
167+
168+
buildingDataToPlace = buildingData;
188169
audioSource.PlayOneShot(selectionSound);
189-
//Debug.Log($"Seçilen: {currentSelectedBuilding.buildingName}. Yere tıklayın.");
190170
}
191171
else
192172
{
193-
audioSource.PlayOneShot(errorSound); // TODO: Burası bildirimler ile değiştirilebilir.
194-
//Debug.LogError($"Database Hatası: {buildingName} bulunamadı!");
173+
audioSource.PlayOneShot(errorSound);
195174
}
196175
}
197176

@@ -220,7 +199,6 @@ private void HandleObjectSelection()
220199
{
221200
if (currentSelectedVehicle != null)
222201
{
223-
//Debug.Log($"Saldırı Emri: {currentSelectedVehicle.name} -> {unit.name}");
224202
currentSelectedVehicle.SetTargetEnemy(unit.gameObject);
225203
audioSource.PlayOneShot(selectionSound);
226204

@@ -230,8 +208,7 @@ private void HandleObjectSelection()
230208
}
231209
else
232210
{
233-
//Debug.Log("Düşmanı seçmek için önce kendi aracınızı seçin.");
234-
audioSource.PlayOneShot(errorSound);
211+
audioSource.PlayOneShot(errorSound); // "Önce bir arac seçmelisin"
235212
}
236213
return;
237214
}
@@ -248,18 +225,25 @@ private void HandleObjectSelection()
248225
vehicleHighlighter?.EnableHighlight(teamId, unit.teamId);
249226
vehiclesHUDController?.ShowVehicleDetails(currentSelectedVehicle);
250227
audioSource.PlayOneShot(selectionSound);
228+
229+
if (currentSelectedVehicle.animator != null)
230+
currentSelectedVehicle.animator.SetBool("isSelected", true);
251231
}
252232
break;
253233

254234
case Unit.UnitType.Building:
255-
tempBuildingHighlighter = GetHighlighter(unit.gameObject);
256-
tempBuildingHighlighter?.EnableHighlight(teamId, unit.teamId);
235+
currentSelectedActiveBuilding = unit.GetComponent<BuildingPlacement.Buildings.Building>();
257236

258-
var building = unit.GetComponent<BuildingPlacement.Buildings.Building>();
259-
if (building != null)
237+
if (currentSelectedActiveBuilding != null)
260238
{
261-
buildingHUDController.ShowBuildingDetails(building);
239+
tempBuildingHighlighter = GetHighlighter(unit.gameObject);
240+
tempBuildingHighlighter?.EnableHighlight(teamId, unit.teamId);
241+
242+
buildingHUDController.ShowBuildingDetails(currentSelectedActiveBuilding);
262243
audioSource.PlayOneShot(selectionSound);
244+
245+
if (currentSelectedActiveBuilding.animator != null)
246+
currentSelectedActiveBuilding.animator.SetBool("isSelected", true);
263247
}
264248
break;
265249
}
@@ -274,12 +258,22 @@ private void DeselectAll()
274258
targetHighlighter?.DisableHighlight();
275259
tempBuildingHighlighter?.DisableHighlight();
276260

261+
if (currentSelectedVehicle != null && currentSelectedVehicle.animator != null)
262+
{
263+
currentSelectedVehicle.animator.SetBool("isSelected", false);
264+
}
265+
266+
if (currentSelectedActiveBuilding != null && currentSelectedActiveBuilding.animator != null)
267+
{
268+
currentSelectedActiveBuilding.animator.SetBool("isSelected", false);
269+
}
270+
277271
vehicleHighlighter = null;
278272
targetHighlighter = null;
279273
tempBuildingHighlighter = null;
280274

281275
currentSelectedVehicle = null;
282-
currentSelectedBuilding = null;
276+
currentSelectedActiveBuilding = null;
283277
}
284278

285279
private SelectionHighlighter GetHighlighter(GameObject obj)

0 commit comments

Comments
 (0)