ofb/Assets/Scripts/PlayerBehavior.cs
slevy14 4b7bb231a9 added a way for the player to die and respawn, reconfigured test level
ALSO installed TMP but it has been giving me ISSUES
2023-04-13 16:52:11 -07:00

147 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBehavior : MonoBehaviour {
[Header("Physics:")]
public float moveSpeed;
public float jumpSpeed;
public float airSpeed;
private float _hInput;
private Rigidbody2D _rb;
private int forward = 1;
public PlayerInput playerInput;
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;
void Start() {
_rb = GetComponent<Rigidbody2D>();
airSpeed = .5f * moveSpeed;
stateController = GameObject.Find("StateController").GetComponent<StateController>();
}
void Update() {
// jump
// if (Input.GetKeyDown(KeyCode.Space)) {
if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded()) {
_rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
// throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) {
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) {
if (hasTambourine && !grapplingRope.isGrappling) {
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
}
// grapple
GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine");
// if (Input.GetKeyDown(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasPressedThisFrame()) {
if (tambourine != null) { // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned) {
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
} else { // grapple to grapple surface
// RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(0.500f * forward, 0.866f));
// if (hit.collider != null) {
// print("hit " + hit.collider.gameObject.tag);
// if (hit.collider.gameObject.tag == "grappleSurface" && !grapplingRope.isGrappling) {
// grapplingGun.GrappleToSurface(hit.collider.gameObject.transform.position);
// grapplingRope.isGrappling = true;
// }
// else {print("missed");}
// }
if (grappleSurface != null) {
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
}
}
// if (Input.GetKeyUp(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasReleasedThisFrame()) {
if (tambourine != null && grapplingRope.isGrappling) {
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
// if (Input.GetKey(KeyCode.L)) {
if (playerInput.actions["Grapple"].IsPressed()) {
Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green);
}
}
void OnMove(InputValue value) {
_hInput = value.Get<Vector2>().x;
if (_hInput < 0) {
forward = -1;
} else if (_hInput > 0) {
forward = 1;
}
}
void FixedUpdate() {
// if (_hInput != 0) {
// _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
// }
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") {
grappleSurface = col.gameObject;
} else if (col.tag == "instaDeath") {
stateController.ToggleDeathCanvas();
Destroy(this.gameObject);
}
}
void OnTriggerExit2D(Collider2D col) {
if (col.tag == "grappleSurface") {
grappleSurface = null;
}
}
bool IsGrounded() {
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer)) {
return true;
}
return false;
}
void OnDrawGizmos() {
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position-transform.up*maxDistanceFromGround,boxSize);
}
}