This commit is contained in:
ntr committed 2023-08-09 17:23:19 +10:00
1 parent 760d3fccb9
commit a5b25de90a
5 files changed
+96 -35

No files matched your search

+46
View File
@@ -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);
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 96b9da5f0e24936cfa2870bdc7f63c64
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+17 -34
View File
@@ -5,6 +5,7 @@ using UnityEngine.InputSystem;
public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions { public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
public Camera aim; public Camera aim;
public Timing timing; public Timing timing;
public PowerUnit powerUnit = new();
SpaceRaceInput input; SpaceRaceInput input;
InputAction lookAction; InputAction lookAction;
@@ -17,25 +18,16 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
Vector3 initialForce; Vector3 initialForce;
Quaternion initialAim; Quaternion initialAim;
ForceMode forceMode;
Vector3 thrust; Vector3 thrust;
Vector3 strafe; Vector3 strafe;
float strafeMag;
float accelerateMag;
float decelerateMag;
float stabliseMag;
ForceMode forceMode;
// Camera Controls // Camera Controls
float camPitch; float camPitch;
float camYaw; float camYaw;
float clampAngle = 90.0f; 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() { void Start() {
Cursor.lockState = CursorLockMode.Locked; Cursor.lockState = CursorLockMode.Locked;
rigidBody = GetComponent<Rigidbody>(); rigidBody = GetComponent<Rigidbody>();
@@ -51,11 +43,6 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
thrust = Vector3.zero; thrust = Vector3.zero;
strafe = 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; camPitch = initialAim.eulerAngles.x;
camYaw = initialAim.eulerAngles.y; camYaw = initialAim.eulerAngles.y;
forceMode = ForceMode.Acceleration; forceMode = ForceMode.Acceleration;
@@ -69,20 +56,19 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
lookAction = input.FindAction("look"); lookAction = input.FindAction("look");
accelerateAction = input.FindAction("accelerate"); accelerateAction = input.FindAction("accelerate");
decelerateAction = input.FindAction("decelerate"); decelerateAction = input.FindAction("decelerate");
Respawn();
} }
void OnDestroy() { void OnDestroy() {
input.ship.RemoveCallbacks(this); input.ship.RemoveCallbacks(this);
} }
void Update() {
// to minimise input lag we poll for the thrust vector every frame and set it // 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 // same with the camera, we always look around
void Update() {
UpdateCamera(); UpdateCamera();
thrust = getThrust();
} }
void FixedUpdate() { void FixedUpdate() {
@@ -95,21 +81,24 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
timing.StartTimer(); timing.StartTimer();
// the ship being propelled forward // the ship being propelled forward
// this is in world space because of the xform from the camera pos
//
rigidBody.AddForce(thrust, forceMode); rigidBody.AddForce(thrust, forceMode);
// moving side to side // moving side to side
// this is relative because it's easy to just multiply by the strafe vector
rigidBody.AddRelativeForce(strafe, forceMode); rigidBody.AddRelativeForce(strafe, forceMode);
// rotating towards the camera's orientation // rotating towards the camera's orientation
// not being done by a force because of strange behaviour // not being done by a force because of strange behaviour
// needs investigation // needs investigation
float step = stabliseMag * Time.fixedDeltaTime; float step = powerUnit.Stabilisation * Time.fixedDeltaTime;
// stop the ship jiggling because of micro camera movement // stop the ship jiggling because of micro camera movement
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);
RegenerateShields(); powerUnit.ShieldsRegenerate();
} }
void UpdateCamera() { void UpdateCamera() {
@@ -130,7 +119,7 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
public void OnStrafe(InputAction.CallbackContext ctx) { public void OnStrafe(InputAction.CallbackContext ctx) {
Vector2 str = ctx.ReadValue<Vector2>(); Vector2 str = ctx.ReadValue<Vector2>();
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 // 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 // decelerates the ship against the direction it is moving
if (decelerateInput > 0f) { if (decelerateInput > 0f) {
// the velocity is already in world space so we just invert it // 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 // thrust in the direction of the camera
if (accelerateInput > 0f) if (accelerateInput > 0f)
// transform from the camera's forward vector into world space // 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; return Vector3.zero;
} }
@@ -181,7 +170,7 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
camPitch = initialAim.eulerAngles.x; camPitch = initialAim.eulerAngles.x;
camYaw = initialAim.eulerAngles.y; camYaw = initialAim.eulerAngles.y;
shields = shieldsMax; powerUnit.Init();
} }
void OnDrawGizmosSelected() { void OnDrawGizmosSelected() {
@@ -212,22 +201,16 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
return rigidBody.velocity.magnitude; return rigidBody.velocity.magnitude;
} }
public string Shields => $"{((shields / shieldsMax) * 100f):0}";
public void Damage(float amt, string src) { public void Damage(float amt, string src) {
Debug.Log($"Damage {amt} from {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}"); Debug.Log($"Ddddestroyed by {src}");
Respawn(); Respawn();
} }
} }
void RegenerateShields() {
shields = Math.Min(shields + shieldsRegen, shieldsMax);
}
// We don't use these because we poll instead // We don't use these because we poll instead
// the callbacks are only invoked when the value changes // the callbacks are only invoked when the value changes
// aka if you hold accelerate it doesn't do what you expect // aka if you hold accelerate it doesn't do what you expect
+6
View File
@@ -11,6 +11,12 @@
<ui:Label tabindex="-1" display-tooltip-when-elided="true" name="shields" text="100" style="min-width: 5%; -unity-text-align: upper-right; flex-basis: 10%;" /> <ui:Label tabindex="-1" display-tooltip-when-elided="true" name="shields" text="100" style="min-width: 5%; -unity-text-align: upper-right; flex-basis: 10%;" />
<ui:Label tabindex="-1" text="%" display-tooltip-when-elided="true" name="units" style="flex-basis: 10%; -unity-text-align: upper-left;" /> <ui:Label tabindex="-1" text="%" display-tooltip-when-elided="true" name="units" style="flex-basis: 10%; -unity-text-align: upper-left;" />
</ui:VisualElement> </ui:VisualElement>
<ui:VisualElement name="puCtr" style="flex-direction: row;">
<ui:Label tabindex="-1" display-tooltip-when-elided="true" name="puAccel" text="100" />
<ui:Label tabindex="-1" text="100" display-tooltip-when-elided="true" name="puDecel" />
<ui:Label tabindex="-1" text="100" display-tooltip-when-elided="true" name="puStrafe" />
<ui:Label tabindex="-1" text="100" display-tooltip-when-elided="true" name="puShield" />
</ui:VisualElement>
<ui:VisualElement name="middleBottom" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-shrink: 0; flex-basis: 20%;" /> <ui:VisualElement name="middleBottom" style="flex-grow: 0; background-color: rgba(0, 0, 0, 0); flex-shrink: 0; flex-basis: 20%;" />
</ui:VisualElement> </ui:VisualElement>
<ui:VisualElement name="right" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); justify-content: center; align-items: center;" /> <ui:VisualElement name="right" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); justify-content: center; align-items: center;" />
+16 -1
View File
@@ -12,6 +12,10 @@ public class ControlsUI : MonoBehaviour {
UIDocument uiDocument; UIDocument uiDocument;
Label speed; Label speed;
Label shields; Label shields;
Label puAccel;
Label puDecel;
Label puStrafe;
Label puShield;
void OnEnable() { void OnEnable() {
Init(); Init();
@@ -21,6 +25,12 @@ public class ControlsUI : MonoBehaviour {
uiDocument = GetComponent<UIDocument>(); uiDocument = GetComponent<UIDocument>();
speed = uiDocument.rootVisualElement.Q<Label>("speed"); speed = uiDocument.rootVisualElement.Q<Label>("speed");
shields = uiDocument.rootVisualElement.Q<Label>("shields"); shields = uiDocument.rootVisualElement.Q<Label>("shields");
// PowerUnit
puAccel = uiDocument.rootVisualElement.Q<Label>("puAccel");
puDecel = uiDocument.rootVisualElement.Q<Label>("puDecel");
puStrafe = uiDocument.rootVisualElement.Q<Label>("puStrafe");
puShield = uiDocument.rootVisualElement.Q<Label>("puShield");
} }
void FixedUpdate() { void FixedUpdate() {
@@ -31,6 +41,11 @@ public class ControlsUI : MonoBehaviour {
} }
speed.text = $"{ship.Speed():0}"; speed.text = $"{ship.Speed():0}";
shields.text = ship.Shields; shields.text = ship.powerUnit.ShieldsPct;
puAccel.text = ship.powerUnit.Acceleration.ToString();
puDecel.text = ship.powerUnit.Deceleration.ToString();
puStrafe.text = ship.powerUnit.Strafing.ToString();
puShield.text = ship.powerUnit.ShieldsMax.ToString();
} }
} }