-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cs
More file actions
399 lines (360 loc) · 10.7 KB
/
GameObject.cs
File metadata and controls
399 lines (360 loc) · 10.7 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ImGuiNET;
using Microsoft.Xna.Framework.Input;
namespace RTS_Engine;
public class GameObject
{
public string Name = "NewObject";
public bool Active = true;
private bool _wasActive;
public bool UseCascadingActiveState = true;
public Transform Transform;
private List<Component> _components = new();
public List<GameObject> Children {get; private set;} = new();
public GameObject Parent {get; private set;}
public GameObject()
{
Transform = new Transform(this);
_wasActive = Active;
}
public void ToggleChildrenActive(bool state)
{
foreach (GameObject child in Children)
{
child.Active = state;
}
}
public void Update()
{
if (UseCascadingActiveState)
{
if (!Active && _wasActive)
{
SetChildrenInactive(this);
}
else if (Active && !_wasActive)
{
SetChildrenActive(this);
}
}
if (Active)
{
Transform.Update();
//Update all components
foreach (Component c in _components)
{
c.Update();
}
//Update all children
foreach (GameObject gameObject in Children)
{
gameObject.Update();
}
}
_wasActive = Active;
}
public T GetComponent<T>() where T : Component
{
return (T)_components.Find(x => x.GetType() == typeof(T));
}
public bool HasComponent<T>() where T : Component
{
return _components.Any(x => x.GetType() == typeof(T));
}
public void AddComponent<T>() where T : Component,new()
{
if(typeof(T) == typeof(Transform))return;
T component = new T();
component.ParentObject = this;
component.Initialize();
_components.Add(component);
}
public void RemoveFirstComponentOfType<T>() where T : Component
{
_components.Remove(_components.FirstOrDefault(x => x.GetType() == typeof(T)));
}
public Component GetComponent(Type type)
{
return _components.Find(x => x.GetType() == type);
}
public void AddComponent(Component component)
{
if(component.GetType() == typeof(Transform))return;
component.ParentObject = this;
_components.Add(component);
}
public bool RemoveComponent(Component component)
{
return _components.Remove(component);
}
public void AddChildObject(GameObject gameObject)
{
Children.Add(gameObject);
gameObject.Parent = this;
}
public void RemoveChildObject(GameObject gameObject)
{
Children.Remove(gameObject);
ClearObject(gameObject);
gameObject.Parent = null;
}
public static void ClearObject(GameObject gameObject)
{
gameObject.Parent?.Children.Remove(gameObject);
for (int i = gameObject.Children.Count - 1; i >= 0 ; i--)
{
ClearObject(gameObject.Children[i]);
}
while (gameObject._components.Count > 0)
{
gameObject._components[0].RemoveComponent();
}
gameObject.Transform = null;
}
private void SetChildrenInactive(GameObject parent)
{
foreach (var child in parent.Children)
{
child.Active = false;
SetChildrenInactive(child);
}
}
private void SetChildrenActive(GameObject parent)
{
foreach (var child in parent.Children)
{
child.Active = true;
SetChildrenActive(child);
}
}
public GameObject FindGameObjectByName(string name)
{
if (Name == name) return this;
foreach (var child in Children)
{
GameObject result = child.FindGameObjectByName(name);
if (result != null) return result;
}
return null;
}
public void ToggleGameObjectActiveState(string name)
{
GameObject gameObject = FindGameObjectByName(name);
if (gameObject != null)
{
gameObject.Active = !gameObject.Active;
}
}
public void ToggleParentActiveState()
{
Parent.Active = !Parent.Active;
if (Parent.Active)
{
SetChildrenActive(Parent);
}
else
{
SetChildrenInactive(Parent);
}
}
public string SaveSceneToXml()
{
StringBuilder builder = new StringBuilder();
builder.Append("<rootObject>");
builder.Append("<name>" + Name + "</name>");
builder.Append("<active>" + Active +"</active>");
builder.Append("<cascadingActive>" + UseCascadingActiveState +"</cascadingActive>");
builder.Append("<components>");
builder.Append(Transform.ComponentToXmlString());
foreach (Component c in _components)
{
builder.Append(c.ComponentToXmlString());
}
builder.Append("</components>");
builder.Append("<childObjects>");
foreach (GameObject gameObject in Children)
{
builder.Append(gameObject.ObjectToXmlString());
}
builder.Append("</childObjects>");
builder.Append("</rootObject>");
return builder.ToString();
}
private string ObjectToXmlString()
{
StringBuilder builder = new StringBuilder();
builder.Append("<object>");
builder.Append("<name>" + Name + "</name>");
builder.Append("<active>" + Active +"</active>");
builder.Append("<cascadingActive>" + UseCascadingActiveState +"</cascadingActive>");
builder.Append("<components>");
builder.Append(Transform.ComponentToXmlString());
foreach (Component c in _components)
{
builder.Append(c.ComponentToXmlString());
}
builder.Append("</components>");
builder.Append("<childObjects>");
foreach (GameObject gameObject in Children)
{
builder.Append(gameObject.ObjectToXmlString());
}
builder.Append("</childObjects>");
builder.Append("</object>");
return builder.ToString();
}
public void LoadPrefab(string name)
{
AddChildObject(FileManager.DeserializeScene(name));
}
public static GameObject GetPrefab(string name)
{
return FileManager.DeserializeScene(name);
}
#if DEBUG
private bool addingOpen = false;
private bool savingPrefab = false;
private bool loadPrefab = false;
private string prefabName;
public void DrawTree()
{
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags.OpenOnArrow;
if(Children.Count == 0)
{
flags |= ImGuiTreeNodeFlags.Leaf;
}
if(this == Globals.CurrentlySelectedObject)
{
flags |= ImGuiTreeNodeFlags.Selected;
}
string _name = "#";
if(this.Name != "")
{
_name = this.Name;
}
bool node_open = ImGui.TreeNodeEx(_name, flags);
if(ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen()) Globals.CurrentlySelectedObject = this;
if(node_open)
{
foreach(GameObject g in Children)
{
g.DrawTree();
}
ImGui.TreePop();
}
}
private void SaveAsPrefab(string name)
{
StringBuilder builder = new StringBuilder();
builder.Append(SaveSceneToXml());
XDocument prefab = XDocument.Parse(builder.ToString());
#if _WINDOWS
StreamWriter streamWriter = new StreamWriter(Globals.MainPath + "/Prefabs/" + name + ".xml");
#else
StreamWriter streamWriter = new StreamWriter(Globals.MainPath + "Prefabs/" + name + ".xml");
#endif
prefab.Save(streamWriter);
streamWriter.Close();
}
public void DrawInspector()
{
ImGui.Begin("Inspector");
ImGui.Checkbox("Active", ref Active);
ImGui.Checkbox("Use cascading active state", ref UseCascadingActiveState);
ImGui.InputText("Object name", ref Name, 20);
if(ImGui.Button("Delete GameObject"))
{
if(Parent != null)
{
Parent.RemoveChildObject(this);
if(Globals.CurrentlySelectedObject == this)
{
Globals.CurrentlySelectedObject = null;
}
}
}
if(ImGui.Button("Add child object"))
{
AddChildObject(new GameObject());
}
if(ImGui.Button("Save as prefab"))
{
prefabName = Name;
savingPrefab = true;
}
if(ImGui.Button("Add prefab as child"))
{
Globals.UpdatePrefabList();
loadPrefab = true;
}
ImGui.Text("");
Transform?.Inspect();
for(int i = _components.Count - 1;i >= 0;i--)
{
_components[i].Inspect();
}
ImGui.Separator();
if(ImGui.Button("Add component"))
{
addingOpen = true;
}
ImGui.End();
if (savingPrefab)
{
ImGui.Begin("Save prefab");
ImGui.InputText("Prefab name", ref prefabName, 20);
if (ImGui.Button("Save"))
{
SaveAsPrefab(prefabName);
savingPrefab = false;
}
if (ImGui.Button("Cancel"))
{
savingPrefab = false;
}
ImGui.End();
}
if (loadPrefab)
{
ImGui.Begin("Load and add prefab");
foreach(string path in Globals.AvailablePrefabs)
{
if (ImGui.Button(path))
{
LoadPrefab(path);
loadPrefab = false;
}
}
if (ImGui.Button("Cancel"))
{
loadPrefab = false;
}
ImGui.End();
}
if (addingOpen)
{
ImGui.Begin("Add component");
var method = typeof(GameObject).GetMethod("AddComponent",Type.EmptyTypes);
foreach(Type t in Globals.ComponentsTypes)
{
if (ImGui.Button(t.Name))
{
var kek = method.MakeGenericMethod(t);
kek.Invoke(this, null);
addingOpen = false;
}
}
if (ImGui.Button("Cancel"))
{
addingOpen = false;
}
ImGui.End();
}
}
#endif
}