Slots: On hover not works

This commit is contained in:
BOT Alex 2024-04-20 18:04:55 +02:00
parent 3a73a81d66
commit b6535a62f9
3 changed files with 43 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
public class CameraSlotClickDetect : MonoBehaviour public class CameraSlotClickDetect : MonoBehaviour
@ -7,15 +8,19 @@ public class CameraSlotClickDetect : MonoBehaviour
public Camera mainCamera; public Camera mainCamera;
public LayerMask layerMask; public LayerMask layerMask;
private SlotManager PrevHoveredSlot;
void Update() void Update()
{ {
if (Input.GetMouseButtonDown(0)) if (Input.GetMouseButtonDown(0))
{ {
ShootRay(); ShootClickRay();
}
} }
void ShootRay() ShootHoverRay();
}
void ShootClickRay()
{ {
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit; RaycastHit hit;
@ -24,8 +29,29 @@ public class CameraSlotClickDetect : MonoBehaviour
{ {
var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>(); var slotInfo = hit.collider.gameObject.GetComponentInParent<SlotManager>();
slotInfo.OnClick(); slotInfo.OnClick();
}
}
Debug.Log($"Hit PlacementSlot! At ({slotInfo.x}, {slotInfo.y})"); 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)
{
slotInfo.OnHovered();
PrevHoveredSlot.OnUnHovered();
}
PrevHoveredSlot = slotInfo;
}
else
{
PrevHoveredSlot = null;
} }
} }
} }

View File

@ -67,6 +67,7 @@ public class TowerPlacementManager : MonoBehaviour
} }
[Space(10)] [Space(10)]
public Material SilhouetteMaterial; public Material SilhouetteMaterial;
public GameObject ToSilhouette(GameObject obj) public GameObject ToSilhouette(GameObject obj)

View File

@ -16,8 +16,21 @@ public class SlotManager : MonoBehaviour
/// </summary> /// </summary>
public event Action<SlotManager, GridManager, int, int> OnSlotClicked; public event Action<SlotManager, GridManager, int, int> OnSlotClicked;
public event Action<SlotManager, GridManager, int, int> OnSlotHovered;
public event Action<SlotManager, GridManager, int, int> OnSlotUnHovered;
public void OnClick() public void OnClick()
{ {
OnSlotClicked?.Invoke(this, spawnerRef, x, y); OnSlotClicked?.Invoke(this, spawnerRef, x, y);
} }
public void OnHovered()
{
OnSlotHovered?.Invoke(this, spawnerRef, x, y);
}
public void OnUnHovered()
{
OnSlotUnHovered?.Invoke(this, spawnerRef, x, y);
}
} }