/*
* author: ryan lin, mark joshwel
* date: 11/8/2024
* description: player interaction behaviour
*/
using UnityEngine;
///
/// player interaction behaviour
///
public class Player : MonoBehaviour
{
///
/// the position of the player
///
[SerializeField] private Transform playerPosition;
///
/// the maximum distance the player can interact with objects
///
[SerializeField] private float interactableDistance;
///
/// the layers the raycast should interact with
///
public LayerMask raycastLayers;
// ///
// /// the current interactable object the player is looking at
// ///
// private CommonInteractable _currentInteractable;
///
/// game manager instance
///
private GameManager _game;
///
/// the raycast hit information
///
private RaycastHit _hit;
///
/// whether the player is looking at an interactable object
///
private bool _raycast;
///
/// initialisation function
///
private void Start()
{
_game = GameManager.Instance;
}
///
/// raycast performer for interactable objects
///
private void Update()
{
_raycast = Physics.Raycast(
playerPosition.position,
playerPosition.TransformDirection(Vector3.forward),
out _hit,
interactableDistance,
raycastLayers
);
Debug.DrawRay(
playerPosition.position,
playerPosition.TransformDirection(Vector3.forward) * interactableDistance,
Color.green
);
if (!_raycast) return;
var interactable = _hit.collider.GetComponent();
if (!interactable)
{
// Debug.Log("not looking at an interactable object");
GameManager.Instance.ClearInteractionPrompt();
return;
}
// show an interaction prompt if we're looking at an interactable object
var prompt = interactable.interactionPrompt;
// Debug.Log(prompt);
if (prompt != "")
GameManager.Instance.SetInteractionPrompt(prompt);
else
GameManager.Instance.ClearInteractionPrompt();
}
///
/// handles the action when the player interacts with an object
///
private void OnAction()
{
if (!_raycast) return;
_hit.collider.GetComponent()?.Interact();
// _currentInteractable?.Interact();
}
///
/// function called by the input system when escape is paused
///
public void OnPause()
{
Debug.Log("escape pressed");
_game.SetDisplayState(_game.Paused ? GameManager.DisplayState.Game : GameManager.DisplayState.OverlayPauseMenu);
}
}