-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneTree.cs
More file actions
161 lines (139 loc) · 3.82 KB
/
SceneTree.cs
File metadata and controls
161 lines (139 loc) · 3.82 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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks.Dataflow;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace StarPong.Framework
{
/// <summary>
/// The scene tree manages the game object hierarchy, frees the objects
/// when necessary and facilitates the main game loop.
/// It can switch between scenes by switching roots. Children that are then
/// no longer referred anywhere will be freed automatically by GC, but they
/// can still exist out of the tree if other objects hold references to them.
/// </summary>
public class SceneTree
{
public static SceneTree Instance;
List<GameObject> freeQueue = new();
public GameObject Root { get; private set; }
Dictionary<string, List<GameObject>> groups = new();
public SceneTree()
{
Instance = this;
}
#region Game Objects
public void UpdateTransform()
{
if (Root != null)
{
Root.UpdateTransformHierarchy();
}
}
public void Update(float delta)
{
if (Root != null)
{
Root.UpdateHierarchy(delta);
}
foreach (GameObject obj in freeQueue.ToList())
{
obj.Parent?.RemoveChild(obj);
}
freeQueue = new();
}
public void QueueDrawObjects()
{
if (Root != null)
{
Root.QueueDrawHierarchy();
}
}
public void QueueFree(GameObject obj)
{
freeQueue.Add(obj);
}
public void SetRoot(GameObject obj)
{
if (Root != null)
{
Root.ExitTreeHierarchy();
}
Root = obj;
Root.EnterTreeHierarchy(this);
}
#endregion
#region Groups
public void AddObjectToGroup(GameObject obj, string group)
{
if (!groups.ContainsKey(group))
{
groups[group] = new();
}
groups[group].Add(obj);
}
public void RemoveObjectFromGroup(GameObject obj, string group)
{
if (groups.ContainsKey(group))
{
groups[group].Remove(obj);
}
}
public static List<GameObject> GetObjectsInGroup(string group)
{
if (Instance.groups.ContainsKey(group))
{
return Instance.groups[group];
}
return new();
}
#endregion
#region Fun Visualisation
const int treeX = 10;
const int treeY = 10;
const int treeSpacingDepth = 40;
const int treeSpacingYIndex = 20;
static Color treePanelColor = Color.Black with { A = 220 };
static Color treeColor = Color.White;
/// <summary>
/// Draws a literal tree to indicate the parent/child relationships of the active objects.
/// For funsies and for debugging purposes.
/// </summary>
/// <param name="batch"></param>
///
public void DebugDrawTree(SpriteBatch batch)
{
if (Root != null)
{
int yindex = DrawTreeRecur(Root, batch, 0, 0, false);
Rectangle rect = new Rectangle(treeX, treeY, 500 + 10, (yindex+1) * treeSpacingYIndex + 10);
Primitives2D.FillRectangle(batch, rect, treePanelColor);
Primitives2D.DrawRectangle(batch, rect, treeColor);
DrawTreeRecur(Root, batch, 0, 0, true);
}
}
int DrawTreeRecur(GameObject obj, SpriteBatch batch, int yindex, int depth, bool draw=true)
{
Vector2 position = new Vector2(treeX+10, treeY+5) + new Vector2(depth * treeSpacingDepth, yindex * treeSpacingYIndex);
if (draw) batch.DrawString(Engine.DebugFont, obj.GetType().Name, position, treeColor);
int baseY = yindex;
float y1 = 1;
float y2 = 1.5f;
foreach (GameObject child in obj.Children)
{
Vector2 vpos1 = position + new Vector2(5, y1 * treeSpacingYIndex);
Vector2 vpos2 = position + new Vector2(5, y2 * treeSpacingYIndex);
Primitives2D.DrawLine(batch, vpos1, vpos2, treeColor);
Vector2 hpos = vpos2 + new Vector2(treeSpacingDepth-10, 0);
Primitives2D.DrawLine(batch, vpos2, hpos, treeColor);
yindex = DrawTreeRecur(child, batch, yindex + 1, depth + 1);
y1 = y2;
y2 = (yindex - baseY) + 1.5f;
}
return yindex;
}
#endregion
}
}