广西桂平帝恒管道燃气投资有限公司多角色网上演练
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.0 KiB

1 year ago
using System;
using System.Collections;
using System.Collections.Generic;
using AX.MessageSystem;
using UnityEngine;
using AX.InputSystem;
using UnityEngine.AI;
using AX.NetworkSystem;
public class CloneSafeAreaTool : CloneBase
{
[SerializeField]
[Tooltip("距离地面距离")]
protected float Height = 0;
public float AreaPrefabSize = 20;
private NavMeshHit nhit;
private NavMeshQueryFilter filter;
private GameObject WaterCarParent;
Vector2 vLU;
Vector2 vLD;
Vector2 vRU;
Vector2 vRD;
Ray rayLU;
Ray rayLD;
Ray rayRU;
Ray rayRD;
RaycastHit HitLU;
RaycastHit HitLD;
RaycastHit HitRU;
RaycastHit HitRD;
public bool cloneAble;//true表示能克隆或停放;否则不行
private void Awake()
{
WaterCarParent = GameObject.Find("P_Cars").transform.Find("P_WaterTanker").gameObject;
filter.agentTypeID = WaterCarParent.GetComponent<CloneCar>().clonePrefab.GetComponent<NavMeshAgent>().agentTypeID;
filter.areaMask = WaterCarParent.GetComponent<CloneCar>().clonePrefab.GetComponent<NavMeshAgent>().areaMask;
}
public override void Execute(IMessage obj)
{
if (transform.childCount > 0)
{//只能存在一个安全区
return;
}
var gameObjID = (long)obj.Sender;
var data = ((CloneCmdArgs)obj.Data);
if (data.cloneObjType == cloneObjType)
{
var hitPoint = data.hitPos;
CheckCloneAble(hitPoint);
if (!cloneAble)
{
return;
}
Vector3 clonedObjPos = new Vector3(hitPoint.x, hitPoint.y + Height, hitPoint.z);
var clonedObj = EntitiesManager.Instance.CreateObj(clonePrefab, clonedObjPos, transform, gameObjID);
clonedObj.name = CloneObjNameTool.Instance().GetCloneObjectName(cloneObjType);
SelectedObjs.gameObjs.Add(clonedObj);
//设置克隆物体所在楼层等基本属性,属性从点击的对象上获取
var hitObj = EntitiesManager.Instance.GetEntityByID(data.gameObjID);
CloneGameObjInfo hitObjInfo = hitObj.GetComponent<CloneGameObjInfo>();
CloneGameObjInfo cloneObjInfo = clonedObj.GetComponent<CloneGameObjInfo>();
cloneObjInfo.gameObjType = cloneObjType;
cloneObjInfo.UserID = CurrentUserInfo.mySelf.Id;
cloneObjInfo.buildNum = hitObjInfo.buildNum;
cloneObjInfo.floorNum = hitObjInfo.floorNum;
cloneObjInfo.interlayerNum = hitObjInfo.interlayerNum;
//克隆同步
SafeAreaSyncData clonesync = new SafeAreaSyncData();
clonesync.gameObjID = clonedObj.GetComponent<BaseGameObjInfo>().gameObjID;
clonesync.gameObjType = clonedObj.GetComponent<BaseGameObjInfo>().gameObjType;
clonesync.UserID = clonedObj.GetComponent<BaseGameObjInfo>().UserID;
clonesync.buildNum = hitObjInfo.buildNum;
clonesync.floorNum = hitObjInfo.floorNum;
clonesync.interlayerNum = hitObjInfo.interlayerNum;
clonesync.SendUserID = CurrentUserInfo.mySelf.Id;
clonesync.ClonePosition = clonedObjPos;
clonesync.name = clonedObj.name;
clonesync.HitObjID = hitObjInfo.gameObjID;
NetworkManager.Default.SendAsync("CLONE_SAFEAREA_SYNC", clonesync);
}
}
/// <summary>
/// 检查当前克隆位置能否克隆安全区(或者当前位置能否停放安全区)
/// </summary>
/// <param name="hitPoint"></param>
public bool CheckCloneAble(Vector3 hitPoint)
{
vLU = Camera.main.WorldToScreenPoint(hitPoint + Vector3.left * AreaPrefabSize / 2 + Vector3.forward * AreaPrefabSize / 2);
vLD = Camera.main.WorldToScreenPoint(hitPoint + Vector3.left * AreaPrefabSize / 2 + Vector3.back * AreaPrefabSize / 2);
vRU = Camera.main.WorldToScreenPoint(hitPoint + Vector3.right * AreaPrefabSize / 2 + Vector3.forward * AreaPrefabSize / 2);
vRD = Camera.main.WorldToScreenPoint(hitPoint + Vector3.right * AreaPrefabSize / 2 + Vector3.back * AreaPrefabSize / 2);
rayLU = Camera.main.ScreenPointToRay(vLU);
rayLD = Camera.main.ScreenPointToRay(vLD);
rayRU = Camera.main.ScreenPointToRay(vRU);
rayRD = Camera.main.ScreenPointToRay(vRD);
if (Physics.Raycast(rayLU, out HitLU))
{
if (NavMesh.SamplePosition(HitLU.point, out nhit, 0.5f, filter))
{
if (Vector3.Distance(HitLU.point, nhit.position) > 0.2f)
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
else
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
if (Physics.Raycast(rayLD, out HitLD))
{
if (NavMesh.SamplePosition(HitLD.point, out nhit, 0.5f, filter))
{
if (Vector3.Distance(HitLD.point, nhit.position) > 0.2f)
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
else
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
if (Physics.Raycast(rayRU, out HitRU))
{
if (NavMesh.SamplePosition(HitRU.point, out nhit, 0.5f, filter))
{
if (Vector3.Distance(HitRU.point, nhit.position) > 0.2f)
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
else
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
if (Physics.Raycast(rayRD, out HitRD))
{
if (NavMesh.SamplePosition(HitRD.point, out nhit, 0.5f, filter))
{
if (Vector3.Distance(HitRD.point, nhit.position) > 0.2f)
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
else
{
LoadPromptWin.Instance.LoadTextPromptWindow("当前位置无法设置安全区!", 1f);
cloneAble = false;
return cloneAble;
}
}
return cloneAble = true;
}
}