From ccd8e2c9230a6365617c86837df79a26b42518ff Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 13 Aug 2023 17:09:16 +1000 Subject: [PATCH] boost cooldowns --- Assets/Scenes/testTrack/TestTrack.unity | 47 +++++++++++++++++++- Assets/Src/PowerUnit.cs | 25 ++++++----- Assets/Src/ShipControls.cs | 59 ++++++++++++++----------- 3 files changed, 91 insertions(+), 40 deletions(-) diff --git a/Assets/Scenes/testTrack/TestTrack.unity b/Assets/Scenes/testTrack/TestTrack.unity index 2c7d2a8..a5e6fde 100644 --- a/Assets/Scenes/testTrack/TestTrack.unity +++ b/Assets/Scenes/testTrack/TestTrack.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.61784047, g: 0.63485193, b: 0.64917755, a: 1} + m_IndirectSpecularColor: {r: 0.61786336, g: 0.6344, b: 0.64918613, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -328,6 +328,18 @@ PrefabInstance: propertyPath: timingDoc value: objectReference: {fileID: 1643546429} + - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} + propertyPath: controlsUI + value: + objectReference: {fileID: 1628786601} + - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} + propertyPath: controlsDoc + value: + objectReference: {fileID: 1628786602} + - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} + propertyPath: crosshairDoc + value: + objectReference: {fileID: 1582941309} - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} propertyPath: shipControls value: @@ -1293,6 +1305,39 @@ Transform: m_CorrespondingSourceObject: {fileID: 7588376663361902483, guid: 73ab56de058af21389c312279181f856, type: 3} m_PrefabInstance: {fileID: 1549479480} m_PrefabAsset: {fileID: 0} +--- !u!114 &1582941309 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 8483217137784193517, guid: 217f9c8f922f2d41cac52c3699bde478, type: 3} + m_PrefabInstance: {fileID: 702758806} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 19102, guid: 0000000000000000e000000000000000, type: 0} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1628786601 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 3792430562793951787, guid: 217f9c8f922f2d41cac52c3699bde478, type: 3} + m_PrefabInstance: {fileID: 702758806} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8401c141ee093632d847505914412cee, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1628786602 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 7583115105922956764, guid: 217f9c8f922f2d41cac52c3699bde478, type: 3} + m_PrefabInstance: {fileID: 702758806} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 19102, guid: 0000000000000000e000000000000000, type: 0} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1643546425 stripped GameObject: m_CorrespondingSourceObject: {fileID: 2603084029878042569, guid: dcb64e3a91f1fec7ab6098f4f407830b, type: 3} diff --git a/Assets/Src/PowerUnit.cs b/Assets/Src/PowerUnit.cs index 8d04568..0f23194 100644 --- a/Assets/Src/PowerUnit.cs +++ b/Assets/Src/PowerUnit.cs @@ -24,7 +24,7 @@ public class PowerUnit { float shieldRegen = 10f; // * 100 ticks per second = 5s fully recharge int boostMax = 2; - int boostCharges = 2; + int boostCharges = 0; static float stablisation = 75f; @@ -37,9 +37,10 @@ public class PowerUnit { public string ShieldPct => $"{((shieldCurrent / shieldMax) * 100f):0}"; public string BoostsAvailable => $"{boostCharges}"; + public bool BoostRecharging => boostCharges < boostMax; public void Init() { - boostCharges = boostMax; + boostCharges = 0; shieldCurrent = initialShieldMax; acceleration = initialAcceleration; deceleration = initialDeceleration; @@ -48,20 +49,20 @@ public class PowerUnit { // Regeneration of boosts is tracked by the ShipControls // it has to handle tracking the coroutines etc - public void BoostRegenerate() { + public void BoostGenerate() { boostCharges = Math.Min(boostCharges + 1, boostMax); Debug.Log($"Boosts available {boostCharges}"); } - public bool BoostUse(Output output) { + public bool BoostDeploy(Output output) { if (boostCharges < 1) return false; float newOutput = output switch { - Output.Acceleration => BoostUseAcceleration(), - Output.Deceleration => BoostUseDeceleration(), - Output.Strafing => BoostUseStrafing(), - Output.Shield => BoostUseShields(), + Output.Acceleration => BoostDeployAcceleration(), + Output.Deceleration => BoostDeployDeceleration(), + Output.Strafing => BoostDeployStrafing(), + Output.Shield => BoostDeployShields(), _ => 0f, }; @@ -73,10 +74,10 @@ public class PowerUnit { return true; } - float BoostUseAcceleration() => acceleration += baseOutput; - float BoostUseDeceleration() => deceleration += baseOutput; - float BoostUseStrafing() => strafing += baseOutput; - float BoostUseShields() { + float BoostDeployAcceleration() => acceleration += baseOutput; + float BoostDeployDeceleration() => deceleration += baseOutput; + float BoostDeployStrafing() => strafing += baseOutput; + float BoostDeployShields() { float shieldFrac = shieldMax / shieldCurrent; shieldMax = 7000f; shieldCurrent = shieldMax * shieldFrac; diff --git a/Assets/Src/ShipControls.cs b/Assets/Src/ShipControls.cs index cb2c441..6b23aba 100755 --- a/Assets/Src/ShipControls.cs +++ b/Assets/Src/ShipControls.cs @@ -27,7 +27,8 @@ public class ShipControls : MonoBehaviour { float clampAngle = 90.0f; public PowerUnit powerUnit = new(); - List 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)