i3e-asg2/Assets/Scripts/CharactorControl.cs

212 lines
6.2 KiB
C#
Raw Permalink Normal View History

/*
*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
*/
2024-07-04 17:22:55 +00:00
using System;
2024-07-01 03:11:10 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
2024-07-04 17:22:55 +00:00
using TMPro;
2024-07-01 03:11:10 +00:00
using Unity.VisualScripting;
using UnityEngine;
public class CharacterControl : MonoBehaviour
{
/// <summary>
/// the player camera position for the refrence of the raycast
/// </summary>
2024-07-01 03:11:10 +00:00
[SerializeField]
Transform playerCamera;
/// <summary>
/// the distance that the player can see
/// </summary>
2024-07-01 03:11:10 +00:00
[SerializeField]
float seeDistance;
/// <summary>
/// for the interact function to access the interactable object that the raycast "sees"
/// </summary>
2024-07-01 03:11:10 +00:00
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>
2024-07-02 02:42:09 +00:00
public GunPickable CurrentGun;
/// <summary>
/// for the player to call the shooting and reload functions to access the gun
/// </summary>
2024-07-02 02:42:09 +00:00
public GameObject holdingGun;
/// <summary>
/// for other scripts to check what the player is looking at
/// </summary>
2024-07-01 03:11:10 +00:00
public RaycastHit hitInfo;
/// <summary>
/// to allow the pickable objects to check if there is a available hand to equip the player with
/// </summary>
2024-07-01 03:11:10 +00:00
public static bool handsFull;
2024-07-02 02:42:09 +00:00
/// <summary>
/// to allow the tooltips for ui to help the player
/// </summary>
2024-07-04 17:22:55 +00:00
public TextMeshProUGUI tooltips;
/// <summary>
/// to be able to disable the raycast to better the experience of the ui
/// </summary>
2024-07-04 17:22:55 +00:00
public bool active;
2024-07-01 03:11:10 +00:00
/// <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>
2024-07-01 03:11:10 +00:00
void Update()
{
2024-07-04 17:22:55 +00:00
if (active)
2024-07-01 03:11:10 +00:00
{
2024-07-04 17:22:55 +00:00
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
)
2024-07-04 17:22:55 +00:00
{
tooltips.text = "get the Generator and Petrol to power the ship";
2024-07-04 17:22:55 +00:00
}
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 = "";
}
}
2024-07-01 03:11:10 +00:00
else
{
curretInteractable = null;
2024-07-04 17:22:55 +00:00
CurrentGun = null;
tooltips.text = "";
2024-07-01 03:11:10 +00:00
}
}
}
/// <summary>
/// to allow the player to interact with the raycasted objects
/// </summary>
2024-07-01 03:11:10 +00:00
void OnInteract()
{
if (curretInteractable != null)
{
2024-07-04 17:22:55 +00:00
if (!handsFull && curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
2024-07-02 02:42:09 +00:00
{
holdingGun = CurrentGun.gameObject;
2024-07-04 17:22:55 +00:00
handsFull = true;
curretInteractable.Interact();
}
else if (!curretInteractable.TryGetComponent<GunPickable>(out CurrentGun))
{
curretInteractable.Interact();
2024-07-02 02:42:09 +00:00
}
2024-07-01 03:11:10 +00:00
}
}
/// <summary>
/// to allow the gun to shoot
/// </summary>
2024-07-01 03:11:10 +00:00
void OnShoot()
{
Debug.Log("Shoot");
2024-07-04 17:22:55 +00:00
if (holdingGun != null)
2024-07-01 03:11:10 +00:00
{
2024-07-04 17:22:55 +00:00
holdingGun.GetComponent<Gun>().Shoot();
2024-07-01 03:11:10 +00:00
}
}
/// <summary>
/// to allow the gun to reload
/// </summary>
2024-07-01 03:11:10 +00:00
void OnReload()
{
2024-07-04 17:22:55 +00:00
if (holdingGun != null)
2024-07-01 03:11:10 +00:00
{
2024-07-04 17:22:55 +00:00
holdingGun.GetComponent<Gun>().Reload();
2024-07-01 03:11:10 +00:00
}
}
/// <summary>
/// to drop the current pickable object
/// </summary>
2024-07-01 03:11:10 +00:00
void OnDrop()
{
2024-07-04 17:22:55 +00:00
holdingGun.transform.GetComponent<Pickable>().Drop();
holdingGun = null;
handsFull = false;
}
/// <summary>
/// to allow the gae to pause
/// </summary>
2024-07-04 17:22:55 +00:00
void OnPause()
{
//TODO: fix the pause menu
2024-07-04 17:22:55 +00:00
GameManager.instance.Pause();
2024-07-01 03:11:10 +00:00
}
}