This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/Player.cs

100 lines
2.6 KiB
C#
Raw Normal View History

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