public bool Initialize(SimulationRoot simulationRoot, int playerServerId, List <ITAlertPlayer> players) { try { SimulationRoot = simulationRoot; Players = players; LogProxy.Warning($"Initializing Director for simulation Instance {InstanceId}"); //TODO: get rid of this hacky sack PlayerCommands.Director = this; ItemPanel = transform.FindComponent <ItemPanel>("Canvas/ItemPanel"); _queuedMessages = new Queue <TickMessage>(); ResetDirector(); // all of this must happen after reset // TODO: this should probably be pushed into the ECS if (SimulationRoot.ECS.TryGetSystem <ICommandSystem>(out var commandSystem) == false) { throw new SimulationIntegrationException("Could not locate command processing system"); } CommandSystem = commandSystem; CalculateNetworkOffset(); CreateInitialEntities(); SetupPlayers(players, playerServerId); // item panel must come after players GetComponentsInChildren <Canvas>(true).ToList().ForEach(c => c.gameObject.SetActive(true)); ItemPanel.Initialize(); ItemPanel.ExplicitUpdate(); SimulationRoot.ECS.EntityRegistry.EntityDestroyed += EntityRegistryOnEntityDestroyed; if (SimulationRoot.ECS.TryGetSystem(out _endGameSystem) == false) { throw new SimulationIntegrationException("Could not locate end game system"); } CultureInfo.CurrentCulture = new CultureInfo("en"); _updatethread = new Thread(ThreadWorker) { IsBackground = true }; _updatethread.Start(); return(true); } catch (Exception ex) { LogProxy.Error($"Error initializing Director: {ex}"); throw; } }
private void UpdateEntityStates() { try { foreach (var entity in SimulationRoot.ECS.Entities) { try { if (_terminateSignal.WaitOne(0)) { LogProxy.Warning("Aborting UpdateEntityStates."); return; } if (TryGetEntity(entity.Key, out var uiEntity)) { uiEntity.UpdateEntityState(); } else { var newUiEntity = CreateEntity(entity.Value); newUiEntity.EntityBehaviour?.Initialize(entity.Value, this); } } catch (Exception ex) { throw new SimulationIntegrationException($"Error updating entity {entity.Key}", ex); } } int[] destroyedEntities; lock (_destroyedEntityLock) { destroyedEntities = _destroyedEntities.ToArray(); _destroyedEntities.Clear(); } foreach (var entityToRemove in destroyedEntities) { if (_terminateSignal.WaitOne(0)) { LogProxy.Warning("Aborting UpdateEntityStates."); return; } if (TryGetEntity(entityToRemove, out var uiEntity)) { Destroy(uiEntity.GameObject); } _trackedEntities.Remove(entityToRemove); } foreach (var untrackedEntity in _untrackedEntities) { if (_terminateSignal.WaitOne(0)) { LogProxy.Warning("Aborting UpdateEntityStates."); return; } untrackedEntity.UpdateEntityState(); } if (_terminateSignal.WaitOne(0)) { return; } ItemPanel.ExplicitUpdate(); _updateCompleteSignal.Set(); } catch (Exception exception) { LogProxy.Error($"Error updating Director {InstanceId} state: {exception}"); throw new SimulationIntegrationException("Error updating Director state", exception); } }