ofb/Assets/Scripts/PlayerBehavior.cs

142 lines
3.7 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;
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;
[SerializeField] private StateController stateController;
2023-04-11 19:55:24 -07:00
2023-04-17 00:01:49 -07:00
void Start()
{
2023-04-11 19:55:24 -07:00
_rb = GetComponent<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
2023-04-11 19:55:24 -07:00
}
2023-04-17 00:01:49 -07:00
void Update()
{
2023-04-11 19:55:24 -07:00
// throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
{
ThrowTambourine();
2023-04-11 19:55:24 -07:00
}
// grapple
tambourine = GameObject.FindGameObjectWithTag("tambourine");
2023-04-11 19:55:24 -07:00
// if (Input.GetKeyDown(KeyCode.L)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Grapple"].WasPressedThisFrame())
{
AttemptGrapple();
2023-04-11 19:55:24 -07:00
}
// if (Input.GetKeyUp(KeyCode.L)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Grapple"].WasReleasedThisFrame())
{
LetGoOfGrapple();
2023-04-11 19:55:24 -07:00
}
}
2023-04-17 00:01:49 -07:00
void OnMove(InputValue value)
{
2023-04-11 19:55:24 -07:00
_hInput = value.Get<Vector2>().x;
2023-04-17 00:01:49 -07:00
if (_hInput < 0)
{
// if (forward != -1) { // if character hasnt already flipped
// FlipScale();
// }
2023-04-11 19:55:24 -07:00
forward = -1;
}
2023-04-17 00:01:49 -07:00
else if (_hInput > 0)
{
// if (forward != 1) { // if character hasnt already flipped
// FlipScale();
// }
2023-04-17 00:01:49 -07:00
forward = 1;
2023-04-11 19:55:24 -07:00
}
}
void FlipScale() { // DOENST WORK RIGHT
Vector3 currentScale = this.gameObject.transform.localScale;
currentScale.x *= -1;
this.gameObject.transform.localScale = currentScale;
2023-04-11 19:55:24 -07:00
}
void ThrowTambourine() {
if (hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
2023-04-11 19:55:24 -07:00
}
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() {
if (tambourine != null && grapplingRope.isGrappling)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
2023-04-11 19:55:24 -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")
{
this.stateController.SetDeathCanvasActive(true);
Destroy(this.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
}
2023-04-17 00:01:49 -07:00
}