ofb/Assets/Scripts/PlayerBehavior.cs

334 lines
9.6 KiB
C#
Raw Normal View History

2023-04-11 19:55:24 -07:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
2023-04-17 00:01:49 -07:00
public class PlayerBehavior : MonoBehaviour
{
2023-04-11 19:55:24 -07:00
[Header("Physics:")]
private float _hInput;
private Rigidbody2D _rb;
private int forward = 1;
public PlayerInput playerInput;
2023-04-17 00:01:49 -07:00
2023-04-11 19:55:24 -07:00
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
GameObject tambourine;
bool unlockedTambourine;
2023-04-11 19:55:24 -07:00
2023-05-02 18:21:00 -07:00
[Header("Clarinet:")]
bool unlockedClarinet;
2023-05-04 13:20:35 -07:00
[SerializeField] private float maxDashTime = 2.5f;
2023-05-03 12:06:29 -07:00
[SerializeField] private float dashStopSpeed = 0.1f;
public Vector3 moveDirection;
2023-05-04 13:20:35 -07:00
public float dashDistance = 0.1f;
2023-05-02 23:32:12 -07:00
private float currentDashTime;
2023-05-03 00:35:59 -07:00
public bool isDash = false;
2023-05-02 18:21:00 -07:00
2023-04-11 19:55:24 -07:00
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
[Header("Controllers:")]
[SerializeField] private PlayerMovement playerController;
private GameUIController gameUI;
Animator animator;
[HideInInspector] public bool playerIsAlive = true;
[Header("Sound")]
[SerializeField] public AudioClip footstepSound;
[SerializeField] public AudioClip deathSound;
AudioSource audioSource;
2023-04-11 19:55:24 -07:00
void Awake()
{ // initialize
2023-04-11 19:55:24 -07:00
_rb = GetComponent<Rigidbody2D>();
gameUI = GameObject.FindGameObjectWithTag("GameUICanvas").GetComponent<GameUIController>();
audioSource = this.gameObject.GetComponent<AudioSource>();
audioSource.clip = footstepSound;
audioSource.loop = true;
animator = this.gameObject.GetComponent<Animator>();
GameObject.Find("Main Camera").GetComponent<CameraMovement>().player = this.gameObject;
playerIsAlive = true;
2023-04-11 19:55:24 -07:00
}
void Start()
{
gameUI.UpdateInstrumentUI();
2023-05-02 23:32:12 -07:00
// for clarinet
currentDashTime = maxDashTime;
}
2023-04-17 00:01:49 -07:00
void Update()
{
unlockedTambourine = StateController.Instance.HasTambourine();
if (playerIsAlive)
{
// throw tambourine
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
{
ThrowTambourine();
}
2023-04-11 19:55:24 -07:00
// grapple
tambourine = GameObject.FindGameObjectWithTag("tambourine");
if (playerInput.actions["Grapple"].WasPressedThisFrame())
{
AttemptGrapple();
}
if (playerInput.actions["Grapple"].WasReleasedThisFrame() && grapplingRope.isGrappling)
{
LetGoOfGrapple();
}
2023-04-11 19:55:24 -07:00
Animate();
2023-04-11 19:55:24 -07:00
}
unlockedClarinet = StateController.Instance.HasClarinet();
if (playerIsAlive && unlockedClarinet)
{
2023-05-02 22:04:18 -07:00
if (playerInput.actions["ClarinetDive"].WasPressedThisFrame())
{
2023-05-03 00:35:59 -07:00
isDash = true;
2023-05-02 23:32:12 -07:00
currentDashTime = 0.0f;
2023-05-03 08:15:59 -07:00
playerInput.DeactivateInput();
2023-05-02 22:04:18 -07:00
}
2023-05-03 11:53:37 -07:00
if (!playerController.IsGrounded() && (currentDashTime < maxDashTime) && isDash)
2023-05-03 00:35:59 -07:00
{
if (forward == 1)
{
moveDirection = new Vector3(1f, -1f, 0f) * dashDistance;
2023-05-03 09:53:26 -07:00
}
else
{
moveDirection = new Vector3(-1f, -1f, 0f) * dashDistance;
2023-05-03 09:53:26 -07:00
}
2023-05-03 11:53:37 -07:00
transform.position = transform.position + moveDirection;
2023-05-03 08:15:59 -07:00
currentDashTime += dashStopSpeed;
print("cdt " + currentDashTime);
}
else
{
isDash = false;
playerInput.ActivateInput();
2023-05-03 00:35:59 -07:00
}
}
}
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);
}
2023-04-11 19:55:24 -07:00
}
2023-04-17 00:01:49 -07:00
void OnMove(InputValue value)
{
if (playerIsAlive)
{
_hInput = value.Get<Vector2>().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;
}
2023-04-11 19:55:24 -07:00
}
}
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;
2023-04-11 19:55:24 -07:00
}
void FlipRenderer()
{
GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX;
}
void ThrowTambourine()
{
if (unlockedTambourine && hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward);
SetHasTambourine(false);
}
2023-04-11 19:55:24 -07:00
}
public void SetHasTambourine(bool state)
{
hasTambourine = state;
gameUI.ToggleTambourine(state);
}
void AttemptGrapple()
{
if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
2023-04-11 19:55:24 -07:00
}
else
2023-04-17 00:01:49 -07:00
{
if (grappleSurface != null)
{
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
2023-04-11 19:55:24 -07:00
}
}
void LetGoOfGrapple()
{
bool currentlyPaused = StateController.Instance.isPaused;
if (grapplingRope.isGrappling && !currentlyPaused)
{
2023-04-25 10:59:49 -07:00
print("currently paused is " + currentlyPaused + ", releasing grapple");
if (tambourine != null)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
2023-04-25 10:59:49 -07:00
}
2023-04-11 19:55:24 -07:00
}
2023-04-17 16:47:12 -07:00
2023-05-04 13:53:37 -07:00
void Bounce()
{
2023-05-04 15:17:29 -07:00
print("boing");
2023-05-04 13:53:37 -07:00
}
2023-04-17 16:47:12 -07:00
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
}
else if (col.tag == "instaDeath")
{
print("player fell in spikes");
StartCoroutine(DestroyPlayer());
2023-04-17 16:47:12 -07:00
}
else if (col.tag == "spawnPoint")
{
StateController.Instance.spawnPoint.GetComponent<SpawnPointBehavior>().DeactivateSpawnPoint();
col.GetComponent<SpawnPointBehavior>().ActivateSpawnPoint();
}
2023-04-27 15:56:36 -07:00
else if (col.tag == "Trumpet")
{
this.playerController.in_range = true;
this.playerController.enemy = col.transform.parent.gameObject;
}
2023-05-04 13:53:37 -07:00
else if (col.tag == "bouncy")
{
Bounce();
}
2023-04-17 16:47:12 -07:00
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
2023-04-27 15:56:36 -07:00
else if (col.tag == "Trumpet")
{
2023-04-28 16:18:50 -07:00
this.playerController.in_range = false;
2023-04-27 15:56:36 -07:00
this.playerController.enemy = null;
}
2023-04-17 16:47:12 -07:00
}
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
{
StartCoroutine(DestroyPlayer());
print("enemy defeated player");
}
}
else if (collision.gameObject.tag == "Projectile")
{
Destroy(collision.gameObject);
StartCoroutine(DestroyPlayer());
}
//stupid stuff for claude's house
else if (collision.gameObject.tag == "SirJacques")
{
Destroy(collision.gameObject);
}
else if (collision.gameObject.tag == "Door")
{
StateController.Instance.RespawnPlayer();
}
}
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);
if (grapplingRope.isGrappling) {
LetGoOfGrapple();
}
// destroy all tambourines
GameObject[] currentTambourines = GameObject.FindGameObjectsWithTag("tambourine");
foreach (GameObject tambourine in currentTambourines)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
// Destroy(tambourine);
}
StateController.Instance.RespawnPlayer();
}
}
2023-04-17 00:01:49 -07:00
}