update
This commit is contained in:
145
Assets/Scripts/CHNRaycaster.cs
Normal file
145
Assets/Scripts/CHNRaycaster.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using UnityEngine;
|
||||
using WI;
|
||||
using System;
|
||||
|
||||
namespace CHN
|
||||
{
|
||||
public class CHNRaycaster : MonoBehaviour, ISingle
|
||||
{
|
||||
OrbitalController controller;
|
||||
|
||||
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 LayerMask floorLayers;
|
||||
public int hitFloorIndex;
|
||||
|
||||
public override void AfterAwake()
|
||||
{
|
||||
controller = FindSingle<OrbitalController>();
|
||||
popupCanvas = FindSingle<Canvas_Popup>();
|
||||
topCanvas = FindSingle<Canvas_Top>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
RaycastChangeView();
|
||||
RaycastOnClickMachine();
|
||||
RaycastOnClickProductionStatusBoard();
|
||||
}
|
||||
|
||||
|
||||
private void RaycastChangeView()
|
||||
{
|
||||
if (controller.viewMode != ViewMode.PerspectiveView)
|
||||
return;
|
||||
|
||||
bool isChangeView = false;
|
||||
|
||||
Transform camera = controller.camera.transform;
|
||||
|
||||
LayerMask layerMask = LayerMask.GetMask("Floor Ground", "Floor Wall");
|
||||
|
||||
if (Physics.Raycast(camera.localPosition, camera.forward, out hit, Mathf.Infinity, layerMask))
|
||||
{
|
||||
var hitLayer = hit.collider.gameObject.layer;
|
||||
isChangeView = hitLayer.Equals(LayerMask.NameToLayer("Floor Ground"));
|
||||
|
||||
if (isChangeView)
|
||||
{
|
||||
hitFloorIndex = hit.collider.gameObject.GetComponentInParent<Floor>().floorIndex;
|
||||
}
|
||||
|
||||
LayerMask wallMask = LayerMask.GetMask("Floor Wall");
|
||||
Collider[] wallOverlapCols = Physics.OverlapSphere(controller.option.target.position, controller.targetColliderRadius, wallMask);
|
||||
|
||||
if (wallOverlapCols.Length > 0)
|
||||
{
|
||||
isChangeView = false;
|
||||
}
|
||||
}
|
||||
|
||||
//topCanvas.panel_toptoolbar.SetChangeViewButtonState(isChangeView);
|
||||
}
|
||||
|
||||
private void RaycastOnClickMachine()
|
||||
{
|
||||
bool leftClick = Input.GetMouseButtonDown(0);
|
||||
bool rightClick = Input.GetMouseButtonDown(1);
|
||||
if (!leftClick && !rightClick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (controller.IsClickUI)
|
||||
return;
|
||||
|
||||
LayerMask exceptionLayer = LayerMask.GetMask("Floor");
|
||||
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>();
|
||||
if (machine == null)
|
||||
{
|
||||
onRightClick?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
if (leftClick)
|
||||
{
|
||||
onLeftClickMachine?.Invoke(machine);
|
||||
}
|
||||
if (rightClick)
|
||||
{
|
||||
onRightClickMachine?.Invoke(machine);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user