-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entity.cs
113 lines (101 loc) · 3.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
// Copyright (C) 2017 Robert A. Wallis, All Rights Reserved.
using System;
using System.Collections;
using System.Collections.Generic;
namespace ECSLight
{
/// <summary>
/// An IEntity represents a single object that contains components.
/// It's really just an interface to the ComponentManager for a single entity.
/// Entities are not meant to be subclassed, please use
/// Components to specify fields (data),
/// and Systems to specify functions (behavior).
/// </summary>
public class Entity : IEntity
{
private readonly IEntityManager _entityManager;
private readonly IComponentManager _componentManager;
private readonly string _name;
/// <summary>
/// Please use Context.CreateEntity, don't subclass this.
/// Please use Components for data, and Systems for behavior.
/// Also to avoid GC, entities are pooled by Context.
/// </summary>
public Entity(IEntityManager entityManager, IComponentManager componentManager, string name = "")
{
_entityManager = entityManager;
_componentManager = componentManager;
_name = name;
}
/// <summary>
/// End the lifecycle of this entity.
/// </summary>
public void Release()
{
_entityManager.ReleaseEntity(this);
}
/// <summary>
/// Add a component to this entity.
/// Updates context's entity sets.
/// </summary>
public void Add<TComponent>(TComponent component) where TComponent : class
{
_componentManager.AddComponent(this, component);
}
/// <summary>
/// Check if this entity has a component type.
/// </summary>
public bool Contains<TComponent>()
{
return _componentManager.ContainsComponent<TComponent>(this);
}
/// <summary>
/// Check if this entity has a component type.
/// </summary>
public bool Contains(Type type)
{
return _componentManager.ContainsComponent(this, type);
}
/// <summary>
/// Get the component attached to this entity.
/// </summary>
/// <returns>null if the component is not attached</returns>
public TComponent Get<TComponent>() where TComponent : class
{
return _componentManager.ComponentFrom<TComponent>(this);
}
/// <summary>
/// Remove the component from this entity.
/// Updates the context's entity sets.
/// </summary>
/// <typeparam name="TComponent"></typeparam>
public void Remove<TComponent>() where TComponent : class
{
_componentManager.RemoveComponent<TComponent>(this);
}
/// <summary>
/// Enumerate through this entity's components.
/// </summary>
public IEnumerator<object> GetEnumerator()
{
return _componentManager.GetEnumerator(this);
}
public override string ToString()
{
// Unity 3.5 target doesnt support IEnumerable to string[], for string.Join() below
var componentStrings = new List<string>();
foreach (var component in this) {
componentStrings.Add(component.ToString());
}
var components = string.Join(", ", componentStrings.ToArray());
return string.Format("{0}:[{1}]", _name, components);
}
/// <summary>
/// Enumerate through this entity's components.
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}