boost rework wip
This commit is contained in:
1 parent
46d742dfa8
commit
fddf8552c2
3 files changed
+62
-116
No files matched your search
+50
-70
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public enum Output {
|
public enum Output {
|
||||||
@@ -9,6 +11,10 @@ public enum Output {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class PowerUnit {
|
public class PowerUnit {
|
||||||
|
static float stablisation = 75f;
|
||||||
|
public float Stabilisation => stablisation;
|
||||||
|
|
||||||
|
// Outputs
|
||||||
static float baseOutput = 25.0f;
|
static float baseOutput = 25.0f;
|
||||||
static float initialAcceleration = 2f * baseOutput;
|
static float initialAcceleration = 2f * baseOutput;
|
||||||
static float initialDeceleration = 4f * baseOutput;
|
static float initialDeceleration = 4f * baseOutput;
|
||||||
@@ -18,94 +24,71 @@ public class PowerUnit {
|
|||||||
float deceleration = initialDeceleration;
|
float deceleration = initialDeceleration;
|
||||||
float strafing = initialStrafing;
|
float strafing = initialStrafing;
|
||||||
|
|
||||||
|
// public float Acceleration => acceleration;
|
||||||
|
// public float Deceleration => deceleration;
|
||||||
|
// public float Strafing => strafing;
|
||||||
|
|
||||||
|
// Shields
|
||||||
static float initialShieldMax = 5000f;
|
static float initialShieldMax = 5000f;
|
||||||
float shieldCurrent = initialShieldMax;
|
float shieldCurrent = initialShieldMax;
|
||||||
float shieldMax = initialShieldMax;
|
float shieldMax = initialShieldMax;
|
||||||
float shieldRegen = 10f; // * 100 ticks per second = 5s fully recharge
|
float shieldRegen = 10f; // * 100 ticks per second = 5s fully recharge
|
||||||
|
|
||||||
int boostMax = 2;
|
|
||||||
int boostCharges = 0;
|
|
||||||
|
|
||||||
static float stablisation = 75f;
|
|
||||||
|
|
||||||
public float Acceleration => acceleration;
|
|
||||||
public float Deceleration => deceleration;
|
|
||||||
public float Strafing => strafing;
|
|
||||||
public float Shield => shieldCurrent;
|
public float Shield => shieldCurrent;
|
||||||
public float ShieldMax => shieldMax;
|
public float ShieldMax => shieldMax;
|
||||||
public float Stabilisation => stablisation;
|
|
||||||
|
|
||||||
public string ShieldPct => $"{((shieldCurrent / shieldMax) * 100f):0}";
|
public string ShieldPct => $"{((shieldCurrent / shieldMax) * 100f):0}";
|
||||||
public string BoostsAvailable => $"{boostCharges}";
|
float ShieldRegen => (shieldMax / 5f) * Time.fixedDeltaTime;
|
||||||
public bool BoostRecharging => boostCharges < boostMax;
|
|
||||||
|
|
||||||
public void Init() {
|
// Boost
|
||||||
boostCharges = 0;
|
List<(Output output, double expiresAt)> boostActive = new();
|
||||||
|
static double boostFullChargeSeconds = 10f;
|
||||||
|
static double boostDurationSeconds = 2f;
|
||||||
|
double boostMax = 2f * boostDurationSeconds;
|
||||||
|
double boostAvailable = 0f;
|
||||||
|
|
||||||
|
// public string BoostAvailable => $"{boostAvailable}";
|
||||||
|
public string BoostAvailable => $"{((boostAvailable / boostMax) * 100f):0}";
|
||||||
|
|
||||||
|
public float Acceleration =>
|
||||||
|
acceleration + boostActive.Count(b => b.output == Output.Acceleration) * baseOutput;
|
||||||
|
|
||||||
|
public float Deceleration =>
|
||||||
|
deceleration + boostActive.Count(b => b.output == Output.Deceleration) * baseOutput;
|
||||||
|
|
||||||
|
public float Strafing =>
|
||||||
|
strafing + boostActive.Count(b => b.output == Output.Strafing) * baseOutput;
|
||||||
|
|
||||||
|
double BoostRegen => (boostMax / boostFullChargeSeconds) * Time.fixedDeltaTime;
|
||||||
|
|
||||||
|
public void Reset() {
|
||||||
shieldCurrent = initialShieldMax;
|
shieldCurrent = initialShieldMax;
|
||||||
acceleration = initialAcceleration;
|
acceleration = initialAcceleration;
|
||||||
deceleration = initialDeceleration;
|
deceleration = initialDeceleration;
|
||||||
strafing = initialStrafing;
|
strafing = initialStrafing;
|
||||||
|
|
||||||
|
boostActive = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regeneration of boosts is tracked by the ShipControls
|
public void Deploy(Output output) {
|
||||||
// it has to handle tracking the coroutines etc
|
boostActive.Add((output, Time.timeAsDouble + boostDurationSeconds));
|
||||||
public void BoostGenerate() {
|
|
||||||
boostCharges = Math.Min(boostCharges + 1, boostMax);
|
|
||||||
Debug.Log($"Boosts available {boostCharges}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool BoostDeploy(Output output) {
|
public void Tick() {
|
||||||
if (boostCharges < 1)
|
// remove expired boosts
|
||||||
return false;
|
boostActive = boostActive.Where(b => b.expiresAt > Time.timeAsDouble).ToList();
|
||||||
|
|
||||||
float newOutput = output switch {
|
double delta = boostActive.Count switch {
|
||||||
Output.Acceleration => BoostDeployAcceleration(),
|
// if there are no boosts active we can increase the store
|
||||||
Output.Deceleration => BoostDeployDeceleration(),
|
0 => BoostRegen,
|
||||||
Output.Strafing => BoostDeployStrafing(),
|
// if there are active boosts we deplete it
|
||||||
Output.Shield => BoostDeployShields(),
|
int count => count * Time.fixedDeltaTime,
|
||||||
_ => 0f,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Boosts are immediately used and tracked here
|
double next = Math.Min(boostAvailable + delta, boostMax);
|
||||||
boostCharges--;
|
boostAvailable = Math.Max(0f, next);
|
||||||
|
|
||||||
Debug.Log($"Set {output} to {newOutput}");
|
shieldMax = initialShieldMax + boostActive.Count(b => b.output == Output.Shield) * 1000f;
|
||||||
|
shieldCurrent = Math.Min(shieldCurrent + ShieldRegen, shieldMax);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
float BoostDeployAcceleration() => acceleration += baseOutput;
|
|
||||||
float BoostDeployDeceleration() => deceleration += baseOutput;
|
|
||||||
float BoostDeployStrafing() => strafing += baseOutput;
|
|
||||||
float BoostDeployShields() {
|
|
||||||
float shieldFrac = shieldMax / shieldCurrent;
|
|
||||||
shieldMax = 7000f;
|
|
||||||
shieldCurrent = shieldMax * shieldFrac;
|
|
||||||
ShieldRegenRecalculate();
|
|
||||||
return shieldMax;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void BoostEnd(Output output) {
|
|
||||||
float newOutput = output switch {
|
|
||||||
Output.Acceleration => BoostEndAcceleration(),
|
|
||||||
Output.Deceleration => BoostEndDeceleration(),
|
|
||||||
Output.Strafing => BoostEndStrafing(),
|
|
||||||
Output.Shield => BoostEndShields(),
|
|
||||||
_ => 0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
Debug.Log($"Set {output} to {newOutput}");
|
|
||||||
}
|
|
||||||
|
|
||||||
float BoostEndAcceleration() => acceleration -= baseOutput;
|
|
||||||
float BoostEndDeceleration() => deceleration -= baseOutput;
|
|
||||||
float BoostEndStrafing() => strafing -= baseOutput;
|
|
||||||
float BoostEndShields() {
|
|
||||||
float shieldFrac = shieldMax / shieldCurrent;
|
|
||||||
shieldMax = 5000f;
|
|
||||||
shieldCurrent = shieldMax * shieldFrac;
|
|
||||||
ShieldRegenRecalculate();
|
|
||||||
return shieldMax;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns whether shield depleted
|
// returns whether shield depleted
|
||||||
@@ -113,7 +96,4 @@ public class PowerUnit {
|
|||||||
shieldCurrent = shieldCurrent - amt;
|
shieldCurrent = shieldCurrent - amt;
|
||||||
return shieldCurrent < 0f;
|
return shieldCurrent < 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ShieldRegenerate() => shieldCurrent = Math.Min(shieldCurrent + shieldRegen, shieldMax);
|
|
||||||
void ShieldRegenRecalculate() => shieldRegen = (shieldMax / 5f) * Time.fixedDeltaTime;
|
|
||||||
}
|
}
|
||||||
+11
-45
@@ -27,8 +27,6 @@ public class ShipControls : MonoBehaviour {
|
|||||||
float clampAngle = 90.0f;
|
float clampAngle = 90.0f;
|
||||||
|
|
||||||
public PowerUnit powerUnit = new();
|
public PowerUnit powerUnit = new();
|
||||||
IEnumerator boostDuration;
|
|
||||||
IEnumerator boostCooldown;
|
|
||||||
int boostDurationSeconds = 2;
|
int boostDurationSeconds = 2;
|
||||||
int boostCooldownSeconds = 5;
|
int boostCooldownSeconds = 5;
|
||||||
|
|
||||||
@@ -88,7 +86,9 @@ public class ShipControls : MonoBehaviour {
|
|||||||
if (Quaternion.Angle(aim.transform.rotation, rigidBody.rotation) > 1f)
|
if (Quaternion.Angle(aim.transform.rotation, rigidBody.rotation) > 1f)
|
||||||
rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, aim.transform.rotation, step);
|
rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, aim.transform.rotation, step);
|
||||||
|
|
||||||
powerUnit.ShieldRegenerate();
|
if (timing.timerEnabled) {
|
||||||
|
powerUnit.Tick();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateCamera() {
|
void UpdateCamera() {
|
||||||
@@ -147,19 +147,12 @@ public class ShipControls : MonoBehaviour {
|
|||||||
camPitch = initialAim.eulerAngles.x;
|
camPitch = initialAim.eulerAngles.x;
|
||||||
camYaw = initialAim.eulerAngles.y;
|
camYaw = initialAim.eulerAngles.y;
|
||||||
|
|
||||||
if (boostCooldown != null) StopCoroutine(boostCooldown);
|
powerUnit.Reset();
|
||||||
if (boostDuration != null) StopCoroutine(boostDuration);
|
|
||||||
|
|
||||||
powerUnit.Init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// stuff we have to do right as they come off the line
|
// stuff we have to do right as they come off the line
|
||||||
void Begin() {
|
void Begin() {
|
||||||
timing.StartTimer();
|
timing.StartTimer();
|
||||||
|
|
||||||
// start with 0 cooldowns and regen them
|
|
||||||
boostCooldown = BoostCooldown();
|
|
||||||
StartCoroutine(boostCooldown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Speed() {
|
public float Speed() {
|
||||||
@@ -176,36 +169,8 @@ public class ShipControls : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoostDeploy(Output output) {
|
void Deploy(Output output) {
|
||||||
bool used = powerUnit.BoostDeploy(output);
|
powerUnit.Deploy(output);
|
||||||
if (!used) return;
|
|
||||||
|
|
||||||
// stops the cooldown until after our boost finishes
|
|
||||||
if (boostCooldown != null) StopCoroutine(boostCooldown);
|
|
||||||
boostDuration = BoostDuration(output);
|
|
||||||
StartCoroutine(boostDuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
IEnumerator BoostDuration(Output output) {
|
|
||||||
Debug.Log($"BoostDuration {output}...");
|
|
||||||
yield return new WaitForSeconds(boostDurationSeconds);
|
|
||||||
BoostEnd(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
// I prefer doing it this way than registering the callbacks etc
|
||||||
@@ -239,10 +204,11 @@ public class ShipControls : MonoBehaviour {
|
|||||||
|
|
||||||
void OnQuit(InputAction.CallbackContext ctx) => Application.Quit();
|
void OnQuit(InputAction.CallbackContext ctx) => Application.Quit();
|
||||||
void OnRespawn(InputAction.CallbackContext ctx) => Respawn();
|
void OnRespawn(InputAction.CallbackContext ctx) => Respawn();
|
||||||
void OnPowerUnit1 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Acceleration);
|
|
||||||
void OnPowerUnit2 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Deceleration);
|
void OnPowerUnit1(InputAction.CallbackContext ctx) => Deploy(Output.Acceleration);
|
||||||
void OnPowerUnit3 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Strafing);
|
void OnPowerUnit2(InputAction.CallbackContext ctx) => Deploy(Output.Deceleration);
|
||||||
void OnPowerUnit4 (InputAction.CallbackContext ctx) => BoostDeploy(Output.Shield);
|
void OnPowerUnit3(InputAction.CallbackContext ctx) => Deploy(Output.Strafing);
|
||||||
|
void OnPowerUnit4(InputAction.CallbackContext ctx) => Deploy(Output.Shield);
|
||||||
|
|
||||||
void OnDrawGizmosSelected() {
|
void OnDrawGizmosSelected() {
|
||||||
if (Application.isPlaying == false)
|
if (Application.isPlaying == false)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class ControlsUI : MonoBehaviour {
|
|||||||
speed.text = $"{ship.Speed():0}";
|
speed.text = $"{ship.Speed():0}";
|
||||||
shields.text = ship.powerUnit.ShieldPct;
|
shields.text = ship.powerUnit.ShieldPct;
|
||||||
|
|
||||||
boosts.text = ship.powerUnit.BoostsAvailable;
|
boosts.text = ship.powerUnit.BoostAvailable;
|
||||||
puAccel.text = ship.powerUnit.Acceleration.ToString();
|
puAccel.text = ship.powerUnit.Acceleration.ToString();
|
||||||
puDecel.text = ship.powerUnit.Deceleration.ToString();
|
puDecel.text = ship.powerUnit.Deceleration.ToString();
|
||||||
puStrafe.text = ship.powerUnit.Strafing.ToString();
|
puStrafe.text = ship.powerUnit.Strafing.ToString();
|
||||||
|
|||||||
Reference in new issue
Block a user