Skip to content

Commit f5430d1

Browse files
committed
Implement energy tower-vehicle connection and refueling logic
Refactored EnergyTower to manage connected vehicles, capacity, and energy transfer. Updated AirVehicle and GroundVehicle to request and receive energy from towers, disconnect when refueling is complete or tower is empty, and improved nearest tower selection logic. Adjusted UI to display current tower status. Tweaked vehicle and asset parameters for more accurate refueling and movement behavior.
1 parent d9a7985 commit f5430d1

File tree

10 files changed

+251
-46
lines changed

10 files changed

+251
-46
lines changed
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,78 @@
11
using UnityEngine;
2-
using BuildingPlacement;
2+
using System.Collections.Generic;
3+
using VehicleSystem.Vehicles;
34

45
namespace BuildingPlacement.Buildings
56
{
67
public class EnergyTower : Building
78
{
8-
// TEST
9-
public float CurrentCapacity = 500f;
10-
public float Density = 0.8f;
9+
[Header("Energy Tower Settings")]
10+
public float maxCapacity = 500f;
11+
public float currentCapacity = 0f;
12+
public int maxDensity = 3;
13+
public float rechargeRate = 10f;
14+
public bool isActive = true;
15+
16+
private List<Vehicle> connectedVehicles = new List<Vehicle>();
17+
18+
private void Start()
19+
{
20+
currentCapacity = maxCapacity;
21+
}
22+
23+
private void Update()
24+
{
25+
if (!isActive) return;
26+
27+
if (currentCapacity < maxCapacity)
28+
{
29+
currentCapacity += rechargeRate * Time.deltaTime;
30+
if (currentCapacity > maxCapacity) currentCapacity = maxCapacity;
31+
}
32+
}
33+
34+
public bool IsAvailable()
35+
{
36+
return connectedVehicles.Count < maxDensity && isActive && currentCapacity > 5f;
37+
}
38+
39+
public float GiveEnergy(float requestedAmount)
40+
{
41+
if (!isActive || currentCapacity <= 0) return 0f;
42+
43+
float amountToGive = Mathf.Min(requestedAmount, currentCapacity);
44+
currentCapacity -= amountToGive;
45+
46+
return amountToGive;
47+
}
48+
49+
public bool NewVehicleConnected(Vehicle vehicle)
50+
{
51+
if (connectedVehicles.Contains(vehicle))
52+
{
53+
return true;
54+
}
55+
56+
if (connectedVehicles.Count < maxDensity)
57+
{
58+
connectedVehicles.Add(vehicle);
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
65+
public void VehicleDisconnected(Vehicle vehicle)
66+
{
67+
if (connectedVehicles.Contains(vehicle))
68+
{
69+
connectedVehicles.Remove(vehicle);
70+
}
71+
}
72+
73+
public (float current, float max, int count, int limit) GetStatus()
74+
{
75+
return (currentCapacity, maxCapacity, connectedVehicles.Count, maxDensity);
76+
}
1177
}
12-
}
78+
}

Red Strike/Assets/Scenes/GameScene.unity

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,79 @@ Transform:
12101210
m_Children: []
12111211
m_Father: {fileID: 0}
12121212
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1213+
--- !u!1001 &1927937557
1214+
PrefabInstance:
1215+
m_ObjectHideFlags: 0
1216+
serializedVersion: 2
1217+
m_Modification:
1218+
serializedVersion: 3
1219+
m_TransformParent: {fileID: 0}
1220+
m_Modifications:
1221+
- target: {fileID: 5848915433920635072, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1222+
propertyPath: m_Name
1223+
value: OrnithopterB
1224+
objectReference: {fileID: 0}
1225+
- target: {fileID: 9172212943852637613, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1226+
propertyPath: bankSpeed
1227+
value: 30
1228+
objectReference: {fileID: 0}
1229+
- target: {fileID: 9172212943852637613, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1230+
propertyPath: loiterRadius
1231+
value: 40
1232+
objectReference: {fileID: 0}
1233+
- target: {fileID: 9172212943852637613, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1234+
propertyPath: altitudeVariance
1235+
value: 6
1236+
objectReference: {fileID: 0}
1237+
- target: {fileID: 9172212943852637613, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1238+
propertyPath: cruisingAltitude
1239+
value: 50
1240+
objectReference: {fileID: 0}
1241+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1242+
propertyPath: m_LocalPosition.x
1243+
value: 3.2209816
1244+
objectReference: {fileID: 0}
1245+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1246+
propertyPath: m_LocalPosition.y
1247+
value: 0.000015258789
1248+
objectReference: {fileID: 0}
1249+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1250+
propertyPath: m_LocalPosition.z
1251+
value: -59.82359
1252+
objectReference: {fileID: 0}
1253+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1254+
propertyPath: m_LocalRotation.w
1255+
value: 1
1256+
objectReference: {fileID: 0}
1257+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1258+
propertyPath: m_LocalRotation.x
1259+
value: 0
1260+
objectReference: {fileID: 0}
1261+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1262+
propertyPath: m_LocalRotation.y
1263+
value: 0
1264+
objectReference: {fileID: 0}
1265+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1266+
propertyPath: m_LocalRotation.z
1267+
value: 0
1268+
objectReference: {fileID: 0}
1269+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1270+
propertyPath: m_LocalEulerAnglesHint.x
1271+
value: 0
1272+
objectReference: {fileID: 0}
1273+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1274+
propertyPath: m_LocalEulerAnglesHint.y
1275+
value: 0
1276+
objectReference: {fileID: 0}
1277+
- target: {fileID: 9215832435080281461, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
1278+
propertyPath: m_LocalEulerAnglesHint.z
1279+
value: 0
1280+
objectReference: {fileID: 0}
1281+
m_RemovedComponents: []
1282+
m_RemovedGameObjects: []
1283+
m_AddedGameObjects: []
1284+
m_AddedComponents: []
1285+
m_SourcePrefab: {fileID: 100100000, guid: cf72a38b114e423489886ad4e9090c4e, type: 3}
12131286
--- !u!1660057539 &9223372036854775807
12141287
SceneRoots:
12151288
m_ObjectHideFlags: 0
@@ -1227,3 +1300,4 @@ SceneRoots:
12271300
- {fileID: 496563313}
12281301
- {fileID: 127145441}
12291302
- {fileID: 1062271675}
1303+
- {fileID: 1927937557}

Red Strike/Assets/UISystem/BuildingHUDController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ private void UpdateBuildingData()
116116
else if (currentlySelectedBuilding is EnergyTower et)
117117
{
118118
if (etCapacityLabel != null)
119-
etCapacityLabel.text = $"Capacity: {et.CurrentCapacity}";
119+
etCapacityLabel.text = $"Capacity: {et.GetStatus().current:F0} / {et.GetStatus().max:F0}";
120120
if (etDensityLabel != null)
121-
etDensityLabel.text = $"Density: {et.Density}";
121+
etDensityLabel.text = $"Density: {et.GetStatus().count:F0} / {et.GetStatus().limit:F0}";
122122
}
123123
}
124124

Red Strike/Assets/VehicleSystem/Vehicles/AirVehicle.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected override void Update()
6262
currentState = AirState.Refueling;
6363
currentRefuelStage = RefuelStage.Calculating;
6464
}
65-
65+
6666
HandleRefueling();
6767
return;
6868
}
@@ -74,7 +74,7 @@ protected override void Update()
7474
case AirState.Engaging: HandleEngaging(); break;
7575
case AirState.Attacking: HandleAttacking(); break;
7676
}
77-
77+
7878
isMoving = true;
7979
ConsumeFuel();
8080
}
@@ -86,7 +86,11 @@ private void HandleRefueling()
8686
if (nearestEnergyTower == null)
8787
{
8888
FindNearestEnergyTower();
89-
if (nearestEnergyTower == null) return;
89+
90+
if (nearestEnergyTower == null)
91+
{
92+
return;
93+
}
9094
}
9195

9296
switch (currentRefuelStage)
@@ -146,7 +150,7 @@ private void ExecuteGlide()
146150
{
147151
// A) Hedefe doğrudan bakış (Burun aşağı)
148152
Quaternion diveRotation = Quaternion.LookRotation(vectorToLanding);
149-
153+
150154
// B) Ufka düz bakış (Yere paralel)
151155
Quaternion flatRotation = Quaternion.LookRotation(flatDirection);
152156

@@ -162,7 +166,7 @@ private void ExecuteGlide()
162166
// Yumuşak geçiş uygula
163167
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * 3f);
164168
}
165-
169+
166170
currentBankAngleZ = Mathf.Lerp(currentBankAngleZ, 0, Time.deltaTime * bankSpeed);
167171

168172
// 3. Yere temas kontrolü
@@ -179,28 +183,39 @@ private void ExecuteRefuel()
179183
Vector3 euler = transform.rotation.eulerAngles;
180184
transform.rotation = Quaternion.Euler(0, euler.y, 0);
181185

182-
fuelLevel += vehicleData.fuelCapacity * 0.2f * Time.deltaTime;
186+
float requestedAmount = vehicleData.fuelCapacity * 0.2f * Time.deltaTime; // TODO: Ayarlanabilir, ScriptableObject'tan çekilebilir
183187

184-
if (fuelLevel >= vehicleData.fuelCapacity)
188+
float receivedAmount = 0f;
189+
if (targetTowerScript != null)
185190
{
186-
fuelLevel = vehicleData.fuelCapacity;
191+
receivedAmount = targetTowerScript.GiveEnergy(requestedAmount);
192+
}
193+
194+
fuelLevel += receivedAmount;
195+
196+
if (fuelLevel >= vehicleData.fuelCapacity || (requestedAmount > 0 && receivedAmount <= 0))
197+
{
198+
fuelLevel = Mathf.Min(fuelLevel, vehicleData.fuelCapacity);
187199
isRefueling = false;
188-
nearestEnergyTower = null;
200+
DisconnectFromTower();
189201

190202
currentState = AirState.TakingOff;
191203

192-
Vector3 takeoffDir = (transform.position - nearestEnergyTower.transform.position).normalized;
193-
Vector3 takeoffPoint = transform.position + (takeoffDir * 60f); // İleri
194-
takeoffPoint.y = cruisingAltitude; // Ve yukarı
195-
204+
Vector3 takeoffDir = transform.forward;
205+
206+
Vector3 takeoffPoint = transform.position + (takeoffDir * 60f);
207+
takeoffPoint.y = cruisingAltitude;
208+
196209
loiterCenterPoint = new Vector3(transform.position.x, cruisingAltitude, transform.position.z);
210+
211+
currentRefuelStage = RefuelStage.Calculating;
197212
}
198213
}
199214

200215
private void HandleTakingOff()
201216
{
202217
Vector3 takeoffTarget = transform.position + (transform.forward * 50f) + (Vector3.up * 20f);
203-
218+
204219
if (takeoffTarget.y > cruisingAltitude) takeoffTarget.y = cruisingAltitude;
205220

206221
MoveAndLook(takeoffTarget, 0.5f);

Red Strike/Assets/VehicleSystem/Vehicles/GroundVehicle.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ private void HandleRefuelingState()
7474
if (nearestEnergyTower == null)
7575
{
7676
FindNearestEnergyTower();
77-
7877
if (nearestEnergyTower == null)
7978
{
8079
agent.isStopped = true;
@@ -87,7 +86,8 @@ private void HandleRefuelingState()
8786
{
8887
agent.isStopped = false;
8988

90-
if (Vector3.Distance(agent.destination, nearestEnergyTower.transform.position) > 1.0f)
89+
// Kuleye doğru git
90+
if (Vector3.Distance(agent.destination, nearestEnergyTower.transform.position) > 2.0f)
9191
{
9292
agent.SetDestination(nearestEnergyTower.transform.position);
9393
}
@@ -97,7 +97,9 @@ private void HandleRefuelingState()
9797

9898
float distToTower = Vector3.Distance(transform.position, nearestEnergyTower.transform.position);
9999

100-
if (distToTower <= 10.0f)
100+
// DÜZELTME: Mesafeyi artırdık (10 -> 20).
101+
// Çünkü ilk araç kuleyi kapatırsa ikinci araç biraz uzakta kalsa bile dolum yapabilsin.
102+
if (distToTower <= 20.0f)
101103
{
102104
Refuel();
103105
}
@@ -109,13 +111,29 @@ private void Refuel()
109111
isMoving = false;
110112
agent.isStopped = true;
111113

112-
fuelLevel += vehicleData.fuelCapacity * 0.2f * Time.deltaTime;
114+
// 1. İstenen miktar
115+
float requestedAmount = vehicleData.fuelCapacity * 0.2f * Time.deltaTime;
116+
117+
// 2. Kuleden al
118+
float receivedAmount = 0f;
119+
if (targetTowerScript != null)
120+
{
121+
receivedAmount = targetTowerScript.GiveEnergy(requestedAmount);
122+
}
123+
124+
// 3. Depoya ekle
125+
fuelLevel += receivedAmount;
113126

114-
if (fuelLevel >= vehicleData.fuelCapacity)
127+
// 4. Çıkış Koşulları:
128+
// - Depo dolduysa (fuelLevel >= Capacity)
129+
// - VEYA Kule boşaldıysa (istedik ama 0 aldık: requested > 0 && received <= 0)
130+
if (fuelLevel >= vehicleData.fuelCapacity || (requestedAmount > 0 && receivedAmount <= 0))
115131
{
116-
fuelLevel = vehicleData.fuelCapacity;
132+
fuelLevel = Mathf.Min(fuelLevel, vehicleData.fuelCapacity);
133+
117134
isRefueling = false;
118-
nearestEnergyTower = null;
135+
DisconnectFromTower(); // Kule kaydını sil
136+
119137
agent.ResetPath();
120138
}
121139
}

Red Strike/Assets/VehicleSystem/Vehicles/Infantry/Infantry.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MonoBehaviour:
2121
stoppingDistance: 15
2222
maxHealth: 100
2323
fuelCapacity: 100
24-
fuelConsumptionRate: 0.7
24+
fuelConsumptionRate: 0.8
2525
ammunitionSettings:
2626
- isEnabled: 1
2727
ammunitionType: 0

Red Strike/Assets/VehicleSystem/Vehicles/OrnithopterA/OrnithopterA.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MonoBehaviour:
2121
stoppingDistance: 15
2222
maxHealth: 100
2323
fuelCapacity: 100
24-
fuelConsumptionRate: 20
24+
fuelConsumptionRate: 2
2525
ammunitionSettings:
2626
- isEnabled: 1
2727
ammunitionType: 0

Red Strike/Assets/VehicleSystem/Vehicles/OrnithopterB/OrnithopterB.prefab

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34938,15 +34938,15 @@ MonoBehaviour:
3493834938
muzzleFlashLights:
3493934939
- {fileID: 7471699319632300459}
3494034940
- {fileID: 3441073963759958482}
34941-
cruisingAltitude: 35
34942-
attackAltitude: 7
34941+
cruisingAltitude: 75
34942+
attackAltitude: 2.4
3494334943
attackLobeRadius: 50
3494434944
rocketRangeMultiplier: 3
34945-
loiterRadius: 40
34945+
loiterRadius: 117
3494634946
maxBankAngle: 100
34947-
bankSpeed: 30
34948-
parameterVariance: 0.1
34949-
altitudeVariance: 5
34947+
bankSpeed: 260
34948+
parameterVariance: 0.12
34949+
altitudeVariance: 11
3495034950
barrelPoints:
3495134951
- {fileID: 5423563311206911032}
3495234952
- {fileID: 3703916012389026853}

Red Strike/Assets/VehicleSystem/Vehicles/Trike/Trike.asset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ MonoBehaviour:
1818
speed: 20
1919
coverageAreaRadius: 20
2020
turnSpeed: 20
21-
stoppingDistance: 5
21+
stoppingDistance: 20
2222
maxHealth: 100
2323
fuelCapacity: 100
2424
fuelConsumptionRate: 0.7
2525
ammunitionSettings:
2626
- isEnabled: 1
2727
ammunitionType: 0
2828
maxAmmunition: 30
29-
reloadTime: 1
29+
reloadTime: 0.17
3030
ammunition: {fileID: 11400000, guid: 22882af161684a743a3776628e0fe427, type: 2}

0 commit comments

Comments
 (0)