Files
PrinceOfGlory/Assets/Scripts/Leave/LeavePlayAllShowControll.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

172 lines
4.8 KiB
C#

using BigSpace.Logic;
using BigSpace.XRCore.Event;
using BigSpace.XRCore.RunningScene;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeavePlayAllShowControll : MonoBehaviour
{
public GameObject pa_Frame; //掉线后显示可活动区域框
public Transform startPosition; //掉线显示鼎的位置
bool isCheck = false;
bool isEnter = false;
Vector3 currentPosition;
int myId = 0; //自己的编号
bool isWaitCheck = true;
bool isIgnoreExit = false;
//计时相关
bool isRun = false;
float waitTime = 1.5f;
float runTime = 0;
private void Awake()
{
string idName = this.gameObject.name.Replace("LeaveBoxCollider_", "");
if (int.TryParse(idName, out int number2)) //获得自己的碰撞编号
{
myId = number2;
}
if (myId % 100 == 1 || RunningSceneMgr.Instance.isOfflineReconnected) //场景的第一个碰撞 或者是掉线进来
{
Debug.Log("====场景第一个开始检测:" + myId);
isCheck = true;
StartCoroutine(AwakeCheckIsEnter(myId));
}
}
private void Start()
{
GlobalEventMgr.Listen<int>(GameEvent.EventIgnoreExitAll, GameDataManage_EventIgnoreExit);
GlobalEventMgr.Listen<bool>(GameEvent.EventSetPAFreame, GameDataManage_EventSetPAFreame);
}
private void Update()
{
if (isRun)
{
runTime += Time.deltaTime;
if (runTime >= waitTime)
{
isRun = false;
if (!isIgnoreExit) //未进入下一个场景,要显示出界
{
Debug.Log("1显示出界");
StartCoroutine(ShowOutSide());
}
}
}
}
IEnumerator AwakeCheckIsEnter(int index)
{
yield return new WaitForSeconds(0.1f);
if (!isEnter)
{
Debug.Log("====Awake检测掉线");
GlobalEventMgr.Dispatch(GameEvent.EventShowCheckLeavePlay, index);
GlobalEventMgr.Dispatch(GameEvent.EventSetPAFreame, true);
}
else //未掉线,要隐藏掉线地图
{
GlobalEventMgr.Dispatch(GameEvent.EventHideLeavePlay);
GlobalEventMgr.Dispatch(GameEvent.EventSetPAFreame, false);
}
}
//忽略检测出界
private void GameDataManage_EventIgnoreExit(int index)
{
if (index == myId)
{
isIgnoreExit = false;
}
else
{
isIgnoreExit = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "XROriginHands" || other.gameObject.name == "Main Camera")
{
Debug.Log("进来了:" + myId);
GlobalEventMgr.Dispatch(GameEvent.EventIgnoreExitAll, myId); //通知场景内其它碰撞
GlobalEventMgr.Dispatch(GameEvent.EventHideLeavePlay);
}
}
private void OnTriggerStay(Collider other)
{
if (isCheck)
{
if (other.gameObject.name == "XROriginHands" || other.gameObject.name == "Main Camera")
{
isCheck = false;
isEnter = true;
}
}
}
private void OnTriggerExit(Collider other)
{
if (GameDataManage.Instance.isShowLeaveOn) return; //显示着掉线地图就不用执行下面的逻辑
//Debug.Log("碰到谁了:" + other.gameObject.name);
if (other.gameObject.name == "XROriginHands" || other.gameObject.name == "Main Camera")
{
currentPosition = GameObject.Find("XROriginHands/Camera Offset/Main Camera").transform.position;
if (isWaitCheck)
{
isRun = true;
}
//else
//{
// Debug.Log("显示出界");
// StartCoroutine(ShowOutSide());
//}
}
}
IEnumerator ShowOutSide()
{
yield return new WaitForSeconds(0.1f);
////等0.5秒取玩家位置
//Vector3 nowPosition = GameObject.Find("XROriginHands/Camera Offset/Main Camera").transform.position;
//// 计算A点和B点之间的向量
//Vector3 direction = currentPosition - nowPosition;
//// 标准化向量以获取单位向量
//direction.Normalize();
////// 移动物体1米
////currentPosition += direction * 1f;
GlobalEventMgr.Dispatch(GameEvent.EventShowLeavePlayByPosition, startPosition);
GlobalEventMgr.Dispatch(GameEvent.EventSetPAFreame, true);
}
private void GameDataManage_EventSetPAFreame(bool isShow)
{
Debug.Log("设置显示一圈:"+isShow);
SetPAFrame(isShow);
}
void SetPAFrame(bool isShow)
{
if (pa_Frame != null)
{
pa_Frame.SetActive(isShow);
}
}
private void OnDestroy()
{
GlobalEventMgr.Remove<int>(GameEvent.EventIgnoreExitAll, GameDataManage_EventIgnoreExit);
}
}