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

No files matched your search

+18 -35
View File
@@ -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<Rigidbody>();
@@ -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<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
@@ -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