示例#1
0
        public void AddAndUpdateEntity()
        {
            GameSnapshot   snapshot  = (GameSnapshot)LevelManager.CreateSnapshot();
            ITemplateGroup templates = LevelManager.CreateTemplateGroup();

            snapshot.Systems.Add(new SystemEventLogger(new Type[] { }));

            IEntity entity = snapshot.CreateEntity(GameSnapshot.EntityAddTarget.Added);

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();
            engine.SynchronizeState().Wait();

            List <TriggerEvent> events = new List <TriggerEvent>();

            events.Add(TriggerEvent.OnAdded);
            events.Add(TriggerEvent.OnGlobalPreUpdate);
            events.Add(TriggerEvent.OnUpdate);
            events.Add(TriggerEvent.OnGlobalPostUpdate);
            Assert.Equal(events, engine.GetSystem <SystemEventLogger>().Events);

            for (int i = 0; i < 20; ++i)
            {
                engine.Update().Wait();
                engine.SynchronizeState().Wait();

                events.Add(TriggerEvent.OnGlobalPreUpdate);
                events.Add(TriggerEvent.OnUpdate);
                events.Add(TriggerEvent.OnGlobalPostUpdate);
                Assert.Equal(events, engine.GetSystem <SystemEventLogger>().Events);
            }
        }
示例#2
0
        public void CreateEngineOnEngineLoaded(IGameSnapshot snapshot, ITemplateGroup templates)
        {
            snapshot.Systems.Add(new OnEngineLoadedSystem());

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            Assert.Equal(1, engine.GetSystem <OnEngineLoadedSystem>().CallCount);
        }
示例#3
0
        public void CreateEngine(IGameSnapshot snapshot, ITemplateGroup templateGroup) {
            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templateGroup).Value;

            for (int i = 0; i < 20; ++i) {
                engine.Update().Wait();
                engine.SynchronizeState().Wait();
                engine.DispatchEvents();
            }
        }
示例#4
0
        public void ToFromContentDatabase(IGameSnapshot snapshot0, ITemplateGroup templates)
        {
            IGameEngine engine0 = GameEngineFactory.CreateEngine(snapshot0, templates).Value;

            IGameSnapshot snapshot1 = engine0.TakeSnapshot();
            IGameEngine   engine1   = GameEngineFactory.CreateEngine(snapshot1, templates).Value;

            Assert.Equal(engine0.GetVerificationHash(), engine1.GetVerificationHash());
        }
示例#5
0
        public void CreateEngine(IGameSnapshot snapshot, ITemplateGroup templateGroup)
        {
            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templateGroup).Value;

            for (int i = 0; i < 20; ++i)
            {
                engine.Update().Wait();
                engine.SynchronizeState().Wait();
                engine.DispatchEvents();
            }
        }
示例#6
0
        public void TestGlobalInputOnlySystem(IGameSnapshot snapshot, ITemplateGroup templates) {
            snapshot.Systems.Add(new GlobalInputSystem());

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update(new List<IGameInput>() { new MyInputType() }).Wait();
            engine.SynchronizeState().Wait();
            Assert.Equal(1, engine.GetSystem<GlobalInputSystem>().CallCount);

            engine.Update(new List<IGameInput>() { new MyInputType(), new MyInputType() }).Wait();
            engine.SynchronizeState().Wait();
            Assert.Equal(3, engine.GetSystem<GlobalInputSystem>().CallCount);
        }
示例#7
0
 /// <summary>
 /// Loads an ITemplateGroup from the given JSON. The JSON should have been generated by
 /// calling SaveTemplates.
 /// </summary>
 /// <exception cref="DeserializationException">An error loading the group
 /// occurred.</exception>
 public static ITemplateGroup LoadTemplateGroup(string json)
 {
     try {
         ITemplateGroup group = SerializationHelpers.Deserialize <TemplateGroup>(json,
                                                                                 RequiredConverters.GetConverters(),
                                                                                 RequiredConverters.GetContextObjects(Maybe <GameEngine> .Empty,
                                                                                                                      new TemplateConversionContext()));
         return(group);
     }
     catch (Exception e) {
         throw new DeserializationException(json, e);
     }
 }
示例#8
0
        public void SendRemoveFromContentDatabase(IGameSnapshot snapshot, ITemplateGroup templates)
        {
            snapshot.Systems.Add(new SystemCounter()
            {
                Filter = new Type[] { }
            });

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();
            engine.SynchronizeState().Wait();

            Assert.Equal(snapshot.RemovedEntities.Count(), engine.GetSystem <SystemCounter>().RemovedCount);
        }
示例#9
0
        /// <summary>
        /// Merges a set of serialized template groups together into one template group. An
        /// InvalidOperationException is thrown if there are two templates with the same TemplateId.
        /// </summary>
        /// <param name="groups">The serialized template groups to merge.</param>
        /// <returns>A serialized template group that contains all of the templates within the given
        /// groups.</returns>
        public static string MergeTemplateGroups(IEnumerable <string> groups)
        {
            // deserialize all of the groups
            List <ITemplateGroup> deserializedGroups = new List <ITemplateGroup>();

            foreach (string serializedGroup in groups)
            {
                var group = LoadTemplateGroup(serializedGroup);
                deserializedGroups.Add(group);
            }

            // merge them
            ITemplateGroup merged = MergeTemplateGroups(deserializedGroups);

            // return the serialized merge result
            return(SaveTemplateGroup(merged));
        }
示例#10
0
        public void RemodifyCurrentData()
        {
            IGameSnapshot snapshot = LevelManager.CreateSnapshot();
            IEntity       entity   = snapshot.CreateEntity();

            entity.AddData <DataConcurrentNonVersioned>();
            entity.AddData <DataConcurrentVersioned>();
            snapshot.Systems.Add(new DoubleModifySystem());

            ITemplateGroup templates = LevelManager.CreateTemplateGroup();

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            for (int i = 0; i < 20; ++i)
            {
                engine.Update().Wait();
                engine.SynchronizeState().Wait();
            }
        }
示例#11
0
        public void TestGlobalInputOnlySystem(IGameSnapshot snapshot, ITemplateGroup templates)
        {
            snapshot.Systems.Add(new GlobalInputSystem());

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update(new List <IGameInput>()
            {
                new MyInputType()
            }).Wait();
            engine.SynchronizeState().Wait();
            Assert.Equal(1, engine.GetSystem <GlobalInputSystem>().CallCount);

            engine.Update(new List <IGameInput>()
            {
                new MyInputType(), new MyInputType()
            }).Wait();
            engine.SynchronizeState().Wait();
            Assert.Equal(3, engine.GetSystem <GlobalInputSystem>().CallCount);
        }
示例#12
0
        public void GotAddedEventsForInitialDatabase(IGameSnapshot snapshot, ITemplateGroup templates) {
            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();

            int notifiedCount = 0;
            engine.EventNotifier.OnEvent<EntityAddedEvent>(evnt => {
                ++notifiedCount;
            });

            engine.DispatchEvents();

            Assert.Equal(1 + snapshot.AddedEntities.Count() + snapshot.ActiveEntities.Count() +
                snapshot.RemovedEntities.Count(), notifiedCount);

            engine.SynchronizeState().Wait();
            engine.Update().Wait();

            notifiedCount = 0;
            engine.DispatchEvents();
            Assert.Equal(0, notifiedCount);
        }
示例#13
0
        public void CorrectUpdateCount()
        {
            GameSnapshot   snapshot  = (GameSnapshot)LevelManager.CreateSnapshot();
            ITemplateGroup templates = LevelManager.CreateTemplateGroup();

            for (int i = 0; i < 10; ++i)
            {
                IEntity entity = snapshot.CreateEntity(GameSnapshot.EntityAddTarget.Active);
                entity.AddData <DataEmpty>();
            }

            snapshot.Systems.Add(new SystemCounter()
            {
                Filter = new[] { typeof(DataEmpty) }
            });

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();
            engine.SynchronizeState().Wait();

            Assert.Equal(snapshot.ActiveEntities.Count(), engine.GetSystem <SystemCounter>().UpdateCount);
        }
示例#14
0
        public void RemoveEntityAndModifyInRemoveNotification()
        {
            GameSnapshot   snapshot  = (GameSnapshot)LevelManager.CreateSnapshot();
            ITemplateGroup templates = LevelManager.CreateTemplateGroup();

            snapshot.Systems.Add(new SystemEventLogger(new Type[] { }));
            snapshot.Systems.Add(new ModifyOnRemovedTrigger());

            {
                IEntity e = snapshot.CreateEntity(GameSnapshot.EntityAddTarget.Removed);
                e.AddData <DataEmpty>();
            }

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();
            engine.SynchronizeState().Wait();

            List <TriggerEvent> events = new List <TriggerEvent>();

            events.Add(
                TriggerEvent.OnRemoved,
                TriggerEvent.OnGlobalPreUpdate,
                TriggerEvent.OnGlobalPostUpdate);
            Assert.Equal(events, engine.GetSystem <SystemEventLogger>().Events);

            for (int i = 0; i < 20; ++i)
            {
                engine.Update().Wait();
                engine.SynchronizeState().Wait();

                events.Add(
                    TriggerEvent.OnGlobalPreUpdate,
                    TriggerEvent.OnGlobalPostUpdate);
                Assert.Equal(events, engine.GetSystem <SystemEventLogger>().Events);
            }
        }
示例#15
0
        public void GotAddedEventsForInitialDatabase(IGameSnapshot snapshot, ITemplateGroup templates)
        {
            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();

            int notifiedCount = 0;

            engine.EventNotifier.OnEvent <EntityAddedEvent>(evnt => {
                ++notifiedCount;
            });

            engine.DispatchEvents();

            Assert.Equal(1 + snapshot.AddedEntities.Count() + snapshot.ActiveEntities.Count() +
                         snapshot.RemovedEntities.Count(), notifiedCount);

            engine.SynchronizeState().Wait();
            engine.Update().Wait();

            notifiedCount = 0;
            engine.DispatchEvents();
            Assert.Equal(0, notifiedCount);
        }
示例#16
0
 /// <summary>
 /// Creates a new game engine that can be used to simulate the game using the content from
 /// the given content database. The passed in snapshot will not be modified.
 /// </summary>
 /// <remarks>
 /// This is a helper method; it serializes the arguments and then calls CreateEngine(string,
 /// string) which does the actual work.
 /// </remarks>
 /// <param name="snapshot">The IGameSnapshot to use to create the engine.</param>
 /// <param name="templates">The ITemplateGroup used to create the engine.</param>
 /// <returns>A game engine that can play the given content.</returns>
 public static Maybe<IGameEngine> CreateEngine(IGameSnapshot snapshot, ITemplateGroup templates) {
     return CreateEngine(LevelManager.SaveSnapshot(snapshot),
         LevelManager.SaveTemplateGroup(templates));
 }
示例#17
0
        public static IGameSnapshot Snapshot3(ITemplateGroup templates)
        {
            var builder = new TestSnapshotBuilder();

            {
                builder.NewEntity()
                .AddData(new DataEmpty());

                IEntity entity1 = builder.NewEntity()
                                  .AddData(new DataInt()
                {
                    A = 10
                })
                                  .Entity;

                builder.NewEntity()
                .AddData(new DataEmpty())
                .AddData(new DataInt()
                {
                    A = 20
                });

                IEntity entity3 = builder.NewEntity()
                                  .AddData(new DataDataReference()
                {
                    DataReference = CreateDataReference <DataEmpty>(entity1)
                })
                                  .Entity;

                builder.NewEntity()
                .AddData(new DataEntity()
                {
                    Entity = entity3
                });

                IEntity entity5 = builder.Snapshot.CreateEntity();
                entity5.AddData <DataEntity>().Entity = entity5;

                builder.NewEntity()
                .AddData(new DataQueryableEntity()
                {
                    QueryableEntity = entity5
                });

                IEntity entity7 = builder.Snapshot.CreateEntity();
                entity7.AddData <DataQueryableEntity>().QueryableEntity = entity7;
            }

            if (templates.Templates.Count() > 0)
            {
                ITemplate template = templates.Templates.First();

                builder.NewEntity()
                .AddData(new DataDataReference()
                {
                    DataReference = CreateDataReference <DataEmpty>(template)
                });

                builder.NewEntity()
                .AddData(new DataQueryableEntity()
                {
                    QueryableEntity = template
                });
            }

            return(builder.Snapshot);
        }
示例#18
0
        public void SendRemoveFromContentDatabase(IGameSnapshot snapshot, ITemplateGroup templates) {
            snapshot.Systems.Add(new SystemCounter() {
                Filter = new Type[] { }
            });

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;

            engine.Update().Wait();
            engine.SynchronizeState().Wait();

            Assert.Equal(snapshot.RemovedEntities.Count(), engine.GetSystem<SystemCounter>().RemovedCount);
        }
示例#19
0
        public void ToFromContentDatabase(IGameSnapshot snapshot0, ITemplateGroup templates) {
            IGameEngine engine0 = GameEngineFactory.CreateEngine(snapshot0, templates).Value;

            IGameSnapshot snapshot1 = engine0.TakeSnapshot();
            IGameEngine engine1 = GameEngineFactory.CreateEngine(snapshot1, templates).Value;

            Assert.Equal(engine0.GetVerificationHash(), engine1.GetVerificationHash());
        }
示例#20
0
 private static void AddData(List <object[]> data,
                             Func <ITemplateGroup, IGameSnapshot> snapshot, ITemplateGroup templateGroup)
 {
     data.Add(new object[] { snapshot(templateGroup), templateGroup });
 }
示例#21
0
        public static IGameSnapshot Snapshot2(ITemplateGroup templates)
        {
            TestSnapshotBuilder builder = new TestSnapshotBuilder();

            {
                builder.NewEntity();

                builder.NewEntity()
                .AddData(new DataEmpty());

                IEntity entity1 = builder.NewEntity()
                                  .AddData(new DataInt()
                {
                    A = 10
                })
                                  .Entity;

                builder.NewEntity()
                .AddData(new DataEmpty())
                .AddData(new DataInt()
                {
                    A = 20
                });

                builder.NewEntity()
                .AddData(new DataDataReference()
                {
                    DataReference = CreateDataReference <DataEmpty>(entity1)
                });

                builder.NewEntity()
                .AddData <DataNonVersionedInt>(data => data.A = 500);

                builder.NewEntity()
                .AddData(new DataEmpty())
                .AddData <DataNonVersionedInt>(data => data.A = 510);
            }

            {
                builder.NewEntity(GameSnapshot.EntityAddTarget.Active);

                for (int i = 0; i < 5; ++i)
                {
                    builder.NewEntity(GameSnapshot.EntityAddTarget.Active)
                    .AddData(new DataEmpty());

                    builder.NewEntity(GameSnapshot.EntityAddTarget.Active)
                    .AddData(new DataInt {
                        A = 30
                    });

                    builder.NewEntity(GameSnapshot.EntityAddTarget.Active)
                    .AddData(new DataEmpty())
                    .AddData(new DataInt {
                        A = 40
                    });
                }
            }

            {
                builder.NewEntity(GameSnapshot.EntityAddTarget.Removed)
                .AddData(new DataEmpty());

                builder.NewEntity(GameSnapshot.EntityAddTarget.Removed)
                .AddData(new DataInt {
                    A = 70
                });

                builder.NewEntity(GameSnapshot.EntityAddTarget.Removed)
                .AddData(new DataEmpty())
                .AddData(new DataInt()
                {
                    A = 80
                });
            }

            return(builder.Snapshot);
        }
示例#22
0
 /// <summary>
 /// Converts a template group to JSON that can be restored later.
 /// </summary>
 public static string SaveTemplateGroup(ITemplateGroup templates) {
     return SerializationHelpers.Serialize<TemplateGroup>((TemplateGroup)templates,
         RequiredConverters.GetConverters(),
         RequiredConverters.GetContextObjects(Maybe<GameEngine>.Empty));
 }
示例#23
0
        public void CreateEngineOnEngineLoaded(IGameSnapshot snapshot, ITemplateGroup templates) {
            snapshot.Systems.Add(new OnEngineLoadedSystem());

            IGameEngine engine = GameEngineFactory.CreateEngine(snapshot, templates).Value;
            Assert.Equal(1, engine.GetSystem<OnEngineLoadedSystem>().CallCount);
        }
示例#24
0
 /// <summary>
 /// Converts a template group to JSON that can be restored later.
 /// </summary>
 public static string SaveTemplateGroup(ITemplateGroup templates)
 {
     return(SerializationHelpers.Serialize <TemplateGroup>((TemplateGroup)templates,
                                                           RequiredConverters.GetConverters(),
                                                           RequiredConverters.GetContextObjects(Maybe <GameEngine> .Empty)));
 }
示例#25
0
 /// <summary>
 /// Creates a new game engine that can be used to simulate the game using the content from
 /// the given content database. The passed in snapshot will not be modified.
 /// </summary>
 /// <remarks>
 /// This is a helper method; it serializes the arguments and then calls CreateEngine(string,
 /// string) which does the actual work.
 /// </remarks>
 /// <param name="snapshot">The IGameSnapshot to use to create the engine.</param>
 /// <param name="templates">The ITemplateGroup used to create the engine.</param>
 /// <returns>A game engine that can play the given content.</returns>
 public static Maybe <IGameEngine> CreateEngine(IGameSnapshot snapshot, ITemplateGroup templates)
 {
     return(CreateEngine(LevelManager.SaveSnapshot(snapshot),
                         LevelManager.SaveTemplateGroup(templates)));
 }
示例#26
0
 public static IGameSnapshot Snapshot1(ITemplateGroup templates)
 {
     return(LevelManager.CreateSnapshot());
 }