-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cs
95 lines (82 loc) · 2.58 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
using System.Collections.Generic;
using System;
using System.Linq;
namespace ECS
{
public class Entity
{
public string Id { get; set; }
public EntityManager ownerManager { get; set; }
public List<IComponent> Components { get; set; }
public Entity(string entityId, EntityManager entityManager)
{
this.Id = entityId;
this.ownerManager = entityManager;
this.Components = new List<IComponent>();
}
public void addComponent(IComponent Component)
{
if (HasComponent(Component.GetType()))
{
throw new Exception();
//component already exists
}
Components.Add(Component);
ownerManager.ComponentAdded(this);
}
public void removeComponent<TComponent>()
where TComponent : IComponent
{
if (!this.HasComponent<TComponent>())
{
throw new Exception();
//component doesn't exist
}
IComponent remove = GetComponent<TComponent>();
Components.Remove(remove);
ownerManager.ComponentRemoved(this);
}
public IComponent GetComponent<TComponent>()
where TComponent : IComponent
{
TComponent match = Components.OfType<TComponent>().FirstOrDefault();
if (match != null) return (TComponent) match;
throw new Exception();
//component does not exist
}
public bool HasComponent(Type type)
{
var match = Components.Any(c => c.GetType() == type);
if (match) return true;
else return false;
}
public bool hasComponents(IEnumerable<Type> types)
{
foreach (var t in types)
{
if (!HasComponent(t)) return false;
}
return true;
}
public bool HasComponent<TComponent>()
where TComponent : IComponent
{
var match = Components.Any(comp => comp.GetType() == typeof(TComponent));
if (match) return true;
else return false;
}
public static Entity operator +(Entity entity, IComponent component)
{
if (entity != null && component != null)
{
entity.addComponent(component);
return entity;
}
else
{
throw new Exception();
//null argument issue
}
}
}
}