Exemplo n.º 1
0
        public Task Update(IEnumerable <IGameInput> input)
        {
            if (_nextState != GameEngineNextState.Update)
            {
                throw new InvalidOperationException("Invalid call to Update; was expecting " + _nextState);
            }

            if (_updateWaitTask != null &&
                _updateWaitTask.IsCompleted == false &&
                _updateWaitTask.IsFaulted == false &&
                _updateWaitTask.IsCanceled == false)
            {
                throw new InvalidOperationException("Currently running Update task has not finished");
            }

            _updateWaitTask = Task.Factory.StartNew(() => {
                DoUpdate(input.ToList());
                _nextState = GameEngineNextState.SynchronizeState;
            });

            return(_updateWaitTask);
        }
Exemplo n.º 2
0
        public Task SynchronizeState()
        {
            if (_nextState != GameEngineNextState.SynchronizeState)
            {
                throw new InvalidOperationException("Invalid call to SynchronizeState; was expecting " + _nextState);
            }

            if (_synchronizeStateTask != null &&
                _synchronizeStateTask.IsCompleted == false &&
                _synchronizeStateTask.IsFaulted == false &&
                _synchronizeStateTask.IsCanceled == false)
            {
                throw new InvalidOperationException("Cannot call SynchronizeState before the " +
                                                    "returned Task has completed");
            }

            _synchronizeStateTask = Task.Factory.StartNew(() => {
                UpdateEntitiesWithStateChanges();
                _nextState = GameEngineNextState.Update;
            });

            return(_synchronizeStateTask);
        }
Exemplo n.º 3
0
        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)
                    }
                }
            }
        }
Exemplo n.º 4
0
        public Task SynchronizeState() {
            if (_nextState != GameEngineNextState.SynchronizeState) {
                throw new InvalidOperationException("Invalid call to SynchronizeState; was expecting " + _nextState);
            }

            if (_synchronizeStateTask != null &&
                _synchronizeStateTask.IsCompleted == false &&
                _synchronizeStateTask.IsFaulted == false &&
                _synchronizeStateTask.IsCanceled == false) {
                throw new InvalidOperationException("Cannot call SynchronizeState before the " +
                    "returned Task has completed");
            }

            _synchronizeStateTask = Task.Factory.StartNew(() => {
                UpdateEntitiesWithStateChanges();
                _nextState = GameEngineNextState.Update;
            });

            return _synchronizeStateTask;
        }
Exemplo n.º 5
0
        public Task Update(IEnumerable<IGameInput> input) {
            if (_nextState != GameEngineNextState.Update) {
                throw new InvalidOperationException("Invalid call to Update; was expecting " + _nextState);
            }

            if (_updateWaitTask != null &&
                _updateWaitTask.IsCompleted == false &&
                _updateWaitTask.IsFaulted == false &&
                _updateWaitTask.IsCanceled == false) {
                throw new InvalidOperationException("Currently running Update task has not finished");
            }

            _updateWaitTask = Task.Factory.StartNew(() => {
                DoUpdate(input.ToList());
                _nextState = GameEngineNextState.SynchronizeState;
            });

            return _updateWaitTask;
        }
Exemplo n.º 6
0
        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)
                    }
                }
            }
        }