/*
*Author: Lin Hengrui Ryan
*Date:06/06/2024
*Description: this script is for all player interaction and to allow the player to see using raycast
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
///
/// the player camera position for the refrence of the raycast
///
[SerializeField]
Transform playerCamera;
///
/// the distance that the player can see
///
[SerializeField]
float seeDistance;
///
/// for the interact function to access the interactable object that the raycast "sees"
///
Interactable curretInteractable;
///
/// for the pickable function to access the specific gun that the player is looking at to pick up the right gun
///
public GunPickable CurrentGun;
///
/// for the player to call the shooting and reload functions to access the gun
///
public GameObject holdingGun;
///
/// for other scripts to check what the player is looking at
///
public RaycastHit hitInfo;
///
/// to allow the pickable objects to check if there is a available hand to equip the player with
///
public static bool handsFull;
///
/// to allow the tooltips for ui to help the player
///
public TextMeshProUGUI tooltips;
///
/// to be able to disable the raycast to better the experience of the ui
///
public bool active;
///
/// to allow pickable object to be picked up
///
Pickable newPickable;
///
/// to allow the player to "see" and to display a tooltip ui when the object is interactable
///
void Update()
{
if (active)
{
bool Raycast = Physics.Raycast(
playerCamera.position,
playerCamera.TransformDirection(Vector3.forward),
out hitInfo,
seeDistance
);
Debug.DrawRay(
playerCamera.position,
playerCamera.TransformDirection(Vector3.forward) * seeDistance,
Color.green
);
if (Raycast)
{
if (tooltips == null)
{
tooltips = GameObject.Find("toolTips").GetComponent();
}
Debug.Log(hitInfo.transform.name);
if (hitInfo.transform.TryGetComponent(out curretInteractable))
{
if (curretInteractable.tag == "door")
{
if (
curretInteractable.name == "doorSlide"
&& !GameManager.instance.petrolCollected
&& !GameManager.instance.gotGenerator
)
{
tooltips.text = "get the Generator and Petrol to power the ship";
}
else
{
tooltips.text = "Press [e] to open door";
}
}
else if (curretInteractable.name == "petrol")
{
if (!GameManager.instance.petrolCollected)
{
tooltips.text = "Press [e] to pump petrol";
}
else
{
tooltips.text = "petrol Collected";
}
}
else if (curretInteractable.TryGetComponent(out newPickable))
{
if (handsFull)
{
tooltips.text = "hands are full drop item using [g]";
}
else
{
tooltips.text = "Press [e] to Pick up";
}
}
else if (curretInteractable.tag == "collectable")
{
tooltips.text = "press [e] to collect";
}
}
else
{
curretInteractable = null;
CurrentGun = null;
tooltips.text = "";
}
}
else
{
curretInteractable = null;
CurrentGun = null;
tooltips.text = "";
}
}
}
///
/// to allow the player to interact with the raycasted objects
///
void OnInteract()
{
if (curretInteractable != null)
{
if (!handsFull && curretInteractable.TryGetComponent(out CurrentGun))
{
holdingGun = CurrentGun.gameObject;
handsFull = true;
curretInteractable.Interact();
}
else if (!curretInteractable.TryGetComponent(out CurrentGun))
{
curretInteractable.Interact();
}
}
}
///
/// to allow the gun to shoot
///
void OnShoot()
{
Debug.Log("Shoot");
if (holdingGun != null)
{
holdingGun.GetComponent().Shoot();
}
}
///
/// to allow the gun to reload
///
void OnReload()
{
if (holdingGun != null)
{
holdingGun.GetComponent().Reload();
}
}
///
/// to drop the current pickable object
///
void OnDrop()
{
holdingGun.transform.GetComponent().Drop();
holdingGun = null;
handsFull = false;
}
///
/// to allow the gae to pause
///
void OnPause()
{
//TODO: fix the pause menu
GameManager.instance.Pause();
}
}