/// <summary> /// Creates a multithreaded system from the given base system. /// </summary> private MultithreadedSystem CreateMultithreadedSystem(ISystem baseSystem, TemplateIndex templateIndex) { // initialize references in the system baseSystem.EventDispatcher = EventNotifier; baseSystem.GlobalEntity = _globalEntity; baseSystem.EntityIndex = _entityIndex; baseSystem.TemplateIndex = templateIndex; MultithreadedSystem multithreadingSystem = new MultithreadedSystem(this, baseSystem); foreach (var entity in _entities) { multithreadingSystem.Restore(entity); } return(multithreadingSystem); }
public GameEngine(string snapshotJson, string templateJson) { _templateJson = templateJson; // Create our own little island of references with its own set of templates GameSnapshotRestorer restorer = GameSnapshotRestorer.Restore( snapshotJson, templateJson, Maybe.Just(this)); GameSnapshot snapshot = restorer.GameSnapshot; EntityIdGenerator = snapshot.EntityIdGenerator; _systems = snapshot.Systems; _entityIndex = new EntityIndex(); // TODO: ensure that when correctly restore UpdateNumber //UpdateNumber = updateNumber; _globalEntity = (RuntimeEntity)snapshot.GlobalEntity; EventNotifier.Submit(EntityAddedEvent.Create(_globalEntity)); foreach (var entity in snapshot.AddedEntities) { AddEntity((RuntimeEntity)entity); } foreach (var entity in snapshot.ActiveEntities) { RuntimeEntity runtimeEntity = (RuntimeEntity)entity; // add the entity InternalAddEntity(runtimeEntity); // TODO: verify that if the modification notifier is already triggered, we can // ignore this //if (((ContentEntity)entity).HasModification) { // runtimeEntity.ModificationNotifier.Notify(); //} // done via InternalAddEntity //if (deserializedEntity.HasStateChange) { // deserializedEntity.Entity.DataStateChangeNotifier.Notify(); //} } foreach (var entity in snapshot.RemovedEntities) { RuntimeEntity runtimeEntity = (RuntimeEntity)entity; // add the entity InternalAddEntity(runtimeEntity); // TODO: verify that if the modification notifier is already triggered, we can // ignore this //if (((ContentEntity)entity).HasModification) { // runtimeEntity.ModificationNotifier.Notify(); //} // done via InternalAddEntity //if (deserializedEntity.HasStateChange) { // deserializedEntity.Entity.DataStateChangeNotifier.Notify(); //} RemoveEntity(runtimeEntity); } TemplateIndex templateIndex = new TemplateIndex(restorer.Templates.Templates); _executionGroups = new List <ExecutionGroup>(); var executionGroups = SystemExecutionGroup.GetExecutionGroups(snapshot.Systems); foreach (var executionGroup in executionGroups) { List <MultithreadedSystem> multithreadedSystems = new List <MultithreadedSystem>(); foreach (var system in executionGroup.Systems) { MultithreadedSystem multithreaded = CreateMultithreadedSystem(system, templateIndex); multithreadedSystems.Add(multithreaded); } _executionGroups.Add(new ExecutionGroup(multithreadedSystems)); } _nextState = GameEngineNextState.SynchronizeState; SynchronizeState().Wait(); // call of the engine loaded systems foreach (var group in _executionGroups) { foreach (var system in group.Systems) { var onLoaded = system.System as Trigger.OnEngineLoaded; if (onLoaded != null) { onLoaded.OnEngineLoaded(EventNotifier); // TODO: verify that OnEngineLoaded didn't change any state (via hashing) } } } }
/// <summary> /// Creates a multithreaded system from the given base system. /// </summary> private MultithreadedSystem CreateMultithreadedSystem(ISystem baseSystem, TemplateIndex templateIndex) { // initialize references in the system baseSystem.EventDispatcher = EventNotifier; baseSystem.GlobalEntity = _globalEntity; baseSystem.EntityIndex = _entityIndex; baseSystem.TemplateIndex = templateIndex; MultithreadedSystem multithreadingSystem = new MultithreadedSystem(this, baseSystem); foreach (var entity in _entities) { multithreadingSystem.Restore(entity); } return multithreadingSystem; }