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

249 lines
7.4 KiB
C#

using BigSpace.Logic;
using BigSpace.XRCore.Event;
using BigSpace.XRCore.RunningScene;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeavePlayAreaControll : MonoBehaviour
{
public GameObject pa_Frame; //掉线后显示可活动区域框
public Transform startPosition; //掉线显示鼎的位置
bool isCheck = false;
bool isEnter = false;
Vector3 currentPosition;
int myId = 0; //自己的编号
bool isWaitCheck = false;
bool isIgnoreExit = false;
//bool isFirstSpecifylocation = 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.EventCheckBoxCollider, GameDataManage_EventCheckBoxCollider);
GlobalEventMgr.Listen<int>(GameEvent.EventWaitCheckExit, GameDataManage_EventWaitCheckExit);
GlobalEventMgr.Listen<int>(GameEvent.EventIgnoreExit, GameDataManage_EventIgnoreExit);
GlobalEventMgr.Listen(GameEvent.EventShowOutLine, GameDataManage_EventShowOutLine);
GlobalEventMgr.Listen<bool>(GameEvent.EventSetPAFreame, GameDataManage_EventSetPAFreame);
GlobalEventMgr.Listen(GameEvent.EventHideLevelPlay, GameDataManage_EventHideLevelPlay);
}
private void Update()
{
if (isRun)
{
runTime += Time.deltaTime;
if (runTime >= waitTime)
{
isRun = false;
if (!isIgnoreExit) //未进入下一个场景,要显示出界
{
Debug.Log("1显示出界");
StartCoroutine(ShowOutSide());
}
}
}
//if (Input.GetKeyDown(KeyCode.B))
//{
// GameDataManage_EventShowOutLine();
//}
}
IEnumerator AwakeCheckIsEnter(int index)
{
yield return new WaitForSeconds(0.1f);
if (!isEnter)
{
Debug.Log("====Awake检测掉线"+index);
SetPAFrame(true);
GlobalEventMgr.Dispatch(GameEvent.EventShowCheckLeavePlay, index);
//if (index == 501) //场景5特殊处理
//{
// isFirstSpecifylocation = true;
//}
}
else //未掉线,要隐藏掉线地图
{
SetPAFrame(false);
GlobalEventMgr.Dispatch(GameEvent.EventHideLeavePlay);
}
}
private void GameDataManage_EventCheckBoxCollider(int index)
{
Debug.Log("自动检测位置:" + index);
//if (this.gameObject.active && index == myId)
if (index == myId)
{
Debug.Log("进来了.....");
isCheck = true;
StartCoroutine(CheckIsEnter(index));
}
}
//忽略检测出界
private void GameDataManage_EventIgnoreExit(int index)
{
if (index == myId)
{
isIgnoreExit = true;
}
}
//等待检测出界
private void GameDataManage_EventWaitCheckExit(int index)
{
if (index == myId)
{
isWaitCheck = true;
}
}
//主动显示掉线地图
private void GameDataManage_EventShowOutLine()
{
SetPAFrame(true);
GlobalEventMgr.Dispatch(GameEvent.EventShowCheckLeavePlay, myId);
}
IEnumerator CheckIsEnter(int index)
{
yield return new WaitForSeconds(1f);
if (!isEnter)
{
SetPAFrame(true);
GlobalEventMgr.Dispatch(GameEvent.EventShowCheckLeavePlay, index);
}
}
void GameDataManage_EventHideLevelPlay()
{
//isFirstSpecifylocation = false;
SetPAFrame(false);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "XROriginHands" || other.gameObject.name == "Main Camera")
{
//if (isFirstSpecifylocation) return;
Debug.Log("进来了:" + myId);
if ((myId - 1) % 100 != 0)
{
GlobalEventMgr.Dispatch(GameEvent.EventIgnoreExit, myId - 1); //通知道上一个碰撞忽略出界
}
SetPAFrame(false);
GlobalEventMgr.Dispatch(GameEvent.EventHideLeavePlay);
}
}
private void OnTriggerStay(Collider other)
{
//if (isFirstSpecifylocation) return;
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; //显示着掉线地图就不用执行下面的逻辑
//if (isFirstSpecifylocation) 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.position);
SetPAFrame(true);
}
//void ShowOutSide()
//{
// //等0.5秒取玩家位置
// Vector3 nowPosition = GameObject.Find("XROriginHands/Camera Offset/Main Camera").transform.position;
// // 计算A点和B点之间的向量
// Vector3 direction = centerPosition - currentPosition;
// // 标准化向量以获取单位向量
// direction.Normalize();
// //// 移动物体1米
// //currentPosition += direction * 1f;
// GlobalEventMgr.Dispatch(GameEvent.EventShowLeavePlayByPosition, currentPosition += direction * 1.5f);
//}
private void GameDataManage_EventSetPAFreame(bool isShow)
{
SetPAFrame(isShow);
}
void SetPAFrame(bool isShow)
{
if (pa_Frame != null)
{
pa_Frame.SetActive(isShow);
}
}
private void OnDestroy()
{
GlobalEventMgr.Remove<int>(GameEvent.EventCheckBoxCollider, GameDataManage_EventCheckBoxCollider);
}
}