boost cooldowns

This commit is contained in:
ntr committed 2023-08-13 17:09:16 +10:00
1 parent e10e034702
commit ccd8e2c923
3 files changed
+91 -40

No files matched your search

+32 -27
View File
@@ -27,7 +27,8 @@ public class ShipControls : MonoBehaviour {
float clampAngle = 90.0f;
public PowerUnit powerUnit = new();
List<IEnumerator> boostLifecycles = new();
IEnumerator boostDuration;
IEnumerator boostCooldown;
int boostDurationSeconds = 2;
int boostCooldownSeconds = 5;
@@ -146,9 +147,8 @@ public class ShipControls : MonoBehaviour {
camPitch = initialAim.eulerAngles.x;
camYaw = initialAim.eulerAngles.y;
foreach (var cd in boostLifecycles) {
StopCoroutine(cd);
}
if (boostCooldown != null) StopCoroutine(boostCooldown);
if (boostDuration != null) StopCoroutine(boostDuration);
powerUnit.Init();
}
@@ -158,9 +158,8 @@ public class ShipControls : MonoBehaviour {
timing.StartTimer();
// start with 0 cooldowns and regen them
var cd = BoostCdLifecycle();
boostLifecycles.Add(cd);
StartCoroutine(cd);
boostCooldown = BoostCooldown();
StartCoroutine(boostCooldown);
}
public float Speed() {
@@ -177,30 +176,36 @@ public class ShipControls : MonoBehaviour {
}
}
void BoostUse(Output output) {
bool used = powerUnit.BoostUse(output);
void BoostDeploy(Output output) {
bool used = powerUnit.BoostDeploy(output);
if (!used) return;
var boost = BoostLifecycle(output);
boostLifecycles.Add(boost);
StartCoroutine(boost);
// stops the cooldown until after our boost finishes
if (boostCooldown != null) StopCoroutine(boostCooldown);
boostDuration = BoostDuration(output);
StartCoroutine(boostDuration);
}
void BoostEnd(Output output) {
powerUnit.BoostEnd(output);
var cd = BoostCdLifecycle();
boostLifecycles.Add(cd);
StartCoroutine(cd);
}
IEnumerator BoostLifecycle(Output output) {
IEnumerator BoostDuration(Output output) {
Debug.Log($"BoostDuration {output}...");
yield return new WaitForSeconds(boostDurationSeconds);
BoostEnd(output);
}
IEnumerator BoostCdLifecycle() {
yield return new WaitForSeconds(boostCooldownSeconds);
powerUnit.BoostRegenerate();
void BoostEnd(Output output) {
Debug.Log($"BoostEnd {output}...");
powerUnit.BoostEnd(output);
if (boostCooldown != null) StopCoroutine(boostCooldown);
boostCooldown = BoostCooldown();
StartCoroutine(boostCooldown);
}
IEnumerator BoostCooldown() {
while (powerUnit.BoostRecharging) {
yield return new WaitForSeconds(boostCooldownSeconds);
powerUnit.BoostGenerate();
}
}
// I prefer doing it this way than registering the callbacks etc
@@ -234,10 +239,10 @@ public class ShipControls : MonoBehaviour {
void OnQuit(InputAction.CallbackContext ctx) => Application.Quit();
void OnRespawn(InputAction.CallbackContext ctx) => Respawn();
void OnPowerUnit1 (InputAction.CallbackContext ctx) => BoostUse(Output.Acceleration);
void OnPowerUnit2 (InputAction.CallbackContext ctx) => BoostUse(Output.Deceleration);
void OnPowerUnit3 (InputAction.CallbackContext ctx) => BoostUse(Output.Strafing);
void OnPowerUnit4 (InputAction.CallbackContext ctx) => BoostUse(Output.Shield);
void OnPowerUnit1 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Acceleration);
void OnPowerUnit2 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Deceleration);
void OnPowerUnit3 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Strafing);
void OnPowerUnit4 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Shield);
void OnDrawGizmosSelected() {
if (Application.isPlaying == false)