using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class PlayerBehavior : MonoBehaviour { [Header("Physics:")] private float _hInput; private Rigidbody2D _rb; private int forward = 1; public PlayerInput playerInput; [Header("Tambourine:")] [SerializeField] private Launch launcher; [HideInInspector] public bool hasTambourine = true; GameObject tambourine; bool unlockedTambourine; [Header("Grappling:")] [SerializeField] public Tutorial_GrapplingGun grapplingGun; [SerializeField] public Tutorial_GrapplingRope grapplingRope; private GameObject grappleSurface; [Header("Controllers:")] [SerializeField] private PlayerMovement playerController; [SerializeField] private StateController stateController; private GameUIController gameUI; Animator animator; [HideInInspector] public bool playerIsAlive = true; [Header("Sound")] [SerializeField] public AudioClip footstepSound; [SerializeField] public AudioClip deathSound; AudioSource audioSource; void Start() { // initialize _rb = GetComponent(); stateController = GameObject.Find("StateController").GetComponent(); gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent(); gameUI.ResetInstrumentUI(); audioSource = this.gameObject.GetComponent(); audioSource.clip = footstepSound; audioSource.loop = true; animator = this.gameObject.GetComponent(); GameObject.Find("Main Camera").GetComponent().player = this.gameObject; playerIsAlive = true; } void Update() { unlockedTambourine = stateController.unlockedTambourine; if (playerIsAlive) { // throw tambourine // if (Input.GetKeyDown(KeyCode.K)) { if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) { ThrowTambourine(); } // 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(); } Animate(); } } void Animate() { // start walking if (playerInput.actions["Move"].WasPressedThisFrame()) { animator.SetBool("Walking", true); } // return to idle animation if (playerInput.actions["Move"].WasReleasedThisFrame()) { animator.SetBool("Walking", false); } } void OnMove(InputValue value) { if (playerIsAlive) { _hInput = value.Get().x; if (_hInput < 0) { if (forward != -1) { // if character hasnt already flipped FlipRenderer(); } forward = -1; } else if (_hInput > 0) { if (forward != 1) { // if character hasnt already flipped FlipRenderer(); } forward = 1; } } } void FlipScale() { // DOENST WORK RIGHT Vector3 currentScale = this.gameObject.transform.localScale; currentScale.x *= -1; this.gameObject.transform.localScale = currentScale; } void FlipRenderer() { GetComponent().flipX = !GetComponent().flipX; } void ThrowTambourine() { if (unlockedTambourine && hasTambourine && !grapplingRope.isGrappling) { launcher.ThrowTambourine(forward); SetHasTambourine(false); } } public void SetHasTambourine(bool state) { hasTambourine = state; gameUI.ToggleTambourine(state); } void AttemptGrapple() { if (tambourine != null) { // grapple to tambourine if (!grapplingRope.isGrappling && tambourine.GetComponent().pinned) { grapplingGun.GrappleToTambourine(tambourine); grapplingRope.isGrappling = true; } } else { if (grappleSurface != null) { grapplingGun.GrappleToSurface(grappleSurface.transform.position); grapplingRope.isGrappling = true; } } } void LetGoOfGrapple() { bool currentlyPaused = stateController.isPaused; if (grapplingRope.isGrappling && !currentlyPaused) { print("currently paused is " + currentlyPaused + ", releasing grapple"); if (tambourine != null) { tambourine.GetComponent().DestroySelf(); } grapplingGun.ReleaseGrapple(); } } void OnTriggerEnter2D(Collider2D col) { if (col.tag == "grappleSurface") { grappleSurface = col.gameObject; } else if (col.tag == "instaDeath") { print("player fell in spikes"); StartCoroutine(DestroyPlayer()); } else if (col.tag == "spawnPoint") { stateController.spawnPoint.GetComponent().DeactivateSpawnPoint(); col.GetComponent().ActivateSpawnPoint(); } else if (col.tag == "Trumpet") { this.playerController.in_range = true; this.playerController.enemy = col.transform.parent.gameObject; } } void OnTriggerExit2D(Collider2D col) { if (col.tag == "grappleSurface") { grappleSurface = null; } else if (col.tag == "Trumpet") { this.playerController.in_range = false; this.playerController.enemy = null; } } 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 { StartCoroutine(DestroyPlayer()); print("enemy defeated player"); } } else if (collision.gameObject.tag == "Projectile") { Destroy(collision.gameObject); StartCoroutine(DestroyPlayer()); } } IEnumerator DestroyPlayer() { if (playerIsAlive) { print("destroyPlayer called"); playerIsAlive = false; audioSource.clip = deathSound; audioSource.loop = false; audioSource.Play(); // animate animator.Play("Die"); // yield return new WaitForSeconds(animator.GetCurrentAnimatorStateInfo(0).length); yield return new WaitForSeconds(audioSource.clip.length); // this.stateController.SetDeathCanvasActive(true); // destroy all tambourines GameObject[] currentTambourines = GameObject.FindGameObjectsWithTag("tambourine"); foreach (GameObject tambourine in currentTambourines) { tambourine.GetComponent().DestroySelf(); // Destroy(tambourine); } this.stateController.RespawnPlayer(); } } }