From 14783deb7e13b7d8782ec625df4ac7eae341f7c5 Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Thu, 25 Jul 2024 16:52:43 +0200 Subject: [PATCH] reduced line length for wiki examples --- src/Tests/ECS/Examples/Component_Types.cs | 40 +++++++++++------------ src/Tests/ECS/Examples/General.cs | 26 +++++++-------- src/Tests/ECS/Examples/Optimization.cs | 26 +++++++-------- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/Tests/ECS/Examples/Component_Types.cs b/src/Tests/ECS/Examples/Component_Types.cs index 4295b562..299ac405 100644 --- a/src/Tests/ECS/Examples/Component_Types.cs +++ b/src/Tests/ECS/Examples/Component_Types.cs @@ -35,22 +35,22 @@ public static void LinkComponents() { var store = new EntityStore(); - var entity1 = store.CreateEntity(1); // link components - var entity2 = store.CreateEntity(2); // symbolized as → - var entity3 = store.CreateEntity(3); // 1 2 3 + var entity1 = store.CreateEntity(1); // link components + var entity2 = store.CreateEntity(2); // symbolized as → + var entity3 = store.CreateEntity(3); // 1 2 3 // add a link component to entity (2) referencing entity (1) - entity2.AddComponent(new AttackComponent { target = entity1 }); // 1 ← 2 3 + entity2.AddComponent(new AttackComponent { target = entity1 }); // 1 ← 2 3 // get all incoming links of given type. O(1) // entity1.GetIncomingLinks(); // { 2 } // update link component of entity (2). It links now entity (3) - entity2.AddComponent(new AttackComponent { target = entity3 }); // 1 2 → 3 + entity2.AddComponent(new AttackComponent { target = entity3 }); // 1 2 → 3 entity1.GetIncomingLinks(); // { } entity3.GetIncomingLinks(); // { 2 } // deleting a linked entity (3) removes all link components referencing it - entity3.DeleteEntity(); // 1 2 + entity3.DeleteEntity(); // 1 2 entity2.HasComponent (); // false } #endregion @@ -83,28 +83,28 @@ public static void LinkRelations() { var store = new EntityStore(); - var entity1 = store.CreateEntity(1); // link relations - var entity2 = store.CreateEntity(2); // symbolized as → - var entity3 = store.CreateEntity(3); // 1 2 3 + var entity1 = store.CreateEntity(1); // link relations + var entity2 = store.CreateEntity(2); // symbolized as → + var entity3 = store.CreateEntity(3); // 1 2 3 // add a link relation to entity (2) referencing entity (1) - entity2.AddRelation(new AttackRelation { target = entity1 }); // 1 ← 2 3 + entity2.AddRelation(new AttackRelation { target = entity1 }); // 1 ← 2 3 // get all links added to the entity. O(1) // entity2.GetRelations (); // { 1 } // get all incoming links. O(1) // entity1.GetIncomingLinks(); // { 2 } // add another one. An entity can have multiple link relations - entity2.AddRelation(new AttackRelation { target = entity3 }); // 1 ← 2 → 3 + entity2.AddRelation(new AttackRelation { target = entity3 }); // 1 ← 2 → 3 entity2.GetRelations (); // { 1, 3 } entity3.GetIncomingLinks(); // { 2 } // deleting a linked entity (1) removes all link relations referencing it - entity1.DeleteEntity(); // 2 → 3 + entity1.DeleteEntity(); // 2 → 3 entity2.GetRelations (); // { 3 } // deleting entity (2) is reflected by incoming links query - entity2.DeleteEntity(); // 3 + entity2.DeleteEntity(); // 3 entity3.GetIncomingLinks(); // { } } #endregion @@ -147,14 +147,14 @@ public static void RelationComponents() entity.AddRelation(new InventoryItem { type = ItemType.Axe, count = 3 }); // Get all relations added to an entity. O(1) - entity.GetRelations (); // { Coin, Axe } + entity.GetRelations (); // { Coin, Axe } // Get a specific relation from an entity. O(1) - entity.GetRelation (ItemType.Coin); // { type = Coin, count = 42 } + entity.GetRelation (ItemType.Coin); // {type=Coin, count=42} // Remove a specific relation from an entity entity.RemoveRelation(ItemType.Axe); - entity.GetRelations (); // { Coin } + entity.GetRelations (); // { Coin } } #endregion @@ -176,17 +176,17 @@ public static void IndexedComponents() entity.AddComponent(new Player { name = $"Player-{n,0:000}"}); } // get all entities where Player.name == "Player-001". O(1) - store.GetEntitiesWithComponentValue("Player-001"); // Count: 1 + store.GetEntitiesWithComponentValue("Player-001"); // Count: 1 // return same result as lookup using a Query(). O(1) - store.Query().HasValue ("Player-001"); // Count: 1 + store.Query().HasValue ("Player-001"); // Count: 1 // return all entities with a Player.name in the given range. // O(N ⋅ log N) - N: all unique player names - store.Query().ValueInRange("Player-000", "Player-099"); // Count: 100 + store.Query().ValueInRange("Player-000", "Player-099"); // Count: 100 // get all unique Player.name's. O(1) - store.GetAllIndexedComponentValues(); // Count: 1000 + store.GetAllIndexedComponentValues(); // Count: 1000 } #endregion diff --git a/src/Tests/ECS/Examples/General.cs b/src/Tests/ECS/Examples/General.cs index 05f842e8..ac1a7603 100644 --- a/src/Tests/ECS/Examples/General.cs +++ b/src/Tests/ECS/Examples/General.cs @@ -31,7 +31,7 @@ public static void CreateEntity() foreach (var entity in store.Entities) { Console.WriteLine($"entity {entity}"); } - // > entity id: 1 [] Info: [] shows entity has no components, tags or scripts + // > entity id: 1 [] Info: [] entity has no components // > entity id: 2 [] } @@ -42,7 +42,7 @@ public static void DeleteEntity() var entity = store.CreateEntity(); entity.DeleteEntity(); var isDeleted = entity.IsNull; - Console.WriteLine($"deleted: {isDeleted}"); // > deleted: True + Console.WriteLine($"deleted: {isDeleted}"); // > deleted: True } [Test] @@ -51,13 +51,13 @@ public static void DisableEntity() var store = new EntityStore(); var entity = store.CreateEntity(); entity.Enabled = false; - Console.WriteLine(entity); // > id: 1 [#Disabled] + Console.WriteLine(entity); // > id: 1 [#Disabled] var query = store.Query(); - Console.WriteLine($"default - {query}"); // > default - Query: [] Count: 0 + Console.WriteLine($"default - {query}"); // > default - Query: [] Count: 0 var disabled = store.Query().WithDisabled(); - Console.WriteLine($"disabled - {disabled}"); // > disabled - Query: [] Count: 1 + Console.WriteLine($"disabled - {disabled}"); // > disabled - Query: [] Count: 1 } [ComponentKey("my-component")] @@ -72,7 +72,7 @@ public static void AddComponents() var entity = store.CreateEntity(); // add components - entity.AddComponent(new EntityName("Hello World!"));// EntityName is a build-in component + entity.AddComponent(new EntityName("Hello World!"));// EntityName is build-in entity.AddComponent(new MyComponent { value = 42 }); Console.WriteLine($"entity: {entity}"); // > entity: id: 1 "Hello World!" [EntityName, Position] @@ -87,13 +87,13 @@ public static void AddComponents() /// /// is used to reduce code coupling. -/// It enables access to a unique entity without the need to pass the entity by external code. +/// It enables access to a unique entity without the need to pass the entity by external code. /// [Test] public static void GetUniqueEntity() { var store = new EntityStore(); - store.CreateEntity(new UniqueEntity("Player")); // UniqueEntity is a build-in component + store.CreateEntity(new UniqueEntity("Player")); // UniqueEntity is build-in var player = store.GetUniqueEntity("Player"); Console.WriteLine($"entity: {player}"); // > entity: id: 1 [UniqueEntity] @@ -112,11 +112,11 @@ public static void AddTags() // add tags entity.AddTag(); entity.AddTag(); - Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [#MyTag1, #MyTag2] + Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [#MyTag1, #MyTag2] // get tag var tag1 = entity.Tags.Has(); - Console.WriteLine($"tag1: {tag1}"); // > tag1: True + Console.WriteLine($"tag1: {tag1}"); // > tag1: True } @@ -135,8 +135,8 @@ public static void EntityQueries() }); // --- query components with tags - var queryNamesWithTags = store.Query().AllTags(Tags.Get()); - queryNamesWithTags.ForEachEntity((ref EntityName name, Entity entity) => { + var namesWithTags = store.Query().AllTags(Tags.Get()); + namesWithTags.ForEachEntity((ref EntityName name, Entity entity) => { // ... 1 match }); } @@ -171,7 +171,7 @@ public static void AddChildEntities() root.AddChild(child1); root.AddChild(child2); - Console.WriteLine($"child entities: {root.ChildEntities}"); // > child entities: Count: 2 + Console.WriteLine($"entities: {root.ChildEntities}"); // > entities: Count: 2 } [Test] diff --git a/src/Tests/ECS/Examples/Optimization.cs b/src/Tests/ECS/Examples/Optimization.cs index 99673a04..e7a1e9c5 100644 --- a/src/Tests/ECS/Examples/Optimization.cs +++ b/src/Tests/ECS/Examples/Optimization.cs @@ -72,12 +72,12 @@ public static void QueryVectorization() foreach (var (component, entities) in query.Chunks) { // increment all MyComponent.value's. add = <1, 1, 1, 1, 1, 1, 1, 1> - var add = Vector256.Create(1); // create int[8] vector - all values = 1 - var values = component.AsSpan256(); // values.Length - multiple of 8 - var step = component.StepSpan256; // step = 8 + var add = Vector256.Create(1); // create int[8] vector - all values = 1 + var values = component.AsSpan256(); // values.Length - multiple of 8 + var step = component.StepSpan256; // step = 8 for (int n = 0; n < values.Length; n += step) { var slice = values.Slice(n, step); - var result = Vector256.Create(slice) + add; // execute 8 add instructions in one CPU cycle + var result = Vector256.Create(slice) + add; // 8 add ops in one CPU cycle result.CopyTo(slice); } } @@ -114,7 +114,7 @@ public static void CreateEntityBatch() .Add(new EntityName("test")) .Add(new Position(1,1,1)) .CreateEntity(); - Console.WriteLine($"entity: {entity}"); // > entity: id: 1 "test" [EntityName, Position] + Console.WriteLine($"{entity}"); // > id: 1 "test" [EntityName, Position] // Create a batch - can be cached if needed. var batch = new CreateEntityBatch(store).AddTag(); @@ -122,7 +122,7 @@ public static void CreateEntityBatch() batch.CreateEntity(); } var taggedEntities = store.Query().AllTags(Tags.Get()); - Console.WriteLine(taggedEntities); // > Query: [#MyTag1] Count: 10 + Console.WriteLine(taggedEntities); // > Query: [#MyTag1] Count: 10 } [Test] @@ -136,7 +136,7 @@ public static void EntityBatch() .AddTag() .Apply(); - Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [Position, #MyTag1] + Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [Position, #MyTag1] } [Test] @@ -151,7 +151,7 @@ public static void BulkBatch() store.Entities.ApplyBatch(batch); var query = store.Query().AllTags(Tags.Get()); - Console.WriteLine(query); // > Query: [Position, #MyTag1] Count: 1000 + Console.WriteLine(query); // > Query: [Position, #MyTag1] Count: 1000 // Same as: store.Entities.ApplyBatch(batch) above foreach (var entity in store.Entities) { @@ -174,14 +174,14 @@ public static void EntityList() var list = new EntityList(store); // Add root and all its children to the list list.AddTree(root); - Console.WriteLine($"list - {list}"); // > list - Count: 31 + Console.WriteLine($"list - {list}"); // > list - Count: 31 var batch = new EntityBatch(); batch.Add(new Position()); list.ApplyBatch(batch); var query = store.Query(); - Console.WriteLine(query); // > Query: [Position] Count: 31 + Console.WriteLine(query); // > Query: [Position] Count: 31 } [Test] @@ -202,9 +202,9 @@ public static void CommandBuffer() cb.Playback(); var entity3 = store.GetEntityById(newEntity); - Console.WriteLine(entity1); // > id: 1 "changed entity" [EntityName, #MyTag1] - Console.WriteLine(entity2); // > id: 2 (detached) - Console.WriteLine(entity3); // > id: 3 "new entity" [EntityName] + Console.WriteLine(entity1); // > id: 1 "changed entity" [EntityName, #MyTag1] + Console.WriteLine(entity2); // > id: 2 (detached) + Console.WriteLine(entity3); // > id: 3 "new entity" [EntityName] } }