b0364a3dbf
commented abit then built the game
212 lines
6.2 KiB
C#
212 lines
6.2 KiB
C#
/*
|
|
*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
|
|
{
|
|
/// <summary>
|
|
/// the player camera position for the refrence of the raycast
|
|
/// </summary>
|
|
[SerializeField]
|
|
Transform playerCamera;
|
|
|
|
/// <summary>
|
|
/// the distance that the player can see
|
|
/// </summary>
|
|
[SerializeField]
|
|
float seeDistance;
|
|
|
|
/// <summary>
|
|
/// for the interact function to access the interactable object that the raycast "sees"
|
|
/// </summary>
|
|
Interactable curretInteractable;
|
|
|
|
/// <summary>
|
|
/// for the pickable function to access the specific gun that the player is looking at to pick up the right gun
|
|
/// </summary>
|
|
public GunPickable CurrentGun;
|
|
|
|
/// <summary>
|
|
/// for the player to call the shooting and reload functions to access the gun
|
|
/// </summary>
|
|
public GameObject holdingGun;
|
|
|
|
/// <summary>
|
|
/// for other scripts to check what the player is looking at
|
|
/// </summary>
|
|
public RaycastHit hitInfo;
|
|
|
|
/// <summary>
|
|
/// to allow the pickable objects to check if there is a available hand to equip the player with
|
|
/// </summary>
|
|
public static bool handsFull;
|
|
|
|
/// <summary>
|
|
/// to allow the tooltips for ui to help the player
|
|
/// </summary>
|
|
public TextMeshProUGUI tooltips;
|
|
|
|
/// <summary>
|
|
/// to be able to disable the raycast to better the experience of the ui
|
|
/// </summary>
|
|
public bool active;
|
|
|
|
/// <summary>
|
|
/// to allow pickable object to be picked up
|
|
/// </summary>
|
|
Pickable newPickable;
|
|
|
|
/// <summary>
|
|
/// to allow the player to "see" and to display a tooltip ui when the object is interactable
|
|
/// </summary>
|
|
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<TextMeshProUGUI>();
|
|
}
|
|
Debug.Log(hitInfo.transform.name);
|
|
if (hitInfo.transform.TryGetComponent<Interactable>(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<Pickable>(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 = "";
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// to allow the player to interact with the raycasted objects
|
|
/// </summary>
|
|
void OnInteract()
|
|
{
|
|
if (curretInteractable != null)
|
|
{
|
|
if (!handsFull && curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
|
|
{
|
|
holdingGun = CurrentGun.gameObject;
|
|
handsFull = true;
|
|
curretInteractable.Interact();
|
|
}
|
|
else if (!curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
|
|
{
|
|
curretInteractable.Interact();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// to allow the gun to shoot
|
|
/// </summary>
|
|
void OnShoot()
|
|
{
|
|
Debug.Log("Shoot");
|
|
if (holdingGun != null)
|
|
{
|
|
holdingGun.GetComponent<Gun>().Shoot();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// to allow the gun to reload
|
|
/// </summary>
|
|
void OnReload()
|
|
{
|
|
if (holdingGun != null)
|
|
{
|
|
holdingGun.GetComponent<Gun>().Reload();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// to drop the current pickable object
|
|
/// </summary>
|
|
void OnDrop()
|
|
{
|
|
holdingGun.transform.GetComponent<Pickable>().Drop();
|
|
holdingGun = null;
|
|
handsFull = false;
|
|
}
|
|
/// <summary>
|
|
/// to allow the gae to pause
|
|
/// </summary>
|
|
void OnPause()
|
|
{
|
|
//TODO: fix the pause menu
|
|
GameManager.instance.Pause();
|
|
}
|
|
}
|