218 lines
7.8 KiB
C#
218 lines
7.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using System.Linq;
|
|
|
|
//Thomas09
|
|
// this script is only enabled if Joystick is currently in use. Enabling / disabling this script is in InputHandler.cs.
|
|
public class GamePadInputEvents : MonoBehaviour
|
|
{
|
|
public static GamePadInputEvents Instance;
|
|
private Camera mainCam;
|
|
|
|
private const float radius = 0.75f;
|
|
private readonly RaycastHit[] _aimAssistEnemyHits = new RaycastHit[10]; // max of 10 enemies to sort
|
|
|
|
[SerializeField] private LayerMask enemyMask;
|
|
[SerializeField] private LayerMask worldMask;
|
|
|
|
private InputHandler inputHandler;
|
|
|
|
private string cachedFocusedCanvasName;
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
mainCam = Camera.main;
|
|
//Debug.Log($"I am Attached to: {transform.name}");
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
for (int i = 0; i < UIManager.Instance.GamePadInputBlockers.Length; i++)
|
|
{
|
|
UIManager.Instance.GamePadInputBlockers[i].SetActive(true);
|
|
}
|
|
|
|
if (gameObject.scene.isLoaded && !UIManager.Instance.getInterface<GameObject>("LoginScreen").activeInHierarchy)
|
|
{
|
|
SetSelectionForFocusedCanvas();
|
|
inputHandler = Player.Instance.GetInputHandler();
|
|
}
|
|
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (gameObject.scene.isLoaded)
|
|
{
|
|
for (int i = 0; i < UIManager.Instance.GamePadInputBlockers.Length; i++)
|
|
{
|
|
UIManager.Instance.GamePadInputBlockers[i].SetActive(false);
|
|
}
|
|
|
|
//if you're no longer using a controller, and the keyboard is active... Disable it.
|
|
if (UIManager.Instance.OnScreenKeyboardGO.activeInHierarchy)
|
|
{
|
|
UIManager.Instance.OnScreenKeyboardGO.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
void Update()
|
|
{
|
|
|
|
if (Player.Instance.updatePlayer == false && UIManager.Instance.Inventory.activeInHierarchy)
|
|
{
|
|
InventoryEvents();
|
|
if (UIManager.Instance.BankVaultCanvas.activeInHierarchy) { BankVaultEvents(); }
|
|
}
|
|
else //if Update Player is true and Inventory is not open.
|
|
{
|
|
if (cachedFocusedCanvasName != UIManager.Instance.MostRecentCanvasOpenedName)
|
|
{
|
|
cachedFocusedCanvasName = UIManager.Instance.MostRecentCanvasOpenedName;
|
|
SetSelectionForFocusedCanvas();
|
|
}
|
|
|
|
if (inputHandler.SwapWeaponKeyPressed)
|
|
{
|
|
inputHandler.SwapWeaponKeyPressed = false;
|
|
PacketManager.sendClickItem(InventoryManager.Instance.inventoryItems[18], 18);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetSelectionForFocusedCanvas()
|
|
{
|
|
EventSystem.current.SetSelectedGameObject(null);
|
|
string currentFocusedCanvas = UIManager.Instance.MostRecentCanvasOpenedName;
|
|
if (EventSystem.current.currentSelectedGameObject == null)
|
|
{
|
|
//Debug.Log($"Current Canvas name focused: {currentFocusedCanvas}");
|
|
InitializeSelectable canvasSelectable = UIManager.Instance.getInterface<GameObject>(currentFocusedCanvas).GetComponent<InitializeSelectable>();
|
|
if (canvasSelectable != null)
|
|
{
|
|
//Debug.Log($"Current Canvas name focused: {currentFocusedCanvas}");
|
|
StartCoroutine(canvasSelectable.WaitThenSelectRoutine());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void InventoryEvents()
|
|
{
|
|
GameObject selectedObj = EventSystem.current.currentSelectedGameObject;
|
|
if (selectedObj != null && selectedObj.GetComponent<InventoryItemEvents>() != null)
|
|
{
|
|
InventoryItemEvents selectedInventoryItem = EventSystem.current.currentSelectedGameObject.GetComponent<InventoryItemEvents>();
|
|
if (inputHandler.UI_Submit)
|
|
{
|
|
inputHandler.UI_Submit = false;
|
|
selectedInventoryItem.OnClick();
|
|
}
|
|
|
|
if (inputHandler.UI_Submit_Released)
|
|
{
|
|
inputHandler.UI_Submit_Released = false;
|
|
Vector3 screenPos = mainCam.WorldToScreenPoint(selectedInventoryItem.gameObject.transform.position);
|
|
selectedInventoryItem.OnClickUp(screenPos);
|
|
}
|
|
|
|
if (inputHandler.UI_Drop_Item)
|
|
{
|
|
inputHandler.UI_Drop_Item = false;
|
|
Debug.LogError("Dropping Item!");
|
|
selectedInventoryItem.GamepadDropEvent();
|
|
}
|
|
}
|
|
|
|
if (EventSystem.current.currentSelectedGameObject.GetComponent<EquipmentItemEvents>() != null)
|
|
{
|
|
EquipmentItemEvents selectedEquipmentItem = EventSystem.current.currentSelectedGameObject.GetComponent<EquipmentItemEvents>();
|
|
if (inputHandler.UI_Submit)
|
|
{
|
|
inputHandler.UI_Submit = false;
|
|
selectedEquipmentItem.OnClick();
|
|
}
|
|
|
|
if (inputHandler.UI_Submit_Released)
|
|
{
|
|
inputHandler.UI_Submit_Released = false;
|
|
selectedEquipmentItem.OnClickUp();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void BankVaultEvents()
|
|
{
|
|
if (EventSystem.current.currentSelectedGameObject.GetComponent<BankVaultItemEvents>() != null)
|
|
{
|
|
BankVaultItemEvents selectedBankVaultItem = EventSystem.current.currentSelectedGameObject.GetComponent<BankVaultItemEvents>();
|
|
if (inputHandler.UI_Submit)
|
|
{
|
|
inputHandler.UI_Submit = false;
|
|
selectedBankVaultItem.OnClick();
|
|
}
|
|
|
|
if (inputHandler.UI_Submit_Released)
|
|
{
|
|
inputHandler.UI_Submit_Released = false;
|
|
Vector3 screenPos = mainCam.WorldToScreenPoint(selectedBankVaultItem.gameObject.transform.position);
|
|
selectedBankVaultItem.OnClickUp(screenPos);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (Player.Instance.IsAiming && SettingsMenu.Instance.ToggleAimAssist) { AimAssist(); }
|
|
else { Player.Instance.cameraController.SetRotation(); }
|
|
}
|
|
|
|
private List<RaycastHit> GetEnemiesInLineOfSight(Transform cam, int numHits)
|
|
{
|
|
List<RaycastHit> enemyHitsWithLineOfSight = new List<RaycastHit>();
|
|
|
|
for (int i = 0; i < numHits; i++)
|
|
{
|
|
RaycastHit hit = _aimAssistEnemyHits[i];
|
|
NPC npc = hit.transform.root.GetComponent<NPC>();
|
|
|
|
if (npc.isDead) continue;
|
|
|
|
Vector3 dirToEnemyHit = (hit.point - cam.position).normalized;
|
|
|
|
if (!Physics.Raycast(cam.position, dirToEnemyHit, hit.distance, worldMask))
|
|
enemyHitsWithLineOfSight.Add(hit);
|
|
}
|
|
|
|
return enemyHitsWithLineOfSight.OrderByDescending(x => Vector3.Dot(cam.forward, (x.point - cam.position).normalized)).ThenBy(
|
|
x => Vector3.Distance(x.point, cam.position)).ToList();
|
|
}
|
|
|
|
private void AimAssist()
|
|
{
|
|
Transform camTransform = Player.Instance.cameraController.playerCam.transform;
|
|
Ray ray = Player.Instance.cameraController.CameraRay;
|
|
int numHits = Physics.SphereCastNonAlloc(ray, radius, _aimAssistEnemyHits, Mathf.Infinity, enemyMask);
|
|
|
|
if (numHits == 0) return;
|
|
|
|
List<RaycastHit> hitsInLineOfSight = GetEnemiesInLineOfSight(camTransform, numHits);
|
|
|
|
if (hitsInLineOfSight.Count == 0) return;
|
|
|
|
NPC targetNpc = hitsInLineOfSight[0].transform.root.GetComponent<NPC>();
|
|
float extents = CalcExtents(targetNpc.transform.lossyScale);
|
|
Vector3 centerOfEnemy = targetNpc.transform.position;
|
|
centerOfEnemy.y += extents;
|
|
Vector3 enemyDir = (centerOfEnemy - Player.Instance.cameraController.playerCam.transform.position).normalized;
|
|
|
|
Player.Instance.cameraController.AddAimAssist(enemyDir);
|
|
}
|
|
|
|
private float CalcExtents(Vector3 scale)
|
|
{
|
|
return scale.y;
|
|
}
|
|
}
|