82 lines
2.2 KiB
C#
82 lines
2.2 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AZTECHWB.Command;
|
|
using Cysharp.Threading.Tasks;
|
|
using AZTECHWB.Core;
|
|
|
|
namespace AZTECHWB.Management
|
|
{
|
|
public class Raycaster : Manager
|
|
{
|
|
private OrbitalController controller;
|
|
private List<Machine> machines = new List<Machine>();
|
|
public Machine currentClickMachine;
|
|
|
|
private RaycastHit[] hitInfo = new RaycastHit[16];
|
|
|
|
public LayerMask floorLayers;
|
|
private bool isInteractable;
|
|
|
|
public override async UniTask Init()
|
|
{
|
|
controller = AZTECHAppMain.Instance.cameraController;
|
|
var building = AZTECHSceneMain.Instance.building;
|
|
|
|
machines = building.machineList;
|
|
await UniTask.CompletedTask;
|
|
}
|
|
public void SetInteractable(bool isClickable)
|
|
{
|
|
isInteractable = isClickable;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
RaycastOnClickMachine();
|
|
}
|
|
|
|
private void RaycastOnClickMachine()
|
|
{
|
|
if (!isInteractable)
|
|
return;
|
|
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
if (!leftClick)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (controller.IsClickUI)
|
|
return;
|
|
|
|
LayerMask exceptionLayer = LayerMask.GetMask("Floor Ground", "Floor Wall", "Roof");
|
|
LayerMask layerMask = floorLayers & ~exceptionLayer;
|
|
|
|
Ray ray = controller.Camera.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 clickMachine);
|
|
|
|
foreach (var machine in machines)
|
|
{
|
|
if(machine == clickMachine)
|
|
{
|
|
if (leftClick)
|
|
{
|
|
new SelectedMachineCommand(machine, true).Execute();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|