RiftMayhem/Assets/Scripts/Player/PlayerController.cs
2025-02-21 18:35:51 +00:00

175 lines
4.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
[RequireComponent(typeof(PlayerMovement))]
public class PlayerController : MonoBehaviour
{
[Header("Listeners:")]
[SerializeField] private GameEventListener onLocalPlayerFainted;
[SerializeField] private GameEventListener onLocalPlayerPermaDeath;
[SerializeField] private GameEventListener onLocalPlayerRevived;
[Space]
[SerializeField] private BoolSharedField directionalInputMode;
public ParticleSystem clickArrows;
public Interactable focus;
public LayerMask movementMask;
public LayerMask interactableMask;
Camera cam;
public PlayerMovement motor;
Plane plane = new Plane(Vector3.down, 0);
ProjectileSpawnLocationController projectileSpawnLocationController;
Ray ray;
RaycastHit hit;
bool isDead;
bool isFishing;
public UnityEvent onStartFishing = new UnityEvent();
private void Awake()
{
cam = Camera.main;
motor = GetComponent<PlayerMovement>();
projectileSpawnLocationController = GetComponentInChildren<ProjectileSpawnLocationController>();
}
// Start is called before the first frame update
void Start()
{
onLocalPlayerFainted.Response.AddListener(() =>
{
isDead = true;
motor.MoveToPoint(this.transform.position);
motor.ToggleAgentMoving(true);
});
onLocalPlayerPermaDeath.Response.AddListener(() =>
{
isDead = true;
motor.MoveToPoint(this.transform.position);
motor.ToggleAgentMoving(true);
});
onLocalPlayerRevived.Response.AddListener(() =>
{
isDead = false;
motor.ToggleAgentMoving(false);
});
clickArrows.transform.SetParent(null);
}
public void StartFishing()
{
isFishing = true;
motor.StopLocomotion(0);
motor.ToggleAgentMoving(true);
motor.InstantFaceCast(projectileSpawnLocationController.GetLookat());
onStartFishing.Invoke();
}
public void OnFishingEnded()
{
((FishingSpotInteractable)focus).OnFishingEnded();
isFishing = false;
motor.ToggleAgentMoving(false);
}
// Update is called once per frame
void Update()
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
if (isDead) return;
if (isFishing) return;
if (Input.GetMouseButtonDown(0) && !directionalInputMode.Value)
{
ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f, movementMask))
{
clickArrows.transform.position = hit.point;
clickArrows.Play();
}
}
if (Input.GetMouseButton(0) && !directionalInputMode.Value)
{
ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100f, movementMask))
{
motor.MoveToPoint(hit.point);
//Debug.Log("hitpoint: " + hit.point);
RemoveFocus();
}
else
{
if (plane.Raycast(ray, out float distance))
{
motor.MoveToPoint(ray.GetPoint(distance));
//Debug.Log("hitpoint: " + ray.GetPoint(distance));
RemoveFocus();
}
}
}
if (Input.GetMouseButton(1))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f, interactableMask))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
SetFocus(interactable);
}
// if we did, set as focus
}
}
}
private void SetFocus(Interactable newFocus)
{
if (newFocus != focus)
{
if (focus != null)
{
focus.OnDeFocus();
}
focus = newFocus;
motor.FollowTarget(focus);
}
newFocus.OnFocused(transform, this);
}
public void RemoveFocus()
{
if (focus != null)
{
focus.OnDeFocus();
}
motor.StopFollowingTarget();
focus = null;
}
}