91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UVC.Management;
|
|
|
|
namespace ChunilENG.Management
|
|
{
|
|
public class MachineClickManager : Manager
|
|
{
|
|
public List<Machine> machines = new List<Machine>();
|
|
public Machine currentClickMachine;
|
|
|
|
RaycastHit[] hitInfo = new RaycastHit[16];
|
|
|
|
RaycastHit hit;
|
|
|
|
public event Action<Machine> onLeftClickMachine;
|
|
public event Action onLeftClickArea;
|
|
|
|
public LayerMask floorLayers;
|
|
public int hitFloorIndex;
|
|
|
|
public override async UniTask Init()
|
|
{
|
|
// machines = ChunilENGSceneMain.Instance.building.GetMachines();
|
|
await UniTask.CompletedTask;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
RaycastOnClickArea();
|
|
RaycastOnClickMachine();
|
|
}
|
|
|
|
private void RaycastOnClickMachine()
|
|
{
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
if (!leftClick)
|
|
{
|
|
return;
|
|
}
|
|
|
|
LayerMask exceptionLayer = LayerMask.GetMask("Floor", "Ground");
|
|
LayerMask layerMask = floorLayers & ~exceptionLayer;
|
|
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
hitInfo = Physics.RaycastAll(ray, Mathf.Infinity, layerMask);
|
|
|
|
if (hitInfo.Length == 0)
|
|
return;
|
|
|
|
Array.Sort(hitInfo, (hit1, hit2) => hit1.distance.CompareTo(hit2.distance));
|
|
var hitColider = hitInfo[0].collider;
|
|
|
|
hitColider.TryGetComponent<Machine>(out var machine);
|
|
|
|
foreach (var floorMachine in machines)
|
|
{
|
|
if(floorMachine == machine)
|
|
{
|
|
if (leftClick)
|
|
{
|
|
onLeftClickMachine?.Invoke(machine);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RaycastOnClickArea()
|
|
{
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
LayerMask exceptionLayer = LayerMask.GetMask("Machine");
|
|
LayerMask layerMask = floorLayers & ~exceptionLayer;
|
|
|
|
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
hitInfo = Physics.RaycastAll(ray, Mathf.Infinity, layerMask);
|
|
|
|
if (hitInfo.Length == 0)
|
|
return;
|
|
|
|
if (leftClick)
|
|
{
|
|
onLeftClickArea?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|