private void Update() { switch (_currentCombatState) { case CombatState.LoadingScene: //we want to wait until we have enemy combatants populated _musicController.EndTravelMusic(); _musicController.PlayBattleMusic(); //todo travel manager checks for testing in combat scene if (_travelManager != null && _travelManager.Party != null && Enemies != null && Enemies.Count > 0) { _currentCombatState = CombatState.Start; } break; case CombatState.Start: CurrentTurnNumber = 1; var party = _travelManager.Party.GetCompanions(); var combatants = new List <Entity>(); Companions = new Dictionary <Entity, CompanionCombatStats>(); foreach (var companion in party) { if (companion.IsDerpus()) { continue; } combatants.Add(companion); Companions.Add(companion, new CompanionCombatStats()); } combatants.AddRange(Enemies); Map = GenerateMap(combatants); DrawMap(); foreach (var combatant in combatants) { foreach (var ability in combatant.Abilities.Values) { ability.SetupForCombat(); } } TurnOrder = DetermineTurnOrder(combatants); ActiveEntity = TurnOrder.Peek(); ActiveEntity.RefillActionPoints(); HighlightActiveEntitySprite(); if (ActiveEntityPlayerControlled()) { _currentCombatState = CombatState.PlayerTurn; } else { _currentCombatState = CombatState.StartAiTurn; } SubscribeToEvents(); _eventMediator.Broadcast(GlobalHelper.CombatSceneLoaded, this, Map); _eventMediator.Broadcast(RefreshUi, this, ActiveEntity); break; case CombatState.PlayerTurn: _eventMediator.Broadcast(GlobalHelper.PlayerTurn, this); var activePlayerSprite = ActiveEntity.CombatSpriteInstance; if (activePlayerSprite == null) { RemoveEntity(ActiveEntity); _currentCombatState = CombatState.EndTurn; return; } var playerController = activePlayerSprite.GetComponent <AiController>(); if (ReferenceEquals(playerController, null)) { return; } _currentCombatState = CombatState.AiTurn; playerController.TakeAction(); break; case CombatState.StartAiTurn: _currentCombatState = CombatState.AiTurn; _eventMediator.Broadcast(GlobalHelper.AiTurn, this); var activeSprite = ActiveEntity.CombatSpriteInstance; if (ReferenceEquals(activeSprite, null)) { return; } var aiController = activeSprite.GetComponent <AiController>(); if (ReferenceEquals(aiController, null)) { return; } aiController.TakeAction(); break; case CombatState.AiTurn: break; case CombatState.EndTurn: if (IsCombatFinished()) { _currentCombatState = CombatState.EndCombat; } else { ActiveEntity = GetNextInTurnOrder(); MoveCameraToObject(ActiveEntity.CombatSpriteInstance.transform); ActiveEntity.RefillActionPoints(); ActiveEntity.UpdateMovedFlags(); HighlightActiveEntitySprite(); if (ActiveEntityPlayerControlled()) { _currentCombatState = CombatState.PlayerTurn; } else { _currentCombatState = CombatState.StartAiTurn; } CurrentTurnNumber++; _eventMediator.Broadcast(RefreshUi, this, ActiveEntity); ActiveEntity.TriggerEffects(); } break; case CombatState.EndCombat: _eventMediator.UnsubscribeFromAllEvents(this); if (_result != CombatResult.Retreat) { if (PlayerRetreated()) { _result = CombatResult.Retreat; //_musicController.PlayBattleVictoryMusic(); _musicController.EndBattleMusic(); } else if (PlayerDead()) { _result = CombatResult.Defeat; _musicController.PlayBattleGameOverMusic(); } else { _result = CombatResult.Victory; //_musicController.PlayBattleVictoryMusic(); _musicController.EndBattleMusic(); } } GlobalHelper.InvokeAfterDelay(() => DisplayPostCombatPopup(_result), 2.5f); _currentCombatState = CombatState.NotActive; break; case CombatState.NotActive: break; case CombatState.LoadFromSave: _eventMediator.UnsubscribeFromAllEvents(this); SubscribeToEvents(); _musicController.PlayBattleMusic(); _eventMediator.Broadcast(GlobalHelper.CombatSceneLoaded, this, Map); _eventMediator.Broadcast(RefreshUi, this, ActiveEntity); MoveCameraToObject(ActiveEntity.CombatSpriteInstance.transform); _currentCombatState = CombatState.PlayerTurn; break; default: throw new ArgumentOutOfRangeException(); } }