-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEntity.cs
More file actions
182 lines (159 loc) · 4.39 KB
/
Entity.cs
File metadata and controls
182 lines (159 loc) · 4.39 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using SharpEngine.Core.Utils;
namespace SharpEngine.Core.Entity;
/// <summary>
/// Class which represents Entity
/// </summary>
[UsedImplicitly]
public class Entity
{
/// <summary>
/// How Entity must be updated when paused
/// </summary>
[UsedImplicitly]
public PauseState PauseState { get; set; } = PauseState.Normal;
/// <summary>
/// Tag of Entity
/// </summary>
[UsedImplicitly]
public string Tag { get; set; } = "";
/// <summary>
/// Name of Entity
/// </summary>
[UsedImplicitly]
public string Name { get; set; } = "";
/// <summary>
/// Scene of Entity
/// </summary>
public Scene? Scene { get; set; }
/// <summary>
/// Get All Components of Entity
/// </summary>
[UsedImplicitly]
public List<Component.Component> Components { get; } = [];
/// <summary>
/// Get All Children of Entity
/// </summary>
[UsedImplicitly]
public List<Entity> Children { get; } = [];
/// <summary>
/// Parent of Entity
/// </summary>
[UsedImplicitly]
public Entity? Parent { get; set; }
/// <summary>
/// Get All Components of one Type
/// </summary>
/// <typeparam name="T">Type of Component</typeparam>
/// <returns>Components of type T</returns>
public List<T> GetComponentsAs<T>()
where T : Component.Component => Components.OfType<T>().ToList();
/// <summary>
/// Get Component of one Type
/// </summary>
/// <typeparam name="T">Type of Component</typeparam>
/// <returns>Component of type T</returns>
public T? GetComponentAs<T>()
where T : Component.Component => Components.OfType<T>().FirstOrDefault();
/// <summary>
/// Get Scene as T
/// </summary>
/// <typeparam name="T">Scene Type</typeparam>
/// <returns>Scene cast as T</returns>
[UsedImplicitly]
public T? GetSceneAs<T>()
where T : Scene => (T?)Scene;
/// <summary>
/// Add Child Entity
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
[UsedImplicitly]
public T AddChild<T>(T entity)
where T : Entity
{
Children.Add(entity);
entity.Parent = this;
entity.Scene = Scene;
return entity;
}
/// <summary>
/// Add a Component and return it
/// </summary>
/// <param name="component">Component which be added</param>
/// <typeparam name="T">Type of Component</typeparam>
/// <returns>Component</returns>
[UsedImplicitly]
public T AddComponent<T>(T component)
where T : Component.Component
{
Components.Add(component);
component.Entity = this;
return component;
}
/// <summary>
/// Remove Child
/// </summary>
/// <param name="entity">Child</param>
[UsedImplicitly]
public void RemoveChild(Entity entity)
{
entity.Parent = null;
entity.Scene = null;
Children.Remove(entity);
}
/// <summary>
/// Remove Component
/// </summary>
/// <param name="component">Component will be removed</param>
[UsedImplicitly]
public void RemoveComponent(Component.Component component)
{
component.Entity = null;
Components.Remove(component);
}
/// <summary>
/// Load Entity
/// </summary>
public virtual void Load()
{
foreach (var component in Components)
component.Load();
foreach (var child in Children)
child.Load();
}
/// <summary>
/// Unload Entity
/// </summary>
public virtual void Unload()
{
foreach (var component in Components)
component.Unload();
foreach (var child in Children)
child.Unload();
}
/// <summary>
/// Update Entity
/// </summary>
/// <param name="delta">Time since last frame</param>
public virtual void Update(float delta)
{
foreach (var component in Components)
component.Update(delta);
foreach (var child in Children)
child.Update(delta);
}
/// <summary>
/// Draw Entity
/// </summary>
public virtual void Draw()
{
foreach (var component in Components)
component.Draw();
foreach (var child in Children)
child.Draw();
}
}