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

@ -6,26 +6,21 @@ using TMPro;
public class DebugSceneSwitcher : MonoBehaviour
{
void Awake() {
if (!GameObject.Find("StateController").GetComponent<StateController>().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<TMP_Dropdown>();
if (sceneDropdown.options.Count == 0) {
if (sceneDropdown.options.Count == 0)
{
List<string> sceneNames = new List<string>();
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<SceneController>().LoadChosenScene(index);
this.gameObject.SetActive(false);
SceneController.Instance.LoadChosenScene(index);
}
}

View File

@ -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<SceneController>().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];
}
}

View File

@ -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<StateController>();
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<Image>().enabled;
if (curEnabled != toggleState) {
if (curEnabled != toggleState)
{
trumpetUI.GetComponent<Image>().enabled = toggleState;
}
}
public void ToggleTambourine(bool toggleState) {
public void ToggleTambourine(bool toggleState)
{
bool curEnabled = tambourineUI.GetComponent<Image>().enabled;
if (curEnabled != toggleState) {
if (curEnabled != toggleState)
{
tambourineUI.GetComponent<Image>().enabled = toggleState;
}
}
public void ToggleClarinet(bool toggleState) {
public void ToggleClarinet(bool toggleState)
{
bool curEnabled = clarinetUI.GetComponent<Image>().enabled;
if (curEnabled != toggleState) {
if (curEnabled != toggleState)
{
clarinetUI.GetComponent<Image>().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());
}
}

View File

@ -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<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent<GameUIController>();
@ -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<Vector2>().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<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().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<TambourineBehavior>().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<TambourineBehavior>().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<SpawnPointBehavior>().DeactivateSpawnPoint();
else if (col.tag == "spawnPoint")
{
StateController.Instance.spawnPoint.GetComponent<SpawnPointBehavior>().DeactivateSpawnPoint();
col.GetComponent<SpawnPointBehavior>().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<EnemyPatrol>().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<TambourineBehavior>().DestroySelf();
// Destroy(tambourine);
}
this.stateController.RespawnPlayer();
StateController.Instance.RespawnPlayer();
}
}
}

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);

View File

@ -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<Button>();
quitButton.onClick.AddListener(BackToMainMenu);
}
if (scene.buildIndex == 0) { // if this is the menu
if (scene.buildIndex == 0)
{ // if this is the menu
GameObject.Find("NewGameButton").GetComponent<Button>().onClick.AddListener(NextScene);
}
}
public void NextScene() {
public void NextScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void BackToMainMenu() {
public void BackToMainMenu()
{
SceneManager.LoadScene(0); // main menu scene should be 0
}
public void LoadChosenScene(int index) {
public void LoadChosenScene(int index)
{
SceneManager.LoadScene(index);
}

View File

@ -4,7 +4,19 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class StateController : MonoBehaviour {
public enum UnlockedItems
{
None,
Trumpet,
Tambourine,
Clarinet,
}
public class StateController : MonoBehaviour
{
// Singleton class
public static StateController Instance = null;
[Header("Respawning")]
[SerializeField] GameObject player;
@ -17,7 +29,7 @@ public class StateController : MonoBehaviour {
[Header("Debug")]
public bool inDebugMode;
GameObject debugCanvas;
public GameObject debugCanvas;
[Header("Enemies")]
GameObject[] enemiesInScene;
@ -26,33 +38,53 @@ public class StateController : MonoBehaviour {
GameObject victoryCanvas;
[Header("Unlocked Items")]
[SerializeField] public bool unlockedTrumpet = false;
[SerializeField] public bool unlockedTambourine = false;
[SerializeField] public bool unlockedClarinet = false;
public UnlockedItems itemProgression = UnlockedItems.None;
void Awake() {
// check to see if a state controller already exists
if (GameObject.FindGameObjectWithTag("StateController") != null) {
Destroy(this.gameObject);
} else { // if it doesn't, then this is the only one
this.gameObject.tag = "StateController";
void Awake()
{
// TODO: Remove this when done
AudioListener.pause = true;
if (Instance == null)
{
Instance = this;
}
DontDestroyOnLoad(this.gameObject);
//DontDestroyOnLoad(this.gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
if (inDebugMode) {
if (this.inDebugMode)
{
debugCanvas = GameObject.Find("DebugCanvas");
debugCanvas.SetActive(false);
}
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
public bool HasTrumpet()
{
return this.itemProgression >= UnlockedItems.Trumpet;
}
public bool HasTambourine()
{
return this.itemProgression >= UnlockedItems.Tambourine;
}
public bool HasClarinet()
{
return this.itemProgression >= UnlockedItems.Clarinet;
}
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
#region FIND OBJECTS
deathCanvas = GameObject.Find("DeathUICanvas");
if (deathCanvas != null) {
if (deathCanvas != null)
{
Button respawnButton = GameObject.Find("RespawnButton").GetComponent<Button>();
if (respawnButton == null) {
if (respawnButton == null)
{
print("respawn button not found!");
}
respawnButton.onClick.AddListener(RespawnPlayer);
@ -60,14 +92,16 @@ public class StateController : MonoBehaviour {
}
pauseMenuCanvas = GameObject.Find("PauseMenuCanvas");
if (pauseMenuCanvas != null) {
if (pauseMenuCanvas != null)
{
Button resumeButton = GameObject.Find("ResumeButton").GetComponent<Button>();
resumeButton.onClick.AddListener(Unpause);
TogglePauseMenu(false);
}
victoryCanvas = GameObject.Find("VictoryCanvas");
if (victoryCanvas != null) {
if (victoryCanvas != null)
{
Button continueButton = GameObject.Find("ContinueButton").GetComponent<Button>();
continueButton.onClick.AddListener(ContinueToNextLevel);
victoryCanvas.SetActive(false);
@ -77,49 +111,47 @@ public class StateController : MonoBehaviour {
enemiesInScene = GameObject.FindGameObjectsWithTag("Enemy");
// print(enemiesInScene);
if (isPaused) {
if (isPaused)
{
Unpause();
}
#endregion
#region UNLOCK ITEMS
if (SceneManager.GetActiveScene().name == "GrenouilleVillage") {
unlockedTrumpet = false;
unlockedTambourine = false;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "Brasslands")
switch (SceneManager.GetActiveScene().name)
{
unlockedTrumpet = true;
unlockedTambourine = false;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "GrappleScene") {
unlockedTrumpet = true;
unlockedTambourine = true;
unlockedClarinet = false;
}
else if (SceneManager.GetActiveScene().name == "ClarinetScene") {
unlockedTrumpet = true;
unlockedTambourine = true;
unlockedClarinet = true;
}
case "GrenouilleVillage":
this.itemProgression = UnlockedItems.None;
break;
case "GrappleScene":
this.itemProgression = UnlockedItems.Trumpet;
break;
case "ClarinetScene":
this.itemProgression = UnlockedItems.Clarinet;
break;
};
#endregion
}
void OnToggleDebugMenu() {
if (inDebugMode) {
debugCanvas.SetActive(!debugCanvas.activeSelf);
void OnToggleDebugMenu()
{
if (inDebugMode)
{
debugCanvas.SetActive(!this.debugCanvas.activeSelf);
}
}
void OnPause() {
if (pauseMenuCanvas != null) {
if (!isPaused) {
void OnPause()
{
if (pauseMenuCanvas != null)
{
if (!isPaused)
{
Time.timeScale = 0;
TogglePauseMenu(true);
} else {
}
else
{
Time.timeScale = 1;
TogglePauseMenu(false);
}
@ -127,17 +159,20 @@ public class StateController : MonoBehaviour {
}
}
public void Unpause() {
public void Unpause()
{
Time.timeScale = 1;
TogglePauseMenu(false);
isPaused = !isPaused;
}
void TogglePauseMenu(bool showPauseMenu) {
void TogglePauseMenu(bool showPauseMenu)
{
pauseMenuCanvas.SetActive(showPauseMenu);
}
public void RespawnPlayer() {
public void RespawnPlayer()
{
Destroy(GameObject.FindGameObjectWithTag("Player"));
SetDeathCanvasActive(false);
GameObject.Find("Main Camera").GetComponent<CameraMovement>().FindPlayer();
@ -145,21 +180,26 @@ public class StateController : MonoBehaviour {
Instantiate(player, spawnPoint.transform.position, player.transform.rotation);
}
public void RespawnEnemies() {
foreach (GameObject enemy in enemiesInScene) {
public void RespawnEnemies()
{
foreach (GameObject enemy in enemiesInScene)
{
enemy.SetActive(true);
}
}
public void SetDeathCanvasActive(bool activeState) {
public void SetDeathCanvasActive(bool activeState)
{
deathCanvas.SetActive(activeState);
}
public void ShowVictoryCanvas() {
public void ShowVictoryCanvas()
{
victoryCanvas.SetActive(true);
}
public void ContinueToNextLevel() {
GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneController>().NextScene();
public void ContinueToNextLevel()
{
SceneController.Instance.NextScene();
}
}

View File

@ -8,7 +8,8 @@ public class VictoryObjectBehavior : MonoBehaviour
public float speed = 1f;
Vector2 initPosition;
void Awake() {
void Awake()
{
initPosition = this.transform.position;
}
@ -18,13 +19,16 @@ public class VictoryObjectBehavior : MonoBehaviour
transform.position = new Vector2(initPosition.x, initPosition.y + Mathf.Sin(Time.time * speed) * height);
}
void OnDrawGizmos() {
void OnDrawGizmos()
{
Gizmos.DrawLine(new Vector3(transform.position.x, transform.position.y - height, transform.position.z), new Vector3(transform.position.x, transform.position.y + height, transform.position.z));
}
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "Player") {
GameObject.FindGameObjectWithTag("StateController").GetComponent<StateController>().ShowVictoryCanvas();
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
StateController.Instance.ShowVictoryCanvas();
}
}
}