Skip to content

Commit

Permalink
reduced line length for wiki examples
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jul 25, 2024
1 parent 24a1173 commit 14783de
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
40 changes: 20 additions & 20 deletions src/Tests/ECS/Examples/Component_Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttackComponent>(); // { 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<AttackComponent>(); // { }
entity3.GetIncomingLinks<AttackComponent>(); // { 2 }

// deleting a linked entity (3) removes all link components referencing it
entity3.DeleteEntity(); // 1 2
entity3.DeleteEntity(); // 1 2
entity2.HasComponent <AttackComponent>(); // false
}
#endregion
Expand Down Expand Up @@ -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 <AttackRelation>(); // { 1 }
// get all incoming links. O(1) //
entity1.GetIncomingLinks<AttackRelation>(); // { 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 <AttackRelation>(); // { 1, 3 }
entity3.GetIncomingLinks<AttackRelation>(); // { 2 }

// deleting a linked entity (1) removes all link relations referencing it
entity1.DeleteEntity(); // 2 → 3
entity1.DeleteEntity(); // 2 → 3
entity2.GetRelations <AttackRelation>(); // { 3 }

// deleting entity (2) is reflected by incoming links query
entity2.DeleteEntity(); // 3
entity2.DeleteEntity(); // 3
entity3.GetIncomingLinks<AttackRelation>(); // { }
}
#endregion
Expand Down Expand Up @@ -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 <InventoryItem>(); // { Coin, Axe }
entity.GetRelations <InventoryItem>(); // { Coin, Axe }

// Get a specific relation from an entity. O(1)
entity.GetRelation <InventoryItem,ItemType>(ItemType.Coin); // { type = Coin, count = 42 }
entity.GetRelation <InventoryItem,ItemType>(ItemType.Coin); // {type=Coin, count=42}

// Remove a specific relation from an entity
entity.RemoveRelation<InventoryItem,ItemType>(ItemType.Axe);
entity.GetRelations <InventoryItem>(); // { Coin }
entity.GetRelations <InventoryItem>(); // { Coin }
}
#endregion

Expand All @@ -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,string>("Player-001"); // Count: 1
store.GetEntitiesWithComponentValue<Player,string>("Player-001"); // Count: 1

// return same result as lookup using a Query(). O(1)
store.Query().HasValue <Player,string>("Player-001"); // Count: 1
store.Query().HasValue <Player,string>("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,string>("Player-000", "Player-099"); // Count: 100
store.Query().ValueInRange<Player,string>("Player-000", "Player-099"); // Count: 100

// get all unique Player.name's. O(1)
store.GetAllIndexedComponentValues<Player,string>(); // Count: 1000
store.GetAllIndexedComponentValues<Player,string>(); // Count: 1000
}

#endregion
Expand Down
26 changes: 13 additions & 13 deletions src/Tests/ECS/Examples/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}

Expand All @@ -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]
Expand All @@ -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")]
Expand All @@ -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]

Expand All @@ -87,13 +87,13 @@ public static void AddComponents()

/// <summary>
/// <see cref="EntityStoreBase.GetUniqueEntity"/> 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.
/// </summary>
[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]
Expand All @@ -112,11 +112,11 @@ public static void AddTags()
// add tags
entity.AddTag<MyTag1>();
entity.AddTag<MyTag2>();
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<MyTag1>();
Console.WriteLine($"tag1: {tag1}"); // > tag1: True
Console.WriteLine($"tag1: {tag1}"); // > tag1: True
}


Expand All @@ -135,8 +135,8 @@ public static void EntityQueries()
});

// --- query components with tags
var queryNamesWithTags = store.Query<EntityName>().AllTags(Tags.Get<MyTag1, MyTag2>());
queryNamesWithTags.ForEachEntity((ref EntityName name, Entity entity) => {
var namesWithTags = store.Query<EntityName>().AllTags(Tags.Get<MyTag1, MyTag2>());
namesWithTags.ForEachEntity((ref EntityName name, Entity entity) => {
// ... 1 match
});
}
Expand Down Expand Up @@ -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]
Expand Down
26 changes: 13 additions & 13 deletions src/Tests/ECS/Examples/Optimization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(1); // create int[8] vector - all values = 1
var values = component.AsSpan256<int>(); // values.Length - multiple of 8
var step = component.StepSpan256; // step = 8
var add = Vector256.Create<int>(1); // create int[8] vector - all values = 1
var values = component.AsSpan256<int>(); // 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<int>(slice) + add; // execute 8 add instructions in one CPU cycle
var result = Vector256.Create<int>(slice) + add; // 8 add ops in one CPU cycle
result.CopyTo(slice);
}
}
Expand Down Expand Up @@ -114,15 +114,15 @@ 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<MyTag1>();
for (int n = 0; n < 10; n++) {
batch.CreateEntity();
}
var taggedEntities = store.Query().AllTags(Tags.Get<MyTag1>());
Console.WriteLine(taggedEntities); // > Query: [#MyTag1] Count: 10
Console.WriteLine(taggedEntities); // > Query: [#MyTag1] Count: 10
}

[Test]
Expand All @@ -136,7 +136,7 @@ public static void EntityBatch()
.AddTag<MyTag1>()
.Apply();

Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [Position, #MyTag1]
Console.WriteLine($"entity: {entity}"); // > entity: id: 1 [Position, #MyTag1]
}

[Test]
Expand All @@ -151,7 +151,7 @@ public static void BulkBatch()
store.Entities.ApplyBatch(batch);

var query = store.Query<Position>().AllTags(Tags.Get<MyTag1>());
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) {
Expand All @@ -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<Position>();
Console.WriteLine(query); // > Query: [Position] Count: 31
Console.WriteLine(query); // > Query: [Position] Count: 31
}

[Test]
Expand All @@ -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]
}

}
Expand Down

0 comments on commit 14783de

Please sign in to comment.