166 lines
4.8 KiB
C#
166 lines
4.8 KiB
C#
using UnityEngine;
|
|
using WI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace CHN
|
|
{
|
|
public class CHNRaycaster : MonoBehaviour, ISingle
|
|
{
|
|
OrbitalController controller;
|
|
public List<Machine> machines = new List<Machine>();
|
|
|
|
RaycastHit[] hitInfo = new RaycastHit[16];
|
|
Canvas_Popup popupCanvas;
|
|
|
|
RaycastHit hit;
|
|
Canvas_Top topCanvas;
|
|
|
|
public event Action<Machine> onLeftClickMachine;
|
|
public event Action<Machine> onRightClickMachine;
|
|
public event Action onRightClick;
|
|
public event Action<BoardType> onClickProductionBoard;
|
|
public event Action onClickThermostat;
|
|
|
|
public LayerMask floorLayers;
|
|
public int hitFloorIndex;
|
|
|
|
public override void AfterAwake()
|
|
{
|
|
controller = FindSingle<OrbitalController>();
|
|
popupCanvas = FindSingle<Canvas_Popup>();
|
|
topCanvas = FindSingle<Canvas_Top>();
|
|
}
|
|
public override void AfterStart()
|
|
{
|
|
var building = FindSingle<Building>();
|
|
machines = building.floors.SelectMany(f => f.machines).ToList();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//RaycastOnClickThermostat();
|
|
RaycastOnClickMachine();
|
|
RaycastOnClickProductionStatusBoard();
|
|
}
|
|
|
|
private void RaycastOnClickMachine()
|
|
{
|
|
var floorMachines = FindSingle<Building>().currentFloor.machines;
|
|
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
if (!leftClick)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (controller.IsClickUI)
|
|
return;
|
|
|
|
LayerMask exceptionLayer = LayerMask.GetMask("Floor", "Floor Ground");
|
|
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;
|
|
|
|
Machine machine = hitColider.GetComponent<Machine>();
|
|
|
|
foreach(var floorMachine in floorMachines)
|
|
{
|
|
if(floorMachine == machine)
|
|
{
|
|
if (machine == null)
|
|
return;
|
|
|
|
if (leftClick)
|
|
{
|
|
onLeftClickMachine?.Invoke(machine);
|
|
ActivateMachinesHighlight(machine);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void RaycastOnClickThermostat()
|
|
{
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
if (!leftClick)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (controller.IsClickUI)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var layerMask = LayerMask.GetMask("Thermostat");
|
|
var 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;
|
|
|
|
var board = hitColider.GetComponent<Thermostat>();
|
|
|
|
if (leftClick)
|
|
{
|
|
onClickThermostat?.Invoke();
|
|
}
|
|
}
|
|
private void ActivateMachinesHighlight(Machine currentMachine)
|
|
{
|
|
foreach (var machine in machines)
|
|
{
|
|
machine.ResetTranslucent();
|
|
machine.StopFlash();
|
|
}
|
|
currentMachine.DeactivateTranslucent();
|
|
currentMachine.StartOneFlash();
|
|
}
|
|
private void RaycastOnClickProductionStatusBoard()
|
|
{
|
|
bool leftClick = Input.GetMouseButtonDown(0);
|
|
|
|
if (!leftClick)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (controller.IsClickUI)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var layerMask = LayerMask.GetMask("ProductionStatusBoard");
|
|
var 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;
|
|
|
|
var board = hitColider.GetComponent<ProductionStatusBoard>();
|
|
|
|
if (leftClick)
|
|
{
|
|
onClickProductionBoard?.Invoke(board.boardType);
|
|
}
|
|
}
|
|
}
|
|
}
|