Skip to content

Commit dca5f07

Browse files
committed
Refactor input and UI: add InputController, vehicle details UI
Removed TerrainClickPlacer and replaced its logic with a new InputController script, which now handles both building placement and vehicle selection. Added VehiclesDetailManager and updated the SampleScene with new UI elements for displaying vehicle details, including vehicle name, fuel level, and bullet count. Updated scene and asset references accordingly.
1 parent 70623fb commit dca5f07

18 files changed

+1276
-195
lines changed

Red Strike/Assets/BuildingPlacement/TerrainClickPlacer.cs

Lines changed: 0 additions & 92 deletions
This file was deleted.

Red Strike/Assets/BuildingPlacement/TerrainClickPlacer.cs.meta

Lines changed: 0 additions & 2 deletions
This file was deleted.

Red Strike/Assets/InputController.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
using VehicleSystem;
5+
using BuildingPlacement;
6+
7+
namespace InputController
8+
{
9+
public class InputController : MonoBehaviour
10+
{
11+
public LayerMask terrainLayer;
12+
public LayerMask selectableLayer;
13+
private Camera mainCamera;
14+
public BuildingsDatabase buildingsDatabase;
15+
private Dictionary<string, int> buildingCounts = new Dictionary<string, int>();
16+
public Button[] createBuildButtons;
17+
private Building selectedBuilding;
18+
private List<GameObject> placedObjects = new List<GameObject>();
19+
public float minDistanceBetweenObjects = 5f;
20+
public VehiclesDetailManager vehiclesDetailManager;
21+
22+
private void Start()
23+
{
24+
mainCamera = Camera.main;
25+
foreach (Button button in createBuildButtons)
26+
{
27+
button.onClick.AddListener(() => OnCreateBuildingButtonClicked(button));
28+
}
29+
}
30+
31+
private void Update()
32+
{
33+
if (Input.GetMouseButtonDown(1) && selectedBuilding != null)
34+
{
35+
Debug.Log("Bina yerleştirme iptal edildi.");
36+
selectedBuilding = null;
37+
return;
38+
}
39+
40+
if (Input.GetMouseButtonDown(0))
41+
{
42+
if (selectedBuilding != null)
43+
{
44+
PlaceBuilding();
45+
}
46+
else
47+
{
48+
SelectObject();
49+
}
50+
}
51+
}
52+
53+
private void PlaceBuilding()
54+
{
55+
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
56+
RaycastHit hitInfo;
57+
58+
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, terrainLayer))
59+
{
60+
Vector3 spawnPosition = hitInfo.point;
61+
62+
if (IsPositionValid(spawnPosition))
63+
{
64+
if (buildingCounts.ContainsKey(selectedBuilding.buildingName) &&
65+
buildingCounts[selectedBuilding.buildingName] >= selectedBuilding.maxCreatedUnits)
66+
{
67+
Debug.Log(selectedBuilding.buildingName + " için maksimum yerleştirme limitine ulaşıldı.");
68+
return;
69+
}
70+
GameObject placedObject = Instantiate(selectedBuilding.buildingPrefab, spawnPosition, Quaternion.identity);
71+
placedObjects.Add(placedObject);
72+
73+
if (buildingCounts.ContainsKey(selectedBuilding.buildingName))
74+
buildingCounts[selectedBuilding.buildingName]++;
75+
else
76+
buildingCounts[selectedBuilding.buildingName] = 1;
77+
78+
selectedBuilding = null;
79+
Debug.Log(placedObject.name + " yerleştirildi.");
80+
}
81+
else
82+
{
83+
Debug.Log("Bu pozisyon başka bir objeye çok yakın!");
84+
}
85+
}
86+
}
87+
88+
private void SelectObject()
89+
{
90+
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
91+
RaycastHit hitInfo;
92+
93+
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, selectableLayer))
94+
{
95+
VehicleSystem.Vehicles.Vehicle clickedVehicle = hitInfo.collider.GetComponent<VehicleSystem.Vehicles.Vehicle>();
96+
97+
if (clickedVehicle != null)
98+
{
99+
vehiclesDetailManager.UpdateVehicleDetails(clickedVehicle);
100+
}
101+
else
102+
{
103+
vehiclesDetailManager.HideDetailsPanel();
104+
}
105+
}
106+
else
107+
{
108+
vehiclesDetailManager.HideDetailsPanel();
109+
}
110+
}
111+
112+
private void OnCreateBuildingButtonClicked(Button button)
113+
{
114+
int buttonIndex = System.Array.IndexOf(createBuildButtons, button);
115+
if (buttonIndex >= 0 && buttonIndex < buildingsDatabase.buildings.Count)
116+
{
117+
selectedBuilding = buildingsDatabase.buildings[buttonIndex];
118+
Debug.Log("Seçilen bina: " + selectedBuilding.buildingName + ". Yerleştirmek için araziye tıklayın.");
119+
}
120+
}
121+
122+
private bool IsPositionValid(Vector3 position)
123+
{
124+
foreach (GameObject placedObject in placedObjects)
125+
{
126+
if (Vector3.Distance(position, placedObject.transform.position) < minDistanceBetweenObjects)
127+
{
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
133+
}
134+
}

Red Strike/Assets/InputController/InputController.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Red Strike/Assets/Scenes/SampleScene.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)