Skip to content

Commit c1b5ee6

Browse files
committed
Add vehicle creation system to Hangar and UI integration
Implemented vehicle creation in the Hangar building using a new VehicleTypes enum and VehiclesDatabase. Updated HangarDetailsContent.uxml and BuildingHUDController to provide UI buttons for creating specific vehicle types. Added vehiclePrefab and vehicleType fields to Vehicle assets, and linked them in VehiclesDatabase. Improved InputController to ignore input when clicking UI, and added EventSystem to the scene for proper UI interaction.
1 parent 096fe3c commit c1b5ee6

File tree

22 files changed

+268
-35
lines changed

22 files changed

+268
-35
lines changed
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
11
using UnityEngine;
2+
using VehicleSystem;
23

34
namespace BuildingPlacement.Buildings
45
{
56
public class Hangar : Building
67
{
78
// TEST
89
public bool IsReady = true;
9-
public string InProductionUnitName = "Tank";
10+
public string InProductionUnitName = "None";
11+
12+
private Vector3 vehicleSpawnPoint;
13+
14+
public VehiclesDatabase vehiclesDatabase;
15+
16+
private void Start()
17+
{
18+
vehicleSpawnPoint = transform.position + transform.forward * 10f;
19+
}
20+
21+
public void CreateVehicle(VehicleTypes vehicleType)
22+
{
23+
if (CanCreateVehicle())
24+
{
25+
InProductionUnitName = vehicleType.ToString();
26+
IsReady = false;
27+
28+
Debug.Log($"Hangar started creating a new vehicle: {InProductionUnitName}");
29+
CreateVehicleInstance(vehicleType);
30+
}
31+
else
32+
{
33+
Debug.LogWarning("Hangar is not ready to create a new vehicle or already in production.");
34+
}
35+
}
36+
37+
private void CreateVehicleInstance(VehicleTypes vehicleType)
38+
{
39+
foreach (Vehicle item in vehiclesDatabase.vehicles)
40+
{
41+
if (item.vehicleType == vehicleType)
42+
{
43+
Instantiate(item.vehiclePrefab, vehicleSpawnPoint, Quaternion.identity);
44+
InProductionUnitName = "None";
45+
IsReady = true;
46+
return;
47+
}
48+
}
49+
}
50+
51+
public bool CanCreateVehicle()
52+
{
53+
return IsReady && InProductionUnitName == "None";
54+
}
1055
}
1156
}

Red Strike/Assets/BuildingPlacement/Buildings/Hangar/Hangar.prefab

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ MonoBehaviour:
221221
buildEffects:
222222
- {fileID: 1855726554393846403}
223223
- {fileID: 4273443631821389091}
224+
IsReady: 1
225+
InProductionUnitName: None
226+
vehiclesDatabase: {fileID: 11400000, guid: 5d4883896aa0b5146966a880184eb941, type: 2}
224227
--- !u!65 &7209396890379076767
225228
BoxCollider:
226229
m_ObjectHideFlags: 0

Red Strike/Assets/InputController/InputController.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
using BuildingPlacement;
55
using UISystem;
66
using System.Linq;
7+
using UnityEngine.EventSystems;
8+
using UnityEngine.UIElements;
79

810
namespace InputController
911
{
1012
public class InputController : MonoBehaviour
1113
{
14+
public UIDocument gameUIDocument;
15+
1216
public LayerMask terrainLayer;
1317
public LayerMask selectableLayer;
1418
private Camera mainCamera;
@@ -36,6 +40,11 @@ private void Update()
3640

3741
if (Input.GetMouseButtonDown(0))
3842
{
43+
if (IsPointerOverUI())
44+
{
45+
return;
46+
}
47+
3948
if (selectedBuilding != null)
4049
{
4150
PlaceBuilding();
@@ -47,6 +56,21 @@ private void Update()
4756
}
4857
}
4958

59+
private bool IsPointerOverUI()
60+
{
61+
if (gameUIDocument == null) return false;
62+
63+
Vector2 mousePosition = Input.mousePosition;
64+
Vector2 pointerPosition = new Vector2(mousePosition.x, Screen.height - mousePosition.y);
65+
VisualElement pickedElement = gameUIDocument.rootVisualElement.panel.Pick(pointerPosition);
66+
67+
if (pickedElement == null) return false;
68+
if (pickedElement.name == "root-container") return false;
69+
if (pickedElement == gameUIDocument.rootVisualElement) return false;
70+
71+
return true;
72+
}
73+
5074
private void PlaceBuilding()
5175
{
5276
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);

Red Strike/Assets/Scenes/SampleScene.unity

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,85 @@ NavMeshSettings:
119119
debug:
120120
m_Flags: 0
121121
m_NavMeshData: {fileID: 0}
122+
--- !u!1 &117385832
123+
GameObject:
124+
m_ObjectHideFlags: 0
125+
m_CorrespondingSourceObject: {fileID: 0}
126+
m_PrefabInstance: {fileID: 0}
127+
m_PrefabAsset: {fileID: 0}
128+
serializedVersion: 6
129+
m_Component:
130+
- component: {fileID: 117385835}
131+
- component: {fileID: 117385834}
132+
- component: {fileID: 117385833}
133+
m_Layer: 0
134+
m_Name: EventSystem
135+
m_TagString: Untagged
136+
m_Icon: {fileID: 0}
137+
m_NavMeshLayer: 0
138+
m_StaticEditorFlags: 0
139+
m_IsActive: 1
140+
--- !u!114 &117385833
141+
MonoBehaviour:
142+
m_ObjectHideFlags: 0
143+
m_CorrespondingSourceObject: {fileID: 0}
144+
m_PrefabInstance: {fileID: 0}
145+
m_PrefabAsset: {fileID: 0}
146+
m_GameObject: {fileID: 117385832}
147+
m_Enabled: 1
148+
m_EditorHideFlags: 0
149+
m_Script: {fileID: 11500000, guid: 01614664b831546d2ae94a42149d80ac, type: 3}
150+
m_Name:
151+
m_EditorClassIdentifier:
152+
m_SendPointerHoverToParent: 1
153+
m_MoveRepeatDelay: 0.5
154+
m_MoveRepeatRate: 0.1
155+
m_XRTrackingOrigin: {fileID: 0}
156+
m_ActionsAsset: {fileID: -944628639613478452, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
157+
m_PointAction: {fileID: -1654692200621890270, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
158+
m_MoveAction: {fileID: -8784545083839296357, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
159+
m_SubmitAction: {fileID: 392368643174621059, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
160+
m_CancelAction: {fileID: 7727032971491509709, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
161+
m_LeftClickAction: {fileID: 3001919216989983466, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
162+
m_MiddleClickAction: {fileID: -2185481485913320682, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
163+
m_RightClickAction: {fileID: -4090225696740746782, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
164+
m_ScrollWheelAction: {fileID: 6240969308177333660, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
165+
m_TrackedDevicePositionAction: {fileID: 6564999863303420839, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
166+
m_TrackedDeviceOrientationAction: {fileID: 7970375526676320489, guid: ca9f5fa95ffab41fb9a615ab714db018, type: 3}
167+
m_DeselectOnBackgroundClick: 1
168+
m_PointerBehavior: 0
169+
m_CursorLockBehavior: 0
170+
m_ScrollDeltaPerTick: 6
171+
--- !u!114 &117385834
172+
MonoBehaviour:
173+
m_ObjectHideFlags: 0
174+
m_CorrespondingSourceObject: {fileID: 0}
175+
m_PrefabInstance: {fileID: 0}
176+
m_PrefabAsset: {fileID: 0}
177+
m_GameObject: {fileID: 117385832}
178+
m_Enabled: 1
179+
m_EditorHideFlags: 0
180+
m_Script: {fileID: 11500000, guid: 76c392e42b5098c458856cdf6ecaaaa1, type: 3}
181+
m_Name:
182+
m_EditorClassIdentifier:
183+
m_FirstSelected: {fileID: 0}
184+
m_sendNavigationEvents: 1
185+
m_DragThreshold: 10
186+
--- !u!4 &117385835
187+
Transform:
188+
m_ObjectHideFlags: 0
189+
m_CorrespondingSourceObject: {fileID: 0}
190+
m_PrefabInstance: {fileID: 0}
191+
m_PrefabAsset: {fileID: 0}
192+
m_GameObject: {fileID: 117385832}
193+
serializedVersion: 2
194+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
195+
m_LocalPosition: {x: 0, y: 0, z: 0}
196+
m_LocalScale: {x: 1, y: 1, z: 1}
197+
m_ConstrainProportionsScale: 0
198+
m_Children: []
199+
m_Father: {fileID: 0}
200+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
122201
--- !u!1 &330585543
123202
GameObject:
124203
m_ObjectHideFlags: 0
@@ -864,6 +943,7 @@ MonoBehaviour:
864943
m_Script: {fileID: 11500000, guid: 768c93e16b4ed8f46a9cbb09dc7237ad, type: 3}
865944
m_Name:
866945
m_EditorClassIdentifier:
946+
gameUIDocument: {fileID: 422005011}
867947
terrainLayer:
868948
serializedVersion: 2
869949
m_Bits: 8
@@ -951,3 +1031,4 @@ SceneRoots:
9511031
- {fileID: 1556403731}
9521032
- {fileID: 422005012}
9531033
- {fileID: 891073697}
1034+
- {fileID: 117385835}

Red Strike/Assets/UISystem/BuildingHUDController.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using UnityEngine;
22
using UnityEngine.UIElements;
33
using BuildingPlacement.Buildings;
4+
using VehicleSystem;
45

56
namespace UISystem
67
{
@@ -11,22 +12,19 @@ public class BuildingHUDController : GameHUDController
1112
public VisualTreeAsset hangarTemplate;
1213
public VisualTreeAsset energyTowerTemplate;
1314

14-
private VisualElement buildingDetailsPanel;
15+
protected VisualElement buildingDetailsPanel;
1516
private Label sharedBuildTypeLabel;
1617
private Label sharedHealthLabel;
1718

18-
// Main Station
1919
private Label msShieldLabel;
2020

21-
// Hangar
2221
private Label hIsReadyLabel;
2322
private Label hProductionLabel;
2423

25-
// Energy Tower
2624
private Label etCapacityLabel;
2725
private Label etDensityLabel;
2826

29-
private Building currentlySelectedBuilding;
27+
protected Building currentlySelectedBuilding;
3028

3129
protected override void OnEnable()
3230
{
@@ -40,9 +38,9 @@ protected override void OnEnable()
4038
var hangarBtn = root.Q<Button>("hangar-button");
4139
var energyTowerBtn = root.Q<Button>("energy-tower-button");
4240

43-
if(mainStationBtn != null) mainStationBtn.clicked += () => OnBuildingButtonClicked("Main Station");
44-
if(hangarBtn != null) hangarBtn.clicked += () => OnBuildingButtonClicked("Hangar");
45-
if(energyTowerBtn != null) energyTowerBtn.clicked += () => OnBuildingButtonClicked("Energy Tower");
41+
if(mainStationBtn != null) mainStationBtn.clicked += () => OnBuildingCategoryClicked("Main Station");
42+
if(hangarBtn != null) hangarBtn.clicked += () => OnBuildingCategoryClicked("Hangar");
43+
if(energyTowerBtn != null) energyTowerBtn.clicked += () => OnBuildingCategoryClicked("Energy Tower");
4644

4745
HideBuildingDetails();
4846
}
@@ -62,7 +60,6 @@ public void ShowBuildingDetails(Building building)
6260
if (building == null) return;
6361

6462
currentlySelectedBuilding = building;
65-
6663
sharedBuildTypeLabel.text = building.BuildingName;
6764

6865
buildingDynamicContentContainer.Clear();
@@ -72,11 +69,14 @@ public void ShowBuildingDetails(Building building)
7269
InstantiateTemplate(mainStationTemplate);
7370
msShieldLabel = buildingDynamicContentContainer.Q<Label>("shield-label");
7471
}
75-
else if (building is Hangar)
72+
else if (building is Hangar hangarScript)
7673
{
7774
InstantiateTemplate(hangarTemplate);
75+
7876
hIsReadyLabel = buildingDynamicContentContainer.Q<Label>("is-ready-label");
7977
hProductionLabel = buildingDynamicContentContainer.Q<Label>("in-production-label");
78+
79+
SetupHangarButtons(hangarScript);
8080
}
8181
else if (building is EnergyTower)
8282
{
@@ -97,6 +97,8 @@ public void HideBuildingDetails()
9797

9898
private void UpdateBuildingData()
9999
{
100+
if (currentlySelectedBuilding == null) return;
101+
100102
sharedHealthLabel.text = $"Health: {currentlySelectedBuilding.CurrentHealth:F0}";
101103

102104
if (currentlySelectedBuilding is MainStation ms)
@@ -109,7 +111,7 @@ private void UpdateBuildingData()
109111
if (hIsReadyLabel != null)
110112
hIsReadyLabel.text = $"Is Ready: {hg.IsReady}";
111113
if (hProductionLabel != null)
112-
hProductionLabel.text = $"Prod: {hg.InProductionUnitName}";
114+
hProductionLabel.text = $"Prod: {hg.InProductionUnitName}";
113115
}
114116
else if (currentlySelectedBuilding is EnergyTower et)
115117
{
@@ -120,19 +122,40 @@ private void UpdateBuildingData()
120122
}
121123
}
122124

125+
private void SetupHangarButtons(Hangar hangar)
126+
{
127+
void BindVehicleBtn(string btnName, VehicleTypes type)
128+
{
129+
Button btn = buildingDynamicContentContainer.Q<Button>(btnName);
130+
131+
if (btn != null)
132+
{
133+
btn.clicked += () =>
134+
{
135+
hangar.CreateVehicle(type);
136+
};
137+
}
138+
}
139+
140+
BindVehicleBtn("infantry-create-button", VehicleTypes.Infantry);
141+
BindVehicleBtn("trike-create-button", VehicleTypes.Trike);
142+
BindVehicleBtn("quad-create-button", VehicleTypes.Quad);
143+
BindVehicleBtn("tank-combat-create-button", VehicleTypes.Tank_Combat);
144+
BindVehicleBtn("tank-heavy-a-create-button", VehicleTypes.Tank_Heavy_A);
145+
BindVehicleBtn("tank-heavy-b-create-button", VehicleTypes.Tank_Heavy_B);
146+
BindVehicleBtn("ornithopter-a-create-button", VehicleTypes.Ornithopter_A);
147+
BindVehicleBtn("ornithopter-b-create-button", VehicleTypes.Ornithopter_B);
148+
}
149+
123150
private void InstantiateTemplate(VisualTreeAsset template)
124151
{
125152
if (template != null)
126153
{
127154
template.CloneTree(buildingDynamicContentContainer);
128155
}
129-
else
130-
{
131-
Debug.LogWarning("İstenen yapı için UXML Template atanmamış!");
132-
}
133156
}
134157

135-
private void OnBuildingButtonClicked(string buildingName)
158+
private void OnBuildingCategoryClicked(string buildingName)
136159
{
137160
if (inputController != null)
138161
inputController.SelectBuildingToPlace(buildingName);
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
22
<Style src="project://database/Assets/UISystem/GameHUDStyles.uss?fileID=7433441132597879392&amp;guid=76481a45a761bb147acf1059658d7ddd&amp;type=3#GameHUDStyles" />
3-
<ui:VisualElement name="root-container">
4-
<ui:VisualElement name="energy-tower-content" class="dynamic-content">
5-
<ui:Label text="Capacity:" name="capacity-label" class="details-text" />
6-
<ui:Label text="Density:" name="density-label" class="details-text" />
7-
</ui:VisualElement>
3+
<ui:VisualElement name="energy-tower-content-area">
4+
<ui:Label text="Capacity:" name="capacity-label" class="details-text" />
5+
<ui:Label text="Density:" name="density-label" class="details-text" />
86
</ui:VisualElement>
97
</ui:UXML>

Red Strike/Assets/UISystem/GameHUDStyles.uss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@
6060
background-color: rgba(120, 120, 120, 1);
6161
}
6262

63+
.action-button {
64+
width: 200px;
65+
height: 45px;
66+
background-color: rgba(60,60,60,0.9);
67+
color: white;
68+
border-width: 0;
69+
border-radius: 4px;
70+
margin-bottom: 10px;
71+
font-size: 14px;
72+
}
73+
6374
/* TEXT STYLES --------------------------------------------------- */
6475

6576
.details-title {

0 commit comments

Comments
 (0)