68 lines
1.8 KiB
C#
68 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class CameraSlotClickDetect : MonoBehaviour
|
|
{
|
|
public Camera mainCamera;
|
|
public LayerMask layerMask;
|
|
public LayerMask selectLayer;
|
|
|
|
private SlotManager PrevHoveredSlot;
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
ShootClickRay();
|
|
}
|
|
|
|
ShootHoverRay();
|
|
}
|
|
|
|
void ShootClickRay()
|
|
{
|
|
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
RaycastHit selectHit;
|
|
if (Physics.Raycast(ray, out selectHit, Mathf.Infinity, selectLayer) && !GameManager.Instance.IsBuildMode)
|
|
{
|
|
var tower = selectHit.collider.gameObject.GetComponentInChildren<Tower>();
|
|
if (tower != null)
|
|
{
|
|
GameManager.Instance.SelectedTower = tower;
|
|
}
|
|
}
|
|
else if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
|
|
{
|
|
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
|
|
slotInfo.OnClick();
|
|
GameManager.Instance.SelectedTower = null;
|
|
}
|
|
}
|
|
|
|
void ShootHoverRay()
|
|
{
|
|
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
|
RaycastHit hit;
|
|
|
|
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
|
|
{
|
|
PrevHoveredSlot = null;
|
|
}
|
|
}
|
|
}
|