Skip to content

Commit

Permalink
Relations - return relation from indexer as ref
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Feb 2, 2025
1 parent ce59360 commit fec110b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/ECS/Relations/Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE file in the project root for full license information.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -85,8 +86,19 @@ public string Debug()
/// Return the relation at the given <paramref name="index"/>.<br/>
/// Executes in O(1).
/// </summary>
public TRelation this[int index] => components[positions != null ? positions[index] : position];

public ref TRelation this[int index] {
get {
if (index >= 0 && index < Length) {
return ref components[positions != null ? positions[index] : position];
}
throw IndexOutOfRangeException(index);
}
}

private IndexOutOfRangeException IndexOutOfRangeException(int index) {
return new IndexOutOfRangeException($"index: {index} Length: {Length}");
}

// --- IEnumerable<>
IEnumerator<TRelation> IEnumerable<TRelation>.GetEnumerator() => new RelationsEnumerator<TRelation>(this);

Expand Down
37 changes: 36 additions & 1 deletion src/Tests/ECS/Relations/Test_Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,42 @@ public static void Test_Relations_add_remove_default()
IsFalse (entity.RemoveRelation<AttackRelation>(default));
relations = entity.GetRelations<AttackRelation>();
AreEqual("{ }", relations.Debug());
}
}

[Test]
public static void Test_Relations_modify_relations()
{
var store = new EntityStore();
var target1 = store.CreateEntity();
var target2 = store.CreateEntity();
var entity = store.CreateEntity();
var relations = entity.GetRelations<AttackRelation>();
Throws<IndexOutOfRangeException>(() => {
_ = relations[0];
});
// --- add first relation
IsTrue (entity.AddRelation(new AttackRelation { target = target1, speed = 1 }));
relations = entity.GetRelations<AttackRelation>();
AreEqual("{ 1 }", relations.Debug());
relations[0].speed = 11;
AreEqual(11, entity.GetRelation<AttackRelation, Entity>(target1).speed);
var e = Throws<IndexOutOfRangeException>(() => {
_ = relations[1];
});
AreEqual("index: 1 Length: 1", e!.Message);

// --- add second relation
IsTrue (entity.AddRelation(new AttackRelation { target = target2, speed = 2 }));
relations = entity.GetRelations<AttackRelation>();
AreEqual("{ 1, 2 }", relations.Debug());
relations[0].speed = 21;
relations[1].speed = 22;
AreEqual(21, entity.GetRelation<AttackRelation, Entity>(target1).speed);
AreEqual(22, entity.GetRelation<AttackRelation, Entity>(target2).speed);
Throws<IndexOutOfRangeException>(() => {
_ = relations[2];
});
}

#pragma warning disable CS0618 // Type or member is obsolete
[Test]
Expand Down

0 comments on commit fec110b

Please sign in to comment.