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); } }
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); }
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(); } }
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()); }
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); }
/// <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); } }
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); }
/// <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)); }
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(); } }
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); }
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); }
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); }
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); } }
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); }
/// <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)); }
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); }
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); }
private static void AddData(List <object[]> data, Func <ITemplateGroup, IGameSnapshot> snapshot, ITemplateGroup templateGroup) { data.Add(new object[] { snapshot(templateGroup), templateGroup }); }
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); }
/// <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)); }
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); }
/// <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))); }
/// <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))); }
public static IGameSnapshot Snapshot1(ITemplateGroup templates) { return(LevelManager.CreateSnapshot()); }