ofb/Assets/Scripts/PlayerBehavior.cs

188 lines
5.2 KiB
C#

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;
[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;
Animator animator;
void Start()
{
_rb = GetComponent<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
animator = GetComponent<Animator>();
GameObject.Find("Main Camera").GetComponent<CameraMovement>().player = this.gameObject;
}
void Update()
{
// 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)
{
_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;
}
}
void FlipScale() { // DOENST WORK RIGHT
Vector3 currentScale = this.gameObject.transform.localScale;
currentScale.x *= -1;
this.gameObject.transform.localScale = currentScale;
}
void FlipRenderer() {
GetComponent<SpriteRenderer>().flipX = !GetComponent<SpriteRenderer>().flipX;
}
void ThrowTambourine() {
if (hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
}
void AttemptGrapple() {
if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().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<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
}
else if (col.tag == "instaDeath")
{
DestroyPlayer();
}
else if (col.tag == "spawnPoint") {
stateController.spawnPoint.GetComponent<SpawnPointBehavior>().DeactivateSpawnPoint();
col.GetComponent<SpawnPointBehavior>().ActivateSpawnPoint();
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
}
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Enemy") {
if (collision.transform.position.y < transform.position.y) {
Destroy(collision.gameObject);
} else {
DestroyPlayer();
}
}
else if (collision.gameObject.tag == "Projectile") {
Destroy(collision.gameObject);
DestroyPlayer();
}
}
public void DestroyPlayer() {
this.stateController.SetDeathCanvasActive(true);
Destroy(this.gameObject);
}
}