private void Update() { if (_uiText != null) { _timer -= Time.deltaTime; while (_timer <= 0f) { _timer += _timePerCharacter; _characterIndex++; string text = _textToWrite.Substring(0, _characterIndex); if (_invisibleCharacters) { text += "<color=#00000000>" + _textToWrite.Substring(_characterIndex) + "</color>"; } _uiText.text = text; if (_characterIndex >= _textToWrite.Length) { _uiText = null; _eventMediator.Broadcast(GlobalHelper.WritingFinished, this); _sound.stop(STOP_MODE.IMMEDIATE); return; } } } }
public void Show() { if (GameManager.CurrentScene.name.Equals(GlobalHelper.CombatScene)) { var combatInput = FindObjectOfType <CombatInputController>(); if (combatInput.TileSelected() || combatInput.AbilitySelected()) { return; } uiContainer.SetActive(true); GameManager.Instance.AddActiveWindow(uiContainer); var combatManager = FindObjectOfType <CombatManager>(); QuitButton.interactable = combatManager.IsPlayerTurn(); RetreatButton.interactable = combatManager.IsPlayerTurn(); return; } uiContainer.SetActive(true); GameManager.Instance.AddActiveWindow(uiContainer); EventMediator eventMediator = FindObjectOfType <EventMediator>(); eventMediator.Broadcast(GlobalHelper.PauseTimer, this); }
public void Hide() { var loadWindow = GameObject.Find("LoadGameMask"); var settingsWindow = GameObject.Find("SettingsWindowMask"); EventMediator eventMediator = FindObjectOfType <EventMediator>(); if (GameManager.Instance.WindowActive(settingsWindow) || GameManager.Instance.WindowActive(loadWindow)) { return; } uiContainer.SetActive(false); GameManager.Instance.RemoveActiveWindow(uiContainer); if (GameManager.Instance.AnyActiveWindows()) { return; } eventMediator.Broadcast(GlobalHelper.ResumeTimer, this); }
//todo need refactor big time private void Update() { if (_isPlayerTurn) { if (!_isTileSelected) { if (MouseHitUi()) { ClearHighlights(); return; } HighlightTileUnderMouse(); } if (Input.GetMouseButtonDown(0)) { if (!_isTileSelected) { //check if ability is selected and if a valid target is clicked then use ability if true if (_isAbilitySelected) { //todo need method for this block - Get <T> from mouse position - entity, tile, floor whatever var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); var mouseCoord = new Coord((int)mousePosition.x, (int)mousePosition.y); var entity = (Entity)_map.Entities.GetItems(mouseCoord).FirstOrDefault(); if (entity != null && _selectedAbility.TargetValid(entity) && _selectedAbility.TargetInRange(entity)) { _selectedAbility.Use(entity); _selectedAbility = null; _isAbilitySelected = false; ClearHighlights(); GameManager.Instance.SetCursorToNormal(); _eventMediator.Broadcast(GlobalHelper.RefreshCombatUi, this, _combatManager.ActiveEntity); } } else { //todo only allow for tiles within entity movement range //todo need to block when interacting with ui if (MouseHitUi()) { ClearHighlights(); return; } HighlightPathToMouse(); } } else { //check if same tile clicked then move there if true var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); var mouseCoord = new Coord((int)mousePosition.x, (int)mousePosition.y); if (_map.OutOfBounds(mouseCoord)) { return; } var targetTile = _map.GetTerrain <Floor>(mouseCoord); if (targetTile == null || !targetTile.IsWalkable) { return; } if (ReferenceEquals(_selectedTile, targetTile)) { var activeEntity = _combatManager.ActiveEntity; if (_apMovementCost > activeEntity.Stats.CurrentActionPoints) { return; } activeEntity.MoveTo(targetTile, _apMovementCost, true, _highlightedTiles); _eventMediator.Broadcast(GlobalHelper.RefreshCombatUi, this, activeEntity); } _isTileSelected = false; _eventMediator.Broadcast(GlobalHelper.TileDeselected, this); } } else if (Input.GetMouseButtonDown(1)) { _isTileSelected = false; _isAbilitySelected = false; _eventMediator.Broadcast(GlobalHelper.TileDeselected, this); GameManager.Instance.SetCursorToNormal(); } else if (Input.GetKeyDown(KeyCode.Escape)) { //todo clear if anything selected //todo show menu if nothing selected _isTileSelected = false; _isAbilitySelected = false; _eventMediator.Broadcast(GlobalHelper.TileDeselected, this); GameManager.Instance.SetCursorToNormal(); } } }
public void RestoreState(object state) { if (state == null) { return; } CombatManagerDto dto = (CombatManagerDto)state; Enemies = new List <Entity>(); foreach (var enemy in dto.Enemies) { var restoredEnemy = new Entity(); restoredEnemy.RestoreState(enemy); Enemies.Add(restoredEnemy); } ActiveEntity = _travelManager.Party.GetCompanionById(dto.ActiveEntityId); Companions = new Dictionary <Entity, CompanionCombatStats>(); foreach (var id in dto.CompanionIds) { var companion = _travelManager.Party.GetCompanionById(id.Key); if (companion == null) { Debug.LogError($"Can't find companion with id {id.Key}"); continue; } Companions.Add(companion, id.Value); } TurnOrder = new Queue <Entity>(); foreach (var id in dto.TurnOrder) { Entity entity = null; foreach (var companion in Companions.Keys) { if (string.Equals(id, companion.Id, StringComparison.OrdinalIgnoreCase)) { entity = companion; break; } } if (entity == null) { foreach (var enemy in Enemies) { if (string.Equals(id, enemy.Id, StringComparison.OrdinalIgnoreCase)) { entity = enemy; break; } } } TurnOrder.Enqueue(entity); } Map = new CombatMap(MapGenerator.MapWidth, MapGenerator.MapHeight); Map.RestoreState(dto.CombatMap); _combatInput = FindObjectOfType <CombatInputController>(); _combatInput.SetMap(Map); _combatInput.ClearHighlights(); DrawMap(); var messenger = FindObjectOfType <CombatMessenger>(); messenger.RestoreState(dto.CombatMessenger); HighlightActiveEntitySprite(); _eventMediator = FindObjectOfType <EventMediator>(); _eventMediator.Broadcast(GlobalHelper.RefreshCombatUi, this, ActiveEntity); }
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(); } }