3DTD/Assets/Scripts/PlacementSystem/CameraSlotClickDetect.cs

120 lines
3.5 KiB
C#

using Cinemachine;
using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour
{
[SerializeField] private TowerCam towerCam;
[SerializeField] private GameObject escImg;
public Camera mainCamera;
public LayerMask layerMask;
public LayerMask selectLayer;
private SlotManager PrevHoveredSlot;
private void Start()
{
escImg.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
ShootClickRay();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
escImg.SetActive(false);
GameManager.Instance.SelectedTower = null;
if (towerCam != null)
towerCam.ChangeToTarget(null);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
}
ShootHoverRay();
}
void ShootClickRay()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
RaycastHit selectHit;
if (!GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer))
{
var tower = selectHit.collider.gameObject.GetComponentInChildren<Tower>();
if (tower != null)
{
GameManager.Instance.SelectedTower = tower;
escImg.SetActive(true);
if (towerCam != null)
towerCam.ChangeToTarget(tower.gameObject);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
}
}
else if (GameManager.Instance.IsBuildMode && Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
slotInfo.OnClick();
GameManager.Instance.SelectedTower = null;
escImg.SetActive(false);
if (towerCam != null)
towerCam.ChangeToTarget(null);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
}
/* else
{
GameManager.Instance.SelectedTower = null;
if (towerCam != null)
towerCam.ChangeToTarget(null);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
} */
}
void ShootHoverRay()
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (GameManager.Instance.IsBuildMode)
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
if (PrevHoveredSlot != null && PrevHoveredSlot != slotInfo)
{
PrevHoveredSlot.OnUnHovered();
}
slotInfo.OnHovered();
PrevHoveredSlot = slotInfo;
}
else
{
if (PrevHoveredSlot != null)
PrevHoveredSlot.OnUnHovered();
PrevHoveredSlot = null;
}
}
public void ExitTurretView()
{
escImg.SetActive(false);
if (towerCam != null)
towerCam.ChangeToTarget(null);
else
Debug.LogWarning("TowerCam scpritet is not assigned, assign in the inspector");
}
}