-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityIndexer.cs
More file actions
93 lines (79 loc) · 2.28 KB
/
EntityIndexer.cs
File metadata and controls
93 lines (79 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using Unity.Collections;
using Unity.Mathematics;
namespace Renderloom
{
internal class EntityIndexer
{
private NativeList<int2> _entities = new NativeList<int2>(64, Allocator.Persistent);
int _nextFreeEntityIndex = -1;
public bool IsValid(in int2 entity)
{
if (_entities.Length <= entity.x)
return false;
return _entities[entity.x].y == entity.y;
}
public int2 CreateEntity(int arrayIndex)
{
// Reuse
if (_nextFreeEntityIndex >= 0)
{
int entityIndex = _nextFreeEntityIndex;
_nextFreeEntityIndex = _entities[entityIndex].x;
int newVersion = _entities[entityIndex].x + 1;
_entities[entityIndex] = new int2()
{
x = arrayIndex,
y = newVersion
};
return new int2()
{
x = entityIndex,
y = newVersion
};
}
// Create new one
{
int entityIndex = _entities.Length;
int version = 1;
_entities.Add(new int2()
{
x = arrayIndex,
y = version,
});
return new int2()
{
x = entityIndex,
y = version,
};
}
}
public void DestroyEntity(int2 entity)
{
int freeIndex = entity.x;
var item = _entities[entity.x];
item.x = _nextFreeEntityIndex;
_nextFreeEntityIndex = entity.x;
_entities[entity.x] = item;
}
public int2 GetItem(int2 entity)
{
return _entities[entity.x];
}
public void UpdateIndex(int2 entity, int newArrayIndex)
{
var item = _entities[entity.x];
item.x = newArrayIndex;
_entities[entity.x] = item;
}
public void Clear()
{
_entities.Clear();
_nextFreeEntityIndex = -1;
}
public void Dispose()
{
_entities.Dispose();
}
}
}