public object CaptureState() { var dto = new CombatManagerDto { ActiveEntityId = ActiveEntity.Id, CombatMap = Map.CaptureState(), TurnNumber = CurrentTurnNumber, TurnOrder = new Queue <string>(), CurrentState = _currentCombatState, Enemies = new List <object>(), CompanionIds = new Dictionary <string, CompanionCombatStats>() }; foreach (var companion in Companions) { dto.CompanionIds.Add(companion.Key.Id, companion.Value); } foreach (var enemy in Enemies) { dto.Enemies.Add(enemy.CaptureState()); } foreach (var entity in TurnOrder) { dto.TurnOrder.Enqueue(entity.Id); } var messenger = FindObjectOfType <CombatMessenger>(); dto.CombatMessenger = (Queue <string>)messenger.CaptureState(); return(dto); }
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); }