private IEnumerator EnteringUnloadedZone(ZoneLoadStatus status, ZoneData from, ZoneData to, bool waitForConnections) { Freeze(); if (to.State == ZoneState.Unloaded || to.State == ZoneState.Unloading) { yield return(LoadZone(to)); } while (to.State == ZoneState.Loading) { yield return(null); } if (waitForConnections) { yield return(LoadConnections(to, from)); } else { StartCoroutine(LoadConnections(to, from)); } EnteringZone(to, from); status.IsDone = true; Thaw(); }
private IEnumerator UnloadZone(ZoneData data) { if (data.State != ZoneState.Loaded) { Debug.LogErrorFormat(this, _zoneNotLoadedError, data.Zone.name); yield break; } data.Unloading(); data.State = ZoneState.Unloading; LoadedZones.Remove(data); var loader = SceneManager.UnloadSceneAsync(data.SceneIndex); if (loader == null) { Debug.LogErrorFormat(this, _zoneUnloadFailedError, data.Zone.name); yield break; } while (!loader.isDone) { yield return(null); } data.State = ZoneState.Unloaded; }
private ZoneLoadStatus ChangeZone(ZoneData to, bool waitForConnections) { var from = Player.Instance.Zone; var loader = new ZoneLoadStatus { IsDone = false }; if (from != null) { LeaveZone(from, to); } if (to != null) { if (to.State == ZoneState.Loaded && !waitForConnections) { EnteringLoadedZone(loader, to, from); } else { StartCoroutine(EnteringUnloadedZone(loader, from, to, waitForConnections)); } } else { loader.IsDone = true; } return(loader); }
private void EnteringZone(ZoneData to, ZoneData from) { _context.SetStore(nameof(Zone), to); SceneManager.SetActiveScene(to.Zone.Scene.Scene); Player.Instance.Zone = to; to.Entering(from); }
internal void Entering(ZoneData from) { IsActive = true; if (AudioManager.Instance && Zone.BackgroundMusic != null && (from == null || Zone.BackgroundMusic != from.Zone.BackgroundMusic)) { AudioManager.Instance.Push(Zone.BackgroundMusic, 1.0f, 1.0f, 1.0f); } }
private void LeaveZone(ZoneData from, ZoneData to) { from.Left(to); Player.Instance.Zone = null; SceneManager.SetActiveScene(gameObject.scene); UnloadConnections(from, to); _context.SetStore(nameof(Zone), null); }
internal void Left(ZoneData to) { IsActive = false; _variables.ResetAvailability(Zone.ZoneActiveAvailability); if (AudioManager.Instance && Zone.BackgroundMusic != null && (to == null || Zone.BackgroundMusic != to.Zone.BackgroundMusic)) { AudioManager.Instance.Pop(1.0f, 1.0f, 1.0f); } }
private void UnloadConnections(ZoneData from, ZoneData to) { foreach (var index in from.Connections) { var data = Zones[index]; if (to == null || (data != to && !to.Connections.Contains(data.SceneIndex))) { StartCoroutine(UnloadZone(data)); } } if (to == null || !to.Connections.Contains(from.SceneIndex)) { StartCoroutine(UnloadZone(from)); } }
private IEnumerator LoadZone(ZoneData data) { while (data.State == ZoneState.Unloading) { yield return(null); } if (data.State != ZoneState.Unloaded) { Debug.LogErrorFormat(this, _zoneAlreadyLoadedError, data.Zone.name); yield break; } var loader = SceneManager.LoadSceneAsync(data.Zone.Scene.Index, LoadSceneMode.Additive); if (loader == null) { Debug.LogErrorFormat(this, _zoneNotAssignedError, data.Zone.name); yield break; } _pendingLoads++; data.State = ZoneState.Loading; while (!loader.isDone) { yield return(null); } var valid = data.Loaded(); if (valid) { data.State = ZoneState.Loaded; LoadedZones.Add(data); } else { SceneManager.UnloadSceneAsync(data.SceneIndex); data.State = ZoneState.Unloaded; } _pendingLoads--; }
private void CreateZones() { Zones = new ZoneData[SceneManager.sceneCountInBuildSettings]; LoadedZones = new List <ZoneData>(); foreach (var zone in World.Zones) { var index = zone.Scene.Index; if (index < 0) { Debug.LogWarningFormat(zone, _missingZoneSceneWarning, zone.name); } else { Zones[index] = CreateZone(zone, index); } } }
private IEnumerator SpawnPlayer(ZoneData zone, SpawnPoint spawnPoint) { if (spawnPoint.IsNamed) { spawnPoint = zone.GetSpawnPoint(spawnPoint.Name); } else { spawnPoint = zone.SpawnPoints.Count > 0 ? zone.SpawnPoints.Values.First() : SpawnPoint.Default; } Player.Instance.Mover.WarpToPosition(spawnPoint.Position, spawnPoint.Direction, spawnPoint.Layer); yield return(TransitionManager.Instance.RunTransition(spawnPoint.Transition == null ? World.DefaultSpawnTransition : spawnPoint.Transition, TransitionPhase.In)); // Don't null coalesce these if (spawnPoint.Move) { Player.Instance.Mover.Move(spawnPoint.Direction); } }
private IEnumerator LoadConnections(ZoneData to, ZoneData from) { foreach (var index in to.Connections) { var data = Zones[index]; if (data.State == ZoneState.Unloaded || data.State == ZoneState.Unloading) { StartCoroutine(LoadZone(data)); } } while (_pendingLoads > 0) { yield return(null); } foreach (var loadedZone in LoadedZones) { var enabled = loadedZone.Zone.MapLayer == to.Zone.MapLayer; if (enabled && !loadedZone.IsEnabled) { loadedZone.Enabled(); yield return(null); // wait for Start and OnEnabled calls loadedZone.RestoreNpcData(); } if (!enabled && loadedZone.IsEnabled) { loadedZone.PersistNpcData(); loadedZone.Disabled(); } } }
protected static VariableValue GetName(ZoneData zone) => VariableValue.Create(zone.Zone.Name);
protected static VariableValue GetPlayer(ZoneData zone) => VariableValue.Create(Player.Instance);
protected static VariableValue GetWorld(ZoneData zone) => VariableValue.Create(zone.World);
private void EnteringLoadedZone(ZoneLoadStatus status, ZoneData to, ZoneData from) { StartCoroutine(LoadConnections(to, from)); EnteringZone(to, from); status.IsDone = true; }
protected static VariableValue GetState(ZoneData zone) => VariableValue.Create(zone.State.ToString());
protected static VariableValue GetIsActive(ZoneData zone) => VariableValue.Create(zone.IsActive);
private void DestroyZone(ZoneData zone) { Destroy(zone); }