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