From a5b25de90a601db1cd283910da0263700f526ccf Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 9 Aug 2023 17:23:19 +1000 Subject: [PATCH] pu class --- Assets/Src/PowerUnit.cs | 46 +++++++++++++++++++++++++++++++ Assets/Src/PowerUnit.cs.meta | 11 ++++++++ Assets/Src/ShipControls.cs | 53 ++++++++++++------------------------ Assets/Src/UI/Controls.uxml | 6 ++++ Assets/Src/UI/ControlsUI.cs | 17 +++++++++++- 5 files changed, 97 insertions(+), 36 deletions(-) create mode 100644 Assets/Src/PowerUnit.cs create mode 100644 Assets/Src/PowerUnit.cs.meta diff --git a/Assets/Src/PowerUnit.cs b/Assets/Src/PowerUnit.cs new file mode 100644 index 0000000..6d3e4e7 --- /dev/null +++ b/Assets/Src/PowerUnit.cs @@ -0,0 +1,46 @@ +using System; +using UnityEngine; + +public class PowerUnit { + static float baseOutput = 25.0f; + + float acceleration = 2f * baseOutput; + float deceleration = 4f * baseOutput; + float strafing = 1f * baseOutput; + float stablisation = 75f; + + float shieldsCurrent = 5000f; + float shieldsMax = 5000f; + float shieldsRegen = 10f; // * 100 ticks per second = 5s fully recharge + + public float Acceleration => acceleration; + public float Deceleration => deceleration; + public float Strafing => strafing; + public float Shields => shieldsCurrent; + public float ShieldsMax => shieldsMax; + public float Stabilisation => stablisation; + + public string ShieldsPct => $"{((shieldsCurrent / shieldsMax) * 100f):0}"; + + public void Init() { + shieldsCurrent = shieldsMax; + shieldsRegen = (shieldsMax / 5f) * Time.fixedDeltaTime; + } + + public void Reassign(float accel, float decel, float strafe, float shields) { + acceleration = baseOutput * accel; + deceleration = baseOutput * decel; + strafing = baseOutput * strafe; + shieldsRegen = shields; + } + + // returns whether shields depleted + public bool ShieldsReduce(float amt) { + shieldsCurrent = shieldsCurrent - amt; + return shieldsCurrent < 0f; + } + + public void ShieldsRegenerate() { + shieldsCurrent = Math.Min(shieldsCurrent + shieldsRegen, shieldsMax); + } +} \ No newline at end of file diff --git a/Assets/Src/PowerUnit.cs.meta b/Assets/Src/PowerUnit.cs.meta new file mode 100644 index 0000000..c6222ce --- /dev/null +++ b/Assets/Src/PowerUnit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 96b9da5f0e24936cfa2870bdc7f63c64 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Src/ShipControls.cs b/Assets/Src/ShipControls.cs index c782101..d3e351a 100755 --- a/Assets/Src/ShipControls.cs +++ b/Assets/Src/ShipControls.cs @@ -5,6 +5,7 @@ using UnityEngine.InputSystem; public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { public Camera aim; public Timing timing; + public PowerUnit powerUnit = new(); SpaceRaceInput input; InputAction lookAction; @@ -17,25 +18,16 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { Vector3 initialForce; Quaternion initialAim; + ForceMode forceMode; Vector3 thrust; Vector3 strafe; - float strafeMag; - float accelerateMag; - float decelerateMag; - float stabliseMag; - ForceMode forceMode; // Camera Controls float camPitch; float camYaw; float clampAngle = 90.0f; - // Power Unit / Shields - float shields = 5000f; - float shieldsMax = 5000f; - float shieldsRegen = 10f; // * 100 ticks per second = 5s fully recharge - void Start() { Cursor.lockState = CursorLockMode.Locked; rigidBody = GetComponent(); @@ -51,11 +43,6 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { thrust = Vector3.zero; strafe = Vector3.zero; - accelerateMag = 50.0f; - decelerateMag = 100.0f; // do not ask how, there is no reason - strafeMag = 25.0f; - stabliseMag = 75.0f; - camPitch = initialAim.eulerAngles.x; camYaw = initialAim.eulerAngles.y; forceMode = ForceMode.Acceleration; @@ -69,20 +56,19 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { lookAction = input.FindAction("look"); accelerateAction = input.FindAction("accelerate"); decelerateAction = input.FindAction("decelerate"); + + Respawn(); } void OnDestroy() { input.ship.RemoveCallbacks(this); } + // to minimise input lag we poll for the thrust vector every frame and set it + // same with the camera, we always look around void Update() { - // to minimise input lag we poll for the thrust vector every frame and set it - thrust = getThrust(); - - // if (rigidBody.velocity.magnitude < 0.5f) rigidBody.velocity = Vector3.zero; - - // same with the camera, we always look around UpdateCamera(); + thrust = getThrust(); } void FixedUpdate() { @@ -95,21 +81,24 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { timing.StartTimer(); // the ship being propelled forward + // this is in world space because of the xform from the camera pos + // rigidBody.AddForce(thrust, forceMode); // moving side to side + // this is relative because it's easy to just multiply by the strafe vector rigidBody.AddRelativeForce(strafe, forceMode); // rotating towards the camera's orientation // not being done by a force because of strange behaviour // needs investigation - float step = stabliseMag * Time.fixedDeltaTime; + float step = powerUnit.Stabilisation * Time.fixedDeltaTime; // stop the ship jiggling because of micro camera movement if (Quaternion.Angle(aim.transform.rotation, rigidBody.rotation) > 1f) rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, aim.transform.rotation, step); - RegenerateShields(); + powerUnit.ShieldsRegenerate(); } void UpdateCamera() { @@ -130,7 +119,7 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { public void OnStrafe(InputAction.CallbackContext ctx) { Vector2 str = ctx.ReadValue(); - strafe = new Vector3(str.x, str.y, 0f) * strafeMag; + strafe = new Vector3(str.x, str.y, 0f) * powerUnit.Strafing; } // if you are decelerating you can't accelerate as well @@ -144,13 +133,13 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { // decelerates the ship against the direction it is moving if (decelerateInput > 0f) { // the velocity is already in world space so we just invert it - return rigidBody.velocity.normalized * -1f * decelerateMag * decelerateInput; + return rigidBody.velocity.normalized * -1f * powerUnit.Deceleration * decelerateInput; } // thrust in the direction of the camera if (accelerateInput > 0f) // transform from the camera's forward vector into world space - return aim.transform.TransformDirection(Vector3.forward) * accelerateMag * accelerateInput; + return aim.transform.TransformDirection(Vector3.forward) * powerUnit.Acceleration * accelerateInput; return Vector3.zero; } @@ -181,7 +170,7 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { camPitch = initialAim.eulerAngles.x; camYaw = initialAim.eulerAngles.y; - shields = shieldsMax; + powerUnit.Init(); } void OnDrawGizmosSelected() { @@ -212,22 +201,16 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { return rigidBody.velocity.magnitude; } - public string Shields => $"{((shields / shieldsMax) * 100f):0}"; - public void Damage(float amt, string src) { Debug.Log($"Damage {amt} from {src}"); - shields = shields - amt; - if (shields < 0f) { + bool destroyed = powerUnit.ShieldsReduce(amt); + if (destroyed) { Debug.Log($"Ddddestroyed by {src}"); Respawn(); } } - void RegenerateShields() { - shields = Math.Min(shields + shieldsRegen, shieldsMax); - } - // We don't use these because we poll instead // the callbacks are only invoked when the value changes // aka if you hold accelerate it doesn't do what you expect diff --git a/Assets/Src/UI/Controls.uxml b/Assets/Src/UI/Controls.uxml index 3ad8c06..35456fa 100644 --- a/Assets/Src/UI/Controls.uxml +++ b/Assets/Src/UI/Controls.uxml @@ -11,6 +11,12 @@ + + + + + + diff --git a/Assets/Src/UI/ControlsUI.cs b/Assets/Src/UI/ControlsUI.cs index ae28898..04b63c4 100644 --- a/Assets/Src/UI/ControlsUI.cs +++ b/Assets/Src/UI/ControlsUI.cs @@ -12,6 +12,10 @@ public class ControlsUI : MonoBehaviour { UIDocument uiDocument; Label speed; Label shields; + Label puAccel; + Label puDecel; + Label puStrafe; + Label puShield; void OnEnable() { Init(); @@ -21,6 +25,12 @@ public class ControlsUI : MonoBehaviour { uiDocument = GetComponent(); speed = uiDocument.rootVisualElement.Q