diff --git a/Assets/Scripts/DebugSceneSwitcher.cs b/Assets/Scripts/DebugSceneSwitcher.cs index c10511a..e45ccf3 100644 --- a/Assets/Scripts/DebugSceneSwitcher.cs +++ b/Assets/Scripts/DebugSceneSwitcher.cs @@ -6,26 +6,21 @@ using TMPro; public class DebugSceneSwitcher : MonoBehaviour { - - void Awake() { - if (!GameObject.Find("StateController").GetComponent().inDebugMode) { - Destroy(this.gameObject); - } - // check to see if a debug canvas already exists - if (GameObject.FindGameObjectWithTag("DebugCanvas") != null) { - Destroy(this.gameObject); - } else { // if it doesn't, then this is the only one - this.gameObject.tag = "DebugCanvas"; - } + void Awake() + { + // Keep the object around when we switch scenes DontDestroyOnLoad(this.gameObject); CreateDropdownOptions(); } - void CreateDropdownOptions() { + void CreateDropdownOptions() + { TMP_Dropdown sceneDropdown = GameObject.Find("SceneSwitcherDropdown").GetComponent(); - if (sceneDropdown.options.Count == 0) { + if (sceneDropdown.options.Count == 0) + { List sceneNames = new List(); - for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) { + for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) + { string newName = System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i)); print(newName); sceneNames.Add(newName); @@ -34,9 +29,10 @@ public class DebugSceneSwitcher : MonoBehaviour } } - public void ChangeScene(int index) { + public void ChangeScene(int index) + { // print(index); - GameObject.FindGameObjectWithTag("SceneManager").GetComponent().LoadChosenScene(index); this.gameObject.SetActive(false); + SceneController.Instance.LoadChosenScene(index); } } diff --git a/Assets/Scripts/DialogBoxes.cs b/Assets/Scripts/DialogBoxes.cs index 53e54f4..f31ca99 100644 --- a/Assets/Scripts/DialogBoxes.cs +++ b/Assets/Scripts/DialogBoxes.cs @@ -26,31 +26,40 @@ public class DialogBoxes : MonoBehaviour textField.text = dialogMessages[0]; backgroundImageObject.sprite = images[0]; } - - void OnAdvanceDialog() { + + void OnAdvanceDialog() + { messageCount += 1; - if (messageCount < dialogMessages.Length) { + if (messageCount < dialogMessages.Length) + { textField.text = dialogMessages[messageCount]; backgroundImageObject.sprite = images[messageCount]; - } else { // no more dialog messages, advance the scene - GameObject.Find("SceneManager").GetComponent().NextScene(); + } + else + { // no more dialog messages, advance the scene + SceneController.Instance.NextScene(); } } - void ReconfigureSpriteArray() { + void ReconfigureSpriteArray() + { // resize if need to - if (images.Length != dialogMessages.Length) { + if (images.Length != dialogMessages.Length) + { Sprite[] newImagesArray = new Sprite[dialogMessages.Length]; - for (int i = 0; i < newImagesArray.Length; i++) { + for (int i = 0; i < newImagesArray.Length; i++) + { newImagesArray[i] = images[i]; } images = newImagesArray; } // update empty slots - for (int i = 1; i < images.Length; i++) { - if (images[i] == null ) { - images[i] = images[i-1]; + for (int i = 1; i < images.Length; i++) + { + if (images[i] == null) + { + images[i] = images[i - 1]; } } diff --git a/Assets/Scripts/GameUIController.cs b/Assets/Scripts/GameUIController.cs index 76a87b1..3ab8252 100644 --- a/Assets/Scripts/GameUIController.cs +++ b/Assets/Scripts/GameUIController.cs @@ -14,64 +14,50 @@ public class GameUIController : MonoBehaviour [HideInInspector] public GameObject tambourineUI; [HideInInspector] public GameObject clarinetUI; - [Header("Other Objects")] - private StateController stateController; + void Start() + { + this.trumpetUI = trumpetBackground.transform.GetChild(0).gameObject; + this.tambourineUI = tambourineBackground.transform.GetChild(0).gameObject; + this.clarinetUI = clarinetBackground.transform.GetChild(0).gameObject; - void Start() { - stateController = GameObject.FindGameObjectWithTag("StateController").GetComponent(); - if (stateController == null) { - print("state controller not found"); - } else { - print("yeehaw"); - } - trumpetUI = trumpetBackground.transform.GetChild(0).gameObject; - tambourineUI = tambourineBackground.transform.GetChild(0).gameObject; - clarinetUI = clarinetBackground.transform.GetChild(0).gameObject; - if (!stateController.unlockedTrumpet) { - trumpetBackground.SetActive(false); - } - if (!stateController.unlockedTambourine) { - tambourineBackground.SetActive(false); - } - if (!stateController.unlockedClarinet) { - clarinetBackground.SetActive(false); - } + // TODO: This can be probably be combined with the update methods + this.trumpetBackground.SetActive(StateController.Instance.HasTrumpet()); + this.tambourineBackground.SetActive(StateController.Instance.HasTambourine()); + this.clarinetBackground.SetActive(StateController.Instance.HasClarinet()); } - public void ToggleTrumpet(bool toggleState) { + public void ToggleTrumpet(bool toggleState) + { bool curEnabled = trumpetUI.GetComponent().enabled; - if (curEnabled != toggleState) { + if (curEnabled != toggleState) + { trumpetUI.GetComponent().enabled = toggleState; } } - public void ToggleTambourine(bool toggleState) { + public void ToggleTambourine(bool toggleState) + { bool curEnabled = tambourineUI.GetComponent().enabled; - if (curEnabled != toggleState) { + if (curEnabled != toggleState) + { tambourineUI.GetComponent().enabled = toggleState; } } - public void ToggleClarinet(bool toggleState) { + public void ToggleClarinet(bool toggleState) + { bool curEnabled = clarinetUI.GetComponent().enabled; - if (curEnabled != toggleState) { + if (curEnabled != toggleState) + { clarinetUI.GetComponent().enabled = toggleState; } } - public void ResetInstrumentUI() { - if (stateController == null) { - print("state controller null"); - } - if (stateController.unlockedTrumpet) { - ToggleTrumpet(true); - } - if (stateController.unlockedTambourine) { - ToggleTambourine(true); - } - if (stateController.unlockedClarinet) { - ToggleClarinet(true); - } + public void UpdateInstrumentUI() + { + this.ToggleTrumpet(StateController.Instance.HasTrumpet()); + this.ToggleTambourine(StateController.Instance.HasTambourine()); + this.ToggleClarinet(StateController.Instance.HasClarinet()); } } diff --git a/Assets/Scripts/PlayerBehavior.cs b/Assets/Scripts/PlayerBehavior.cs index 8c0fef4..c28f3c7 100644 --- a/Assets/Scripts/PlayerBehavior.cs +++ b/Assets/Scripts/PlayerBehavior.cs @@ -33,7 +33,6 @@ public class PlayerBehavior : MonoBehaviour [Header("Controllers:")] [SerializeField] private PlayerMovement playerController; - [SerializeField] private StateController stateController; private GameUIController gameUI; Animator animator; @@ -43,12 +42,11 @@ public class PlayerBehavior : MonoBehaviour [SerializeField] public AudioClip footstepSound; [SerializeField] public AudioClip deathSound; AudioSource audioSource; - + void Awake() { // initialize _rb = GetComponent(); - stateController = GameObject.Find("StateController").GetComponent(); gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent(); @@ -61,18 +59,19 @@ public class PlayerBehavior : MonoBehaviour playerIsAlive = true; } - void Start() { - gameUI.ResetInstrumentUI(); + void Start() + { + gameUI.UpdateInstrumentUI(); // for clarinet currentDashTime = maxDashTime; } void Update() { - unlockedTambourine = stateController.unlockedTambourine; - if (playerIsAlive) { + unlockedTambourine = StateController.Instance.HasTambourine(); + if (playerIsAlive) + { // throw tambourine - // if (Input.GetKeyDown(KeyCode.K)) { if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) { ThrowTambourine(); @@ -80,12 +79,10 @@ public class PlayerBehavior : MonoBehaviour // grapple tambourine = GameObject.FindGameObjectWithTag("tambourine"); - // if (Input.GetKeyDown(KeyCode.L)) { if (playerInput.actions["Grapple"].WasPressedThisFrame()) { AttemptGrapple(); } - // if (Input.GetKeyUp(KeyCode.L)) { if (playerInput.actions["Grapple"].WasReleasedThisFrame()) { LetGoOfGrapple(); @@ -94,8 +91,8 @@ public class PlayerBehavior : MonoBehaviour Animate(); } - unlockedClarinet = stateController.unlockedClarinet; - if(playerIsAlive && unlockedClarinet) + unlockedClarinet = StateController.Instance.HasClarinet(); + if (playerIsAlive && unlockedClarinet) { if (playerInput.actions["ClarinetDive"].WasPressedThisFrame()) { @@ -125,31 +122,37 @@ public class PlayerBehavior : MonoBehaviour } } - void Animate() { + void Animate() + { // start walking - if (playerInput.actions["Move"].WasPressedThisFrame()) { + if (playerInput.actions["Move"].WasPressedThisFrame()) + { animator.SetBool("Walking", true); } // return to idle animation - if (playerInput.actions["Move"].WasReleasedThisFrame()) { + if (playerInput.actions["Move"].WasReleasedThisFrame()) + { animator.SetBool("Walking", false); } } void OnMove(InputValue value) { - if (playerIsAlive) { + if (playerIsAlive) + { _hInput = value.Get().x; if (_hInput < 0) { - if (forward != -1) { // if character hasnt already flipped + if (forward != -1) + { // if character hasnt already flipped FlipRenderer(); } forward = -1; } else if (_hInput > 0) { - if (forward != 1) { // if character hasnt already flipped + if (forward != 1) + { // if character hasnt already flipped FlipRenderer(); } forward = 1; @@ -158,30 +161,35 @@ public class PlayerBehavior : MonoBehaviour } - void FlipScale() { // DOENST WORK RIGHT (that's so sad for you) + void FlipScale() + { // DOENST WORK RIGHT (that's so sad for you) Vector3 currentScale = this.gameObject.transform.localScale; currentScale.x *= -1; this.gameObject.transform.localScale = currentScale; } - void FlipRenderer() { + void FlipRenderer() + { GetComponent().flipX = !GetComponent().flipX; } - void ThrowTambourine() { + void ThrowTambourine() + { if (unlockedTambourine && hasTambourine && !grapplingRope.isGrappling) - { - launcher.ThrowTambourine(forward); - SetHasTambourine(false); - } + { + launcher.ThrowTambourine(forward); + SetHasTambourine(false); + } } - public void SetHasTambourine(bool state) { + public void SetHasTambourine(bool state) + { hasTambourine = state; gameUI.ToggleTambourine(state); } - void AttemptGrapple() { + void AttemptGrapple() + { if (tambourine != null) { // grapple to tambourine if (!grapplingRope.isGrappling && tambourine.GetComponent().pinned) @@ -200,11 +208,14 @@ public class PlayerBehavior : MonoBehaviour } } - void LetGoOfGrapple() { - bool currentlyPaused = stateController.isPaused; - if (grapplingRope.isGrappling && !currentlyPaused) { + void LetGoOfGrapple() + { + bool currentlyPaused = StateController.Instance.isPaused; + if (grapplingRope.isGrappling && !currentlyPaused) + { print("currently paused is " + currentlyPaused + ", releasing grapple"); - if (tambourine != null) { + if (tambourine != null) + { tambourine.GetComponent().DestroySelf(); } grapplingGun.ReleaseGrapple(); @@ -222,8 +233,9 @@ public class PlayerBehavior : MonoBehaviour print("player fell in spikes"); StartCoroutine(DestroyPlayer()); } - else if (col.tag == "spawnPoint") { - stateController.spawnPoint.GetComponent().DeactivateSpawnPoint(); + else if (col.tag == "spawnPoint") + { + StateController.Instance.spawnPoint.GetComponent().DeactivateSpawnPoint(); col.GetComponent().ActivateSpawnPoint(); } else if (col.tag == "Trumpet") @@ -246,17 +258,23 @@ public class PlayerBehavior : MonoBehaviour } } - void OnCollisionEnter2D(Collision2D collision) { - if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "ProjectileEnemy") { - if (collision.transform.position.y < transform.position.y) { + void OnCollisionEnter2D(Collision2D collision) + { + if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "ProjectileEnemy") + { + if (collision.transform.position.y < transform.position.y) + { _rb.AddForce(Vector2.up * 8, ForceMode2D.Impulse); collision.gameObject.GetComponent().DefeatEnemy(); - } else { + } + else + { StartCoroutine(DestroyPlayer()); print("enemy defeated player"); } } - else if (collision.gameObject.tag == "Projectile") { + else if (collision.gameObject.tag == "Projectile") + { Destroy(collision.gameObject); StartCoroutine(DestroyPlayer()); } @@ -267,12 +285,14 @@ public class PlayerBehavior : MonoBehaviour } else if (collision.gameObject.tag == "Door") { - this.stateController.RespawnPlayer(); + StateController.Instance.RespawnPlayer(); } } - IEnumerator DestroyPlayer() { - if (playerIsAlive) { + IEnumerator DestroyPlayer() + { + if (playerIsAlive) + { print("destroyPlayer called"); playerIsAlive = false; audioSource.clip = deathSound; @@ -288,12 +308,13 @@ public class PlayerBehavior : MonoBehaviour // destroy all tambourines GameObject[] currentTambourines = GameObject.FindGameObjectsWithTag("tambourine"); - foreach (GameObject tambourine in currentTambourines) { + foreach (GameObject tambourine in currentTambourines) + { tambourine.GetComponent().DestroySelf(); // Destroy(tambourine); } - this.stateController.RespawnPlayer(); + StateController.Instance.RespawnPlayer(); } } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 9e65a8c..ee28915 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -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(); playerBehavior = this.gameObject.GetComponent(); grapplingRope = playerBehavior.grapplingRope; - stateController = GameObject.FindGameObjectWithTag("StateController").GetComponent(); audioSource = GetComponent(); gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent(); 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(); - } 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().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); diff --git a/Assets/Scripts/SceneController.cs b/Assets/Scripts/SceneController.cs index 6e6698c..e24d1cb 100644 --- a/Assets/Scripts/SceneController.cs +++ b/Assets/Scripts/SceneController.cs @@ -7,38 +7,46 @@ using UnityEngine.UI; public class SceneController : MonoBehaviour { + public static SceneController Instance = null; + // this object will always exist! - void Awake() { - // check to see if a state controller already exists - if (GameObject.FindGameObjectWithTag("SceneManager") != null) { - Destroy(this.gameObject); - } else { // if it doesn't, then this is the only one - this.gameObject.tag = "SceneManager"; + void Awake() + { + if (Instance == null) + { + Instance = this; } + // Make this object stay around when switching scenes DontDestroyOnLoad(this.gameObject); SceneManager.sceneLoaded += OnSceneLoaded; } - void OnSceneLoaded(Scene scene, LoadSceneMode mode) { + void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { GameObject pauseMenu = GameObject.Find("PauseMenuCanvas"); - if (pauseMenu != null) { + if (pauseMenu != null) + { Button quitButton = GameObject.Find("QuitButton").GetComponent