using System.Collections;
using UnityEngine;
using BigSpace.XRCore.Base;
using BigSpace.XRCore.Config;
using UnityEngine.SceneManagement;
using BigSpace.XRCore.Timeline;
using BigSpace.XRCore.Video;
using BigSpace.XRCore.Scene;
using BigSpace.XRCore.GameMode;
using BigSpace.XRCore.Event;
using BigSpace.Logic;
namespace BigSpace.XRCore.RunningScene
{
public class RunningSceneData
{
public int id;
public float time;
}
///
/// 场景加载之后 负责场景的恢复,timeline video的恢复, 已经上传当前运行场景的信息
///
public class RunningSceneMgr : MonoSingleton
{
public readonly int WaitingSceneId = 99999; //组队房间
//public readonly int LoadingSceneId = 10000;
public RunningSceneData _runningSceneData = new RunningSceneData();
public TimeLineControll timelineCtr { get; set; }
public VideoPlayControll videoCtr { get; set; }
//有断线数据计时,要晚一点上传数据,加载场景和恢复场景数据需要时间
//public Progress RecoverData = null; //断线数据
public bool isOfflineReconnected = false; //是不是断线重连
private float elapsed = 0;
private bool isPause = true;
private float timerInterval = 6; //等待几秒后开启心跳并开始上传数据,目的是等timeline和video加载好
private void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
Application.quitting += OnApplicationQuit;
}
private void Update()
{
if (isPause) return;
elapsed += Time.deltaTime;
if (elapsed >= timerInterval)
{
isPause = true;
elapsed = 0;
//RecoverData = null;
isOfflineReconnected = false; //不能过早的清除,掉线后进来,检测地图时要用到
}
}
private void OnSceneLoaded(UnityEngine.SceneManagement.Scene arg0, LoadSceneMode arg1)
{
var scene = SceneManager.GetActiveScene();
InitSceneInfo(scene);
if (scene.name == "Loading") return;//断线进来有可能是这个场景名,不能弹出去 return; //||
//Debug.Log("加载完场景名:"+ scene.name);
StartCoroutine(HandleRunningScene(scene));
}
private void InitSceneInfo(UnityEngine.SceneManagement.Scene scene)
{
var sceneConfigs = ConfigMgr.Instance.GetConfigList();
SceneDataConfig currentSceneConfig = null;
foreach (var sc in sceneConfigs)
{
if (sc.SceneName == scene.name)
{
currentSceneConfig = sc;
break;
}
}
if (currentSceneConfig != null)
{
_runningSceneData.id = currentSceneConfig.id;
}
}
private IEnumerator HandleRunningScene(UnityEngine.SceneManagement.Scene scene)
{
yield return null;
//等待视频预加载完成
if (videoCtr != null)
{
yield return StartCoroutine(videoCtr.IsAllPrepared());
//yield return new WaitForSeconds(1f);//防止场景白一下
}
if (isOfflineReconnected)
{
RecoveryRunScene(); // 断线重连逻辑
}
else
{
StartCoroutine(DelayRunScene(GameModeMgr.Instance.IsOffline ? 0.1f : 1f)); //正常运行逻辑
}
//Debug.Log("变亮淡入===========================" + scene.name);
//ChangSceneSphereMgr.Instance.ShowChangeSecne(false); //旧UI相关
//yield return new WaitForSeconds(GameDataManage.Instance.inFadeTime); //旧UI相关
StartCoroutine(DelaySetIsCanMove(scene.name));
}
private IEnumerator DelayRunScene(float delay)
{
yield return new WaitForSeconds(delay);
NormalRunScene();
}
private IEnumerator DelaySetIsCanMove(string sceneName)
{
yield return new WaitForSeconds(1);
if (sceneName != "Waiting" && sceneName != "Loading")
{
GlobalEventMgr.Dispatch(GameEvent.EventSetHandMove, 2);
}
}
///
/// 正常进入时,走正常进入场景的逻辑
///
public void NormalRunScene()
{
if (timelineCtr != null) timelineCtr.NormalPlay();
if (videoCtr != null) videoCtr.NormalPlay();
}
///
/// 断线重连或者重新进入时,做恢复场景的逻辑,因为要跳转到指定的场景,timeline指定的位置,video指定的位置
///
public void RecoveryRunScene()
{
if (timelineCtr != null)
{
//timelineCtr.RecoveryPlay(RecoverData.Timeline);
Debug.Log("timelineCtr不为空..........................");
}
else
{
Debug.Log("timelineCtr是空..........................");
}
if (videoCtr != null)
{
//videoCtr.RecoveryPlay(RecoverData.Video);
Debug.Log("videoCtr不为空..........................");
}
else
{
Debug.Log("videoCtr是空..........................");
}
isPause = false; //计时等待清除离线数据
Debug.Log("离线数据恢复后心跳启动..........................");
//WebSocketAgent.Ins.SendHeartBeat(); //开启心跳
}
void OnApplicationQuit()
{
//if(RoomNetwork.Ins!=null)
// RoomNetwork.Ins.ExitApp();
}
}
}