Skip to content

Commit d72aa89

Browse files
[Entities] More improvements to ComponentVersionTracker;
1 parent ffcf588 commit d72aa89

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

Engine/Staple.Core/Entities/ComponentVersionTracker.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23

34
namespace Staple;
45

56
public class ComponentVersionTracker<T> where T: IComponent, IComponentVersion
67
{
78
private ulong[] versions = new ulong[1024];
89

10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
911
public bool ShouldUpdateComponent(Entity entity, in T component)
1012
{
1113
if (component == null)
1214
{
1315
throw new ArgumentNullException(nameof(component), "Component can't be null");
1416
}
1517

16-
if(versions.Length < entity.Identifier.ID)
18+
var index = entity.Identifier.ID - 1;
19+
var componentVersion = component.Version;
20+
21+
if (index >= versions.Length)
1722
{
18-
Array.Resize(ref versions, versions.Length * 2);
19-
}
23+
var newSize = versions.Length * 2;
2024

21-
var componentVersion = component.Version;
22-
var index = entity.Identifier.ID - 1;
25+
while(newSize < entity.Identifier.ID)
26+
{
27+
newSize *= 2;
28+
}
29+
30+
Array.Resize(ref versions, newSize);
31+
}
2332

2433
var version = versions[index];
2534

Engine/Staple.Core/Entities/Transform.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,8 @@ public class Transform : IComponent, IComponentVersion
2020
/// </summary>
2121
public ulong Version
2222
{
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2324
get => version;
24-
25-
private set
26-
{
27-
var previous = version;
28-
29-
version = value;
30-
31-
if(previous != version)
32-
{
33-
for(var i = 0; i < Children.Length; i++)
34-
{
35-
Children[i].Version++;
36-
}
37-
}
38-
}
3925
}
4026

4127
/// <summary>
@@ -126,7 +112,7 @@ public Vector3 Position
126112

127113
if(target != position)
128114
{
129-
Version++;
115+
BumpVersion();
130116
}
131117

132118
position = target;
@@ -144,7 +130,7 @@ public Vector3 LocalPosition
144130
{
145131
if(value != position)
146132
{
147-
Version++;
133+
BumpVersion();
148134
}
149135

150136
position = value;
@@ -171,7 +157,7 @@ public Vector3 Scale
171157

172158
if (target != scale)
173159
{
174-
Version++;
160+
BumpVersion();
175161
}
176162

177163
scale = target;
@@ -189,7 +175,7 @@ public Vector3 LocalScale
189175
{
190176
if (value != scale)
191177
{
192-
Version++;
178+
BumpVersion();
193179
}
194180

195181
scale = value;
@@ -216,7 +202,7 @@ public Quaternion Rotation
216202

217203
if (target != rotation)
218204
{
219-
Version++;
205+
BumpVersion();
220206
}
221207

222208
rotation = target;
@@ -234,7 +220,7 @@ public Quaternion LocalRotation
234220
{
235221
if (value != rotation)
236222
{
237-
Version++;
223+
BumpVersion();
238224
}
239225

240226
rotation = value;
@@ -353,7 +339,7 @@ public void SetParent(Transform parent)
353339

354340
parent?.AttachChild(this);
355341

356-
Version++;
342+
BumpVersion();
357343

358344
Scene.RequestWorldUpdate();
359345
}
@@ -465,6 +451,20 @@ private bool MoveChild(Transform child, int index)
465451
return true;
466452
}
467453

454+
/// <summary>
455+
/// Increases the transform's version
456+
/// </summary>
457+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
458+
private void BumpVersion()
459+
{
460+
version++;
461+
462+
for (var i = 0; i < Children.Length; i++)
463+
{
464+
Children[i].BumpVersion();
465+
}
466+
}
467+
468468
/// <summary>
469469
/// Rotates this transform
470470
/// </summary>

0 commit comments

Comments
 (0)