ofb/Assets/Scripts/PlayerBehavior.cs

163 lines
4.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:")]
public float moveSpeed;
public float jumpSpeed;
public float airSpeed;
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
public LayerMask groundLayer;
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
[Header("State Control:")]
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>();
airSpeed = .5f * moveSpeed;
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
// jump
// if (Input.GetKeyDown(KeyCode.Space)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded())
{
2023-04-11 19:55:24 -07:00
_rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
// throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
{
if (hasTambourine && !grapplingRope.isGrappling)
{
2023-04-11 19:55:24 -07:00
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
}
// grapple
GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine");
// if (Input.GetKeyDown(KeyCode.L)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Grapple"].WasPressedThisFrame())
{
if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
2023-04-11 19:55:24 -07:00
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
2023-04-17 00:01:49 -07:00
}
else
{
if (grappleSurface != null)
{
2023-04-11 19:55:24 -07:00
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
}
}
// if (Input.GetKeyUp(KeyCode.L)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Grapple"].WasReleasedThisFrame())
{
if (tambourine != null && grapplingRope.isGrappling)
{
2023-04-11 19:55:24 -07:00
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
// if (Input.GetKey(KeyCode.L)) {
2023-04-17 00:01:49 -07:00
if (playerInput.actions["Grapple"].IsPressed())
{
2023-04-11 19:55:24 -07:00
Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green);
}
}
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)
{
2023-04-11 19:55:24 -07:00
forward = -1;
}
2023-04-17 00:01:49 -07:00
else if (_hInput > 0)
{
forward = 1;
2023-04-11 19:55:24 -07:00
}
}
2023-04-17 00:01:49 -07:00
// void FixedUpdate() {
// if (grapplingRope.isGrappling && _hInput != 0 && !IsGrounded()) {
// // print("grappling force");
// _rb.AddForce(new Vector2(_hInput * (airSpeed / 3), 0));
// } else if (_hInput != 0 && !IsGrounded()) {
// _rb.AddForce(new Vector2(_hInput * airSpeed, 0));
// } else if (_hInput != 0) {
// // print("normal movement");
// _rb.AddForce(new Vector2(_hInput * moveSpeed, 0));
// // _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
// }
// }
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
2023-04-11 19:55:24 -07:00
grappleSurface = col.gameObject;
2023-04-17 00:01:49 -07:00
}
else if (col.tag == "instaDeath")
{
stateController.ToggleDeathCanvas();
Destroy(this.gameObject);
2023-04-11 19:55:24 -07:00
}
}
2023-04-17 00:01:49 -07:00
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
2023-04-11 19:55:24 -07:00
grappleSurface = null;
}
}
2023-04-17 00:01:49 -07:00
bool IsGrounded()
{
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer))
{
2023-04-11 19:55:24 -07:00
return true;
}
return false;
}
2023-04-17 00:01:49 -07:00
void OnDrawGizmos()
{
2023-04-11 19:55:24 -07:00
Gizmos.color = Color.red;
2023-04-17 00:01:49 -07:00
Gizmos.DrawCube(transform.position - transform.up * maxDistanceFromGround, boxSize);
2023-04-11 19:55:24 -07:00
}
2023-04-17 00:01:49 -07:00
}