public static GameObject RegisterListener(LevelLoadListener.OnDoneFunc onDone) { GameObject gameObject = new GameObject("LevelLoadListener"); LevelLoadListener levelLoadListener = gameObject.AddComponent <LevelLoadListener>(); DontDestroyOnLoad(levelLoadListener); levelLoadListener.onDone = onDone; return(gameObject); }
public void DoLoad() { Entity player = null; Camera playerCam = null; PlayerController playerCont = null; TileMeshRefOwner refOwner = null; SceneDoor targetDoor = null; PlayerSpawner.DoSpawnFunc onDoSpawn = null; Vector3 spawnDir = base.transform.forward; Vector3 defSpawnPos = this._fallbackPosition; bool hasFallbackPos = this._hasFallbackPos; string wantedDoor = this._correspondingDoor; FadeEffectData fadeData = this._fadeInData ?? this._fadeData; OverlayFader.OnStartFunc onStartFade = null; OverlayFader.OnDoneFunc startFadeIn = null; startFadeIn = delegate() { startFadeIn = null; if (onStartFade != null) { if (targetDoor != null) { onStartFade(new Vector3?(SceneDoor.WorldToScreen(targetDoor.transform.position))); } else { onStartFade(null); } } if (player != null) { BC_Collider component = player.GetComponent <BC_Collider>(); if (component != null) { component.enabled = true; } try { RoomSwitchable component2 = player.GetComponent <RoomSwitchable>(); if (component2 != null) { component2.SendStartEvent(); } } catch (Exception exception) { UnityEngine.Debug.LogException(exception, player); } } if (playerCont != null) { playerCont.SetEnabled(true); } SceneDoor.CheckStartIn(player, targetDoor); }; PlayerSpawner.RegisterSpawnListener(delegate(Entity p, GameObject c, PlayerController cont) { player = p; BC_Collider component = p.GetComponent <BC_Collider>(); if (component != null) { component.enabled = false; } playerCont = cont; playerCont.SetEnabled(false); Camera[] componentsInChildren = c.GetComponentsInChildren <Camera>(); for (int j = 0; j < componentsInChildren.Length; j++) { if (componentsInChildren[j].name == "Main Camera") { playerCam = componentsInChildren[j]; } } onStartFade = OverlayFader.PrepareFadeOut(fadeData, null); SceneDoor.CheckAndBuild(player, playerCam, refOwner, startFadeIn); }); PlayerSpawner.RegisterSpawnDelegation(delegate(PlayerSpawner.DoSpawnFunc func, Vector3 pos, Vector3 dir) { onDoSpawn = func; if (!hasFallbackPos) { defSpawnPos = pos; } }); TileMeshRefOwner.RegisterAwakeListener(delegate(TileMeshRefOwner owner) { refOwner = owner; SceneDoor.CheckAndBuild(player, playerCam, refOwner, startFadeIn); }); SceneDoor.RegisterListener(wantedDoor, delegate(SceneDoor door) { targetDoor = door; targetDoor.coolDown = 1f; if (onDoSpawn == null) { return; } Vector3 pos = targetDoor.transform.TransformPoint(targetDoor._spawnOffset); spawnDir = -targetDoor.transform.forward; PlayerSpawner.DoSpawnFunc doSpawnFunc = onDoSpawn; onDoSpawn = (PlayerSpawner.DoSpawnFunc)null; doSpawnFunc(pos, spawnDir); }); Stopwatch clock = null; LevelLoadListener.RegisterListener(delegate { if (onDoSpawn != null) { if (targetDoor == null) { UnityEngine.Debug.Log(string.Concat(new object[] { "didn't find ", wantedDoor, ", spawning at default: ", defSpawnPos })); } else { defSpawnPos = targetDoor.transform.TransformPoint(targetDoor._spawnOffset); spawnDir = -targetDoor.transform.forward; } PlayerSpawner.DoSpawnFunc doSpawnFunc = onDoSpawn; onDoSpawn = (PlayerSpawner.DoSpawnFunc)null; doSpawnFunc(defSpawnPos, spawnDir); } else if (UnityEngine.Object.FindObjectOfType <PlayerSpawner>() == null) { UnityEngine.Debug.Log("didn't find a spawner in " + Utility.GetCurrentSceneName()); SceneDoor.ClearListeners(); PlayerSpawner.ClearListeners(); if (onStartFade == null) { onStartFade = OverlayFader.PrepareFadeOut(fadeData, null); } } else { UnityEngine.Debug.Log("awaiting spawning in " + Utility.GetCurrentSceneName()); } clock.Stop(); UnityEngine.Debug.Log(string.Concat(new object[] { "Loading ", this._scene, " took ", clock.ElapsedMilliseconds, " ms" })); if (UnityEngine.Object.FindObjectOfType <TileMeshRefOwner>() == null && startFadeIn != null) { startFadeIn(); } }); int prepareCounter = 0; SceneDoor.OnPrepareFunc onPrepareDone = delegate() { --prepareCounter; if (prepareCounter > 0) { return; } clock = Stopwatch.StartNew(); Utility.LoadLevel(this._scene); }; List <SceneDoor.OnPrepareFunc> list = new List <SceneDoor.OnPrepareFunc>(); if (this._saver != null) { list.Add(delegate { this.SaveStartPos(this._saver, wantedDoor, null); this._saver.SaveAll(true, delegate(bool success, string msg) { if (!success) { UnityEngine.Debug.LogError("Error saving: " + msg); } else { UnityEngine.Debug.Log("Game saved"); } onPrepareDone(); }); }); } prepareCounter = list.Count; if (prepareCounter == 0) { onPrepareDone(); return; } for (int i = 0; i < list.Count; i++) { list[i](); } // Invoke custom event EventListener.SceneUnload(); }