-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEntity.cs
168 lines (131 loc) · 5.09 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// <copyright file="Entity.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>
namespace FinalEngine.ECS;
using System;
using System.Collections.Generic;
using System.Dynamic;
public class Entity : DynamicObject, IReadOnlyEntity
{
private readonly Dictionary<Type, IEntityComponent> typeToComponentMap;
public Entity(Guid? uniqueID = null)
{
this.UniqueIdentifier = uniqueID ?? Guid.NewGuid();
this.typeToComponentMap = [];
}
public ICollection<IEntityComponent> Components
{
get { return this.typeToComponentMap.Values; }
}
public Guid UniqueIdentifier { get; }
internal EventHandler<EventArgs>? OnComponentsChanged { get; set; }
public void AddComponent(IEntityComponent component)
{
ArgumentNullException.ThrowIfNull(component, nameof(component));
var type = component.GetType();
if (this.ContainsComponent(type))
{
throw new ArgumentException($"The specified {nameof(component)} parameters type has already been added to this entity", nameof(component));
}
this.typeToComponentMap.Add(type, component);
this.OnComponentsChanged?.Invoke(this, EventArgs.Empty);
}
public void AddComponent<TComponent>()
where TComponent : IEntityComponent, new()
{
this.AddComponent(Activator.CreateInstance<TComponent>());
}
public bool ContainsComponent(IEntityComponent component)
{
ArgumentNullException.ThrowIfNull(component, nameof(component));
foreach (var other in this.typeToComponentMap.Values)
{
if (other == component)
{
return true;
}
}
return false;
}
public bool ContainsComponent(Type type)
{
ArgumentNullException.ThrowIfNull(type, nameof(type));
return !typeof(IEntityComponent).IsAssignableFrom(type)
? throw new ArgumentException($"The specified {nameof(type)} parameter does not implement {nameof(IEntityComponent)}.", nameof(type))
: this.typeToComponentMap.ContainsKey(type);
}
public bool ContainsComponent<TComponent>()
where TComponent : IEntityComponent
{
return this.ContainsComponent(typeof(TComponent));
}
public IEntityComponent GetComponent(Type type)
{
ArgumentNullException.ThrowIfNull(type, nameof(type));
if (!typeof(IEntityComponent).IsAssignableFrom(type))
{
throw new ArgumentException($"The specified {nameof(type)} parameter does not implement {nameof(IEntityComponent)}.", nameof(type));
}
return !this.ContainsComponent(type)
? throw new ArgumentException($"The specified {nameof(type)} parameter is not a component type that has been added to this entity.", nameof(type))
: this.typeToComponentMap[type];
}
public TComponent GetComponent<TComponent>()
where TComponent : IEntityComponent
{
return (TComponent)this.GetComponent(typeof(TComponent));
}
public void RemoveComponent(IEntityComponent component)
{
ArgumentNullException.ThrowIfNull(component, nameof(component));
if (!this.ContainsComponent(component))
{
throw new ArgumentException($"The specified {nameof(component)} parameter has not been added to this entity.", nameof(component));
}
this.RemoveComponent(component.GetType());
}
public void RemoveComponent(Type type)
{
ArgumentNullException.ThrowIfNull(type, nameof(type));
if (!typeof(IEntityComponent).IsAssignableFrom(type))
{
throw new ArgumentException($"The specified {nameof(type)} parameter does not implement {nameof(IEntityComponent)}.", nameof(type));
}
if (!this.ContainsComponent(type))
{
throw new ArgumentException($"The specified {nameof(type)} parameter is not a component type that has been added to this entity.", nameof(type));
}
this.typeToComponentMap.Remove(type);
this.OnComponentsChanged?.Invoke(this, EventArgs.Empty);
}
public void RemoveComponent<TComponent>()
where TComponent : IEntityComponent
{
this.RemoveComponent(typeof(TComponent));
}
public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
ArgumentNullException.ThrowIfNull(binder, nameof(binder));
string? memberName = binder.Name;
foreach (var kvp in this.typeToComponentMap)
{
string? typeName = kvp.Key.Name;
if (typeName.EndsWith("Component", StringComparison.CurrentCulture))
{
if (typeName == memberName)
{
result = kvp.Value;
return true;
}
typeName = typeName.Remove(typeName.Length - "Component".Length);
}
if (typeName == memberName)
{
result = kvp.Value;
return true;
}
}
result = null;
return false;
}
}