boosts
This commit is contained in:
1 parent
716da3b146
commit
fc20f6e197
7 files changed
+258
-98
No files matched your search
+106
-66
@@ -1,16 +1,15 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
public class ShipControls : MonoBehaviour {
|
||||
public Camera aim;
|
||||
public Timing timing;
|
||||
public PowerUnit powerUnit = new();
|
||||
|
||||
SpaceRaceInput input;
|
||||
InputAction lookAction;
|
||||
InputAction accelerateAction;
|
||||
InputAction decelerateAction;
|
||||
|
||||
Rigidbody rigidBody;
|
||||
|
||||
@@ -22,12 +21,16 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
Vector3 thrust;
|
||||
Vector3 strafe;
|
||||
|
||||
|
||||
// Camera Controls
|
||||
float camPitch;
|
||||
float camYaw;
|
||||
float clampAngle = 90.0f;
|
||||
|
||||
public PowerUnit powerUnit = new();
|
||||
List<IEnumerator> boostLifecycles = new();
|
||||
int boostDurationSeconds = 2;
|
||||
int boostCooldownSeconds = 5;
|
||||
|
||||
void Start() {
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
rigidBody = GetComponent<Rigidbody>();
|
||||
@@ -47,23 +50,10 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
camYaw = initialAim.eulerAngles.y;
|
||||
forceMode = ForceMode.Acceleration;
|
||||
|
||||
if (input == null) {
|
||||
input = new SpaceRaceInput();
|
||||
input.ship.SetCallbacks(this);
|
||||
}
|
||||
input.ship.Enable();
|
||||
|
||||
lookAction = input.FindAction("look");
|
||||
accelerateAction = input.FindAction("accelerate");
|
||||
decelerateAction = input.FindAction("decelerate");
|
||||
|
||||
SetupActions();
|
||||
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() {
|
||||
@@ -76,9 +66,8 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
// needs to be done here in fixedUpdate because otherwise the input is checked on every frame
|
||||
// so they will have already hit the button before this tick and they actually start moving
|
||||
// aka ripoff
|
||||
bool shouldStartTimer = !timing.timerEnabled && (thrust != Vector3.zero || strafe != Vector3.zero);
|
||||
if (shouldStartTimer)
|
||||
timing.StartTimer();
|
||||
bool shouldBegin = !timing.timerEnabled && (thrust != Vector3.zero || strafe != Vector3.zero);
|
||||
if (shouldBegin) Begin();
|
||||
|
||||
// the ship being propelled forward
|
||||
// this is in world space because of the xform from the camera pos
|
||||
@@ -98,7 +87,7 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
if (Quaternion.Angle(aim.transform.rotation, rigidBody.rotation) > 1f)
|
||||
rigidBody.rotation = Quaternion.RotateTowards(transform.rotation, aim.transform.rotation, step);
|
||||
|
||||
powerUnit.ShieldsRegenerate();
|
||||
powerUnit.ShieldRegenerate();
|
||||
}
|
||||
|
||||
void UpdateCamera() {
|
||||
@@ -110,25 +99,20 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
// delta *= 0.1f; // Account for sensitivity setting on old Mouse X and Y axes.
|
||||
// def needs to be configurable
|
||||
|
||||
Vector2 lookDelta = lookAction.ReadValue<Vector2>();
|
||||
Vector2 lookDelta = input.ship.look.ReadValue<Vector2>();
|
||||
camYaw += lookDelta.x;
|
||||
camPitch -= lookDelta.y;
|
||||
camPitch = Mathf.Clamp(camPitch, -clampAngle, clampAngle);
|
||||
aim.transform.eulerAngles = new Vector3(camPitch, camYaw, 0.0f);
|
||||
}
|
||||
|
||||
public void OnStrafe(InputAction.CallbackContext ctx) {
|
||||
Vector2 str = ctx.ReadValue<Vector2>();
|
||||
strafe = new Vector3(str.x, str.y, 0f) * powerUnit.Strafing;
|
||||
}
|
||||
|
||||
// if you are decelerating you can't accelerate as well
|
||||
// otherwise you can flip the ship around and
|
||||
// decelerate + accelerate against the velocity vector
|
||||
// aka cheating?
|
||||
Vector3 getThrust() {
|
||||
float accelerateInput = accelerateAction.ReadValue<float>();
|
||||
float decelerateInput = decelerateAction.ReadValue<float>();
|
||||
float accelerateInput = input.ship.accelerate.ReadValue<float>();
|
||||
float decelerateInput = input.ship.decelerate.ReadValue<float>();
|
||||
|
||||
// decelerates the ship against the direction it is moving
|
||||
if (decelerateInput > 0f) {
|
||||
@@ -144,14 +128,6 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
public void OnQuit(InputAction.CallbackContext ctx) {
|
||||
Application.Quit();
|
||||
}
|
||||
|
||||
public void OnRespawn(InputAction.CallbackContext ctx) {
|
||||
Respawn();
|
||||
}
|
||||
|
||||
public void Respawn() {
|
||||
// IMPORTANT
|
||||
// stop the timer before we move the ship or anything
|
||||
@@ -170,9 +146,99 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
camPitch = initialAim.eulerAngles.x;
|
||||
camYaw = initialAim.eulerAngles.y;
|
||||
|
||||
foreach (var cd in boostLifecycles) {
|
||||
StopCoroutine(cd);
|
||||
}
|
||||
|
||||
powerUnit.Init();
|
||||
}
|
||||
|
||||
// stuff we have to do right as they come off the line
|
||||
void Begin() {
|
||||
timing.StartTimer();
|
||||
|
||||
// start with 0 cooldowns and regen them
|
||||
var cd = BoostCdLifecycle();
|
||||
boostLifecycles.Add(cd);
|
||||
StartCoroutine(cd);
|
||||
}
|
||||
|
||||
public float Speed() {
|
||||
return rigidBody.velocity.magnitude;
|
||||
}
|
||||
|
||||
public void Damage(float amt, string src) {
|
||||
Debug.Log($"Damage {amt} from {src}");
|
||||
|
||||
bool destroyed = powerUnit.ShieldReduce(amt);
|
||||
if (destroyed) {
|
||||
Debug.Log($"Ddddestroyed by {src}");
|
||||
Respawn();
|
||||
}
|
||||
}
|
||||
|
||||
void BoostUse(Output output) {
|
||||
bool used = powerUnit.BoostUse(output);
|
||||
if (!used) return;
|
||||
|
||||
var boost = BoostLifecycle(output);
|
||||
boostLifecycles.Add(boost);
|
||||
StartCoroutine(boost);
|
||||
}
|
||||
|
||||
void BoostEnd(Output output) {
|
||||
powerUnit.BoostEnd(output);
|
||||
var cd = BoostCdLifecycle();
|
||||
boostLifecycles.Add(cd);
|
||||
StartCoroutine(cd);
|
||||
}
|
||||
|
||||
IEnumerator BoostLifecycle(Output output) {
|
||||
yield return new WaitForSeconds(boostDurationSeconds);
|
||||
BoostEnd(output);
|
||||
}
|
||||
|
||||
IEnumerator BoostCdLifecycle() {
|
||||
yield return new WaitForSeconds(boostCooldownSeconds);
|
||||
powerUnit.BoostRegenerate();
|
||||
}
|
||||
|
||||
// I prefer doing it this way than registering the callbacks etc
|
||||
// the control of the actions is more granular
|
||||
// plus the callbacks don't have to be public, bonus
|
||||
void SetupActions() {
|
||||
if (input == null)
|
||||
input = new SpaceRaceInput();
|
||||
|
||||
input.ship.Enable();
|
||||
|
||||
// we have to update strafe the whole time it's being pressed
|
||||
// including setting it back to zero when released
|
||||
input.ship.strafe.started += OnStrafe;
|
||||
input.ship.strafe.performed += OnStrafe;
|
||||
input.ship.strafe.canceled += OnStrafe;
|
||||
|
||||
// these we only care about the initial press
|
||||
input.ship.respawn.started += OnRespawn;
|
||||
input.ship.quit.started += OnQuit;
|
||||
input.ship.powerUnit1.started += OnPowerUnit1;
|
||||
input.ship.powerUnit2.started += OnPowerUnit2;
|
||||
input.ship.powerUnit3.started += OnPowerUnit3;
|
||||
input.ship.powerUnit4.started += OnPowerUnit4;
|
||||
}
|
||||
|
||||
void OnStrafe(InputAction.CallbackContext ctx) {
|
||||
Vector2 str = ctx.ReadValue<Vector2>();
|
||||
strafe = new Vector3(str.x, str.y, 0f) * powerUnit.Strafing;
|
||||
}
|
||||
|
||||
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 OnDrawGizmosSelected() {
|
||||
if (Application.isPlaying == false)
|
||||
return;
|
||||
@@ -196,30 +262,4 @@ public class ShipControls : MonoBehaviour, SpaceRaceInput.IShipActions {
|
||||
Gizmos.color = Color.cyan;
|
||||
Gizmos.DrawRay(transform.position, thrust);
|
||||
}
|
||||
|
||||
public float Speed() {
|
||||
return rigidBody.velocity.magnitude;
|
||||
}
|
||||
|
||||
public void Damage(float amt, string src) {
|
||||
Debug.Log($"Damage {amt} from {src}");
|
||||
|
||||
bool destroyed = powerUnit.ShieldsReduce(amt);
|
||||
if (destroyed) {
|
||||
Debug.Log($"Ddddestroyed by {src}");
|
||||
Respawn();
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
public void OnAccelerate(InputAction.CallbackContext ctx) { }
|
||||
public void OnDecelerate(InputAction.CallbackContext ctx) { }
|
||||
public void OnLook(InputAction.CallbackContext ctx) { }
|
||||
|
||||
public void OnPowerUnit1 (InputAction.CallbackContext ctx) => powerUnit.Reassign(2f, 4f, 1f);
|
||||
public void OnPowerUnit2 (InputAction.CallbackContext ctx) => powerUnit.Reassign(2f, 4f, 3f);
|
||||
public void OnPowerUnit4 (InputAction.CallbackContext ctx) => powerUnit.Reassign(2f, 5f, 0f);
|
||||
public void OnPowerUnit3 (InputAction.CallbackContext ctx) => powerUnit.Reassign(1f, 3f, 3f);
|
||||
}
|
||||
Reference in new issue
Block a user