-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cs
144 lines (120 loc) · 4.23 KB
/
Entity.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Runtime.CompilerServices;
namespace Gemserk.Leopotam.Ecs
{
public static class EntityWorldExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Entity GetEntity(this BaseSystem baseSystem, int entity)
{
return baseSystem.world.GetEntity(entity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(this Entity entity) where T : struct
{
entity.world.AddComponent<T>(entity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(this Entity entity, T t) where T : struct
{
entity.world.AddComponent(entity, t);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddOrSet<T>(this Entity entity, T t) where T : struct
{
entity.world.AddOrSetComponent(entity, t);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T Get<T>(this Entity entity) where T : struct
{
return ref entity.world.GetComponent<T>(entity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Has<T>(this Entity entity) where T : struct
{
return entity.world.HasComponent<T>(entity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Remove<T>(this Entity entity) where T : struct
{
entity.world.RemoveComponent<T>(entity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Exists(this Entity entity)
{
if (entity == Entity.NullEntity)
{
return false;
}
if (entity.world == null)
{
return false;
}
return entity.world.Exists(entity);
}
}
// This is a higher concept of Entity, not the ecs entity itself, it has useful methods and data to simplify
// accessing the ecs layer.
public struct Entity
{
public static readonly Entity NullEntity = new Entity(null, -1, -1);
public World world;
public int ecsEntity;
public short ecsGeneration;
public static Entity[] CreateArray(int count)
{
var entities = new Entity[count];
for (var i = 0; i < count; i++)
{
entities[i] = NullEntity;
}
return entities;
}
public static void CreateArray(Entity[] entityArray)
{
for (var i = 0; i < entityArray.Length; i++)
{
entityArray[i] = NullEntity;
}
}
public static Entity Create(World world, int entity, short generation)
{
return new Entity(world, entity, generation);
}
public Entity(World world, int ecsEntity, short ecsGeneration)
{
this.world = world;
this.ecsEntity = ecsEntity;
this.ecsGeneration = ecsGeneration;
}
public override int GetHashCode()
{
return HashCode.Combine(ecsEntity, ecsGeneration);
}
public static implicit operator int(Entity entity) => entity.ecsEntity;
public static bool operator ==(Entity reference, Entity e)
{
return reference.ecsEntity == e.ecsEntity && reference.ecsGeneration == e.ecsGeneration;
}
public static bool operator !=(Entity reference, Entity e)
{
return reference.ecsEntity != e.ecsEntity || reference.ecsGeneration != e.ecsGeneration;
}
public override bool Equals(object obj)
{
if (obj is int entity)
{
return this.ecsEntity == entity;
}
return base.Equals(obj);
}
public bool Equals(Entity other)
{
return ecsEntity == other.ecsEntity;
}
public override string ToString()
{
return $"[{ecsEntity},{ecsGeneration}]";
}
}
}