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

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