change: Switched the state controller and scene controller to a singleton class

This doesnt' change any of the logic, but simplifies a lot of the main
game loop code.

Many things still rely on the singleton class, but shouldn't so this
will be fixed in a later commit
This commit is contained in:
Nicholas Novak
2023-05-04 02:19:51 -07:00
parent afbcb920ac
commit 5a7ce8fb2b
8 changed files with 301 additions and 211 deletions

View File

@@ -65,8 +65,8 @@ public class PlayerMovement : MonoBehaviour
[Header("Layers & Tags")]
[SerializeField] private LayerMask _groundLayer;
// Static classes
[HideInInspector] private PlayerBehavior playerBehavior;
[HideInInspector] private StateController stateController;
[HideInInspector] private AudioSource audioSource;
[HideInInspector] private bool soundPlaying = false;
@@ -79,7 +79,6 @@ public class PlayerMovement : MonoBehaviour
RB = GetComponent<Rigidbody2D>();
playerBehavior = this.gameObject.GetComponent<PlayerBehavior>();
grapplingRope = playerBehavior.grapplingRope;
stateController = GameObject.FindGameObjectWithTag("StateController").GetComponent<StateController>();
audioSource = GetComponent<AudioSource>();
gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent<GameUIController>();
trumpetSprite.enabled = false;
@@ -93,16 +92,20 @@ public class PlayerMovement : MonoBehaviour
void OnMove(InputValue value)
{
if (playerBehavior.playerIsAlive) {
if (playerBehavior.playerIsAlive)
{
this._moveInput = value.Get<Vector2>();
} else {
}
else
{
this._moveInput = Vector2.zero;
}
}
void OnJump()
{
if (playerBehavior.playerIsAlive) {
if (playerBehavior.playerIsAlive)
{
OnJumpInput();
}
}
@@ -141,21 +144,30 @@ public class PlayerMovement : MonoBehaviour
if (IsGrounded()) //checks if set box overlaps with ground
{
LastOnGroundTime = Data.coyoteTime; //if so sets the lastGrounded to coyoteTime
if (unlockedTrumpet) {
if (unlockedTrumpet)
{
trumpet = 2;
gameUI.ToggleTrumpet(true);
} else {
}
else
{
trumpet = -1;
}
wasGrappling = false;
isRegFalling = false;
} else {
}
else
{
// print("not jumping");
if(!_isJumpFalling && !isRegFalling) {
if(unlockedTrumpet) {
if (!_isJumpFalling && !isRegFalling)
{
if (unlockedTrumpet)
{
trumpet = 1;
gameUI.ToggleTrumpet(true);
} else {
}
else
{
trumpet = -1;
}
isRegFalling = true;
@@ -208,7 +220,7 @@ public class PlayerMovement : MonoBehaviour
_isJumpCut = false;
_isJumpFalling = false;
Jump();
// determine if trumpet jump
if (!IsGrounded() && in_range && trumpet > 0)
{
@@ -223,14 +235,16 @@ public class PlayerMovement : MonoBehaviour
trumpet -= 1;
}
// check if double jump, play sound
if (trumpet == 0) {
if (trumpet == 0)
{
StartCoroutine(ActivateTrumpetSprite());
gameObject.transform.Find("Trumpet").GetComponent<AudioSource>().Play();
}
}
// stop sound if needed
if (soundPlaying && (isRegFalling || IsJumping || _isJumpFalling)) {
if (soundPlaying && (isRegFalling || IsJumping || _isJumpFalling))
{
print("footsteps stop");
audioSource.Stop();
soundPlaying = false;
@@ -252,7 +266,8 @@ public class PlayerMovement : MonoBehaviour
#region GRAPPLE CHECKS
// set wasGrappling to true if the player starts grappling
if (grapplingRope.isGrappling) {
if (grapplingRope.isGrappling)
{
wasGrappling = true;
}
#endregion
@@ -303,13 +318,17 @@ public class PlayerMovement : MonoBehaviour
#endregion
#region SOUND CHECKS
if (!IsJumping && !_isJumpFalling && !isRegFalling && _moveInput.x != 0) {
if (!soundPlaying) {
if (!IsJumping && !_isJumpFalling && !isRegFalling && _moveInput.x != 0)
{
if (!soundPlaying)
{
// print("footsteps PLAY");
audioSource.Play();
soundPlaying = true;
}
} else if (soundPlaying && audioSource.clip.name == "footsteps") {
}
else if (soundPlaying && audioSource.clip.name == "footsteps")
{
// print("footsteps stop");
audioSource.Stop();
soundPlaying = false;
@@ -317,7 +336,8 @@ public class PlayerMovement : MonoBehaviour
#endregion
#region UPDATE UI
if (trumpet == 0) {
if (trumpet == 0)
{
gameUI.ToggleTrumpet(false);
}
#endregion
@@ -371,12 +391,16 @@ public class PlayerMovement : MonoBehaviour
//Gets an acceleration value based on if we are accelerating (includes turning)
//or trying to decelerate (stop). As well as applying a multiplier if we're air borne.
if (LastOnGroundTime > 0) {
if (LastOnGroundTime > 0)
{
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount : Data.runDeccelAmount;
}
else if (wasGrappling) {
else if (wasGrappling)
{
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount * Data.accelInAir : Data.runDeccelAmount * (Data.deccelInAir / 5);
} else {
}
else
{
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount * Data.accelInAir : Data.runDeccelAmount * Data.deccelInAir;
}
#endregion
@@ -410,10 +434,10 @@ public class PlayerMovement : MonoBehaviour
RB.AddForce(movement * Vector2.right, ForceMode2D.Force);
/*
* For those interested here is what AddForce() will do
* RB.velocity = new Vector2(RB.velocity.x + (Time.fixedDeltaTime * speedDif * accelRate) / RB.mass, RB.velocity.y);
* Time.fixedDeltaTime is by default in Unity 0.02 seconds equal to 50 FixedUpdate() calls per second
*/
* For those interested here is what AddForce() will do
* RB.velocity = new Vector2(RB.velocity.x + (Time.fixedDeltaTime * speedDif * accelRate) / RB.mass, RB.velocity.y);
* Time.fixedDeltaTime is by default in Unity 0.02 seconds equal to 50 FixedUpdate() calls per second
*/
}
private void Turn()
@@ -547,8 +571,10 @@ public class PlayerMovement : MonoBehaviour
#endregion
#region ADDITIONAL TRUMPET METHODS
IEnumerator ActivateTrumpetSprite() {
if (!trumpetActive) {
IEnumerator ActivateTrumpetSprite()
{
if (!trumpetActive)
{
trumpetActive = true;
trumpetSprite.enabled = true;
yield return new WaitForSeconds(.5f);