119 lines
3.2 KiB
C#
119 lines
3.2 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public GameObject DougBody;
|
|
public Shovel Shovel;
|
|
public float walkSpeed;
|
|
|
|
float digTime = 0.9f;
|
|
float digTimestamp = 0;
|
|
bool isDigging = false;
|
|
|
|
Interactable nearestInteractable;
|
|
private CharacterController characterController;
|
|
private Vector3 moveDir;
|
|
private float gravity = 10;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
characterController = GetComponent<CharacterController>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (!isDigging)
|
|
{
|
|
RotatePlayerTowardMouse();
|
|
TryInteract();
|
|
}
|
|
|
|
Walk();
|
|
DigDetector();
|
|
}
|
|
|
|
void Walk()
|
|
{
|
|
moveDir = Vector3.zero;
|
|
|
|
if (!characterController.isGrounded)
|
|
{
|
|
moveDir.y -= gravity * Time.deltaTime;
|
|
}
|
|
|
|
if (!isDigging)
|
|
{
|
|
Vector3 verticalMovement = DougBody.transform.forward * Input.GetAxis("Vertical") * walkSpeed * Time.deltaTime;
|
|
Vector3 horizontalMovement = DougBody.transform.right * Input.GetAxis("Horizontal") * walkSpeed * Time.deltaTime;
|
|
moveDir += verticalMovement + horizontalMovement;
|
|
}
|
|
|
|
characterController.Move(moveDir);
|
|
}
|
|
|
|
void DigDetector()
|
|
{
|
|
if (!isDigging)
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
Shovel.Dig();
|
|
isDigging = true;
|
|
digTimestamp = Time.time + digTime; // fuck coroutines fr fr
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (Time.time > digTimestamp)
|
|
{
|
|
isDigging = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void RotatePlayerTowardMouse()
|
|
{
|
|
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(DougBody.transform.position);
|
|
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
|
|
|
|
float angle = Mathf.Atan2(positionOnScreen.y - mouseOnScreen.y, positionOnScreen.x - mouseOnScreen.x) * Mathf.Rad2Deg;
|
|
|
|
DougBody.transform.rotation = Quaternion.Euler(new Vector3(0f, -(angle + 90), 0f));
|
|
}
|
|
|
|
void TryInteract()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.E) && nearestInteractable != null)
|
|
{
|
|
nearestInteractable.Interact();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
Interactable interactable = other.GetComponent<Interactable>();
|
|
|
|
// store nearest interactable if it exists
|
|
if (interactable != null)
|
|
{
|
|
nearestInteractable = interactable;
|
|
interactable.MoveInsideRange();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
// just null out interactables when we leave an interactable trigger (we cant and shouldn't overlap interactables)
|
|
Interactable interactable = other.GetComponent<Interactable>();
|
|
|
|
// store nearest interactable if it exists
|
|
if (interactable != null && nearestInteractable != null)
|
|
{
|
|
nearestInteractable.MoveOutsideRange();
|
|
nearestInteractable = null;
|
|
}
|
|
}
|
|
}
|