-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObjectManager.cs
147 lines (121 loc) · 4.56 KB
/
GameObjectManager.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
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
#region
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#endregion
namespace CludoEngine {
public class GameObjectManager : IUpdateable {
public delegate void OnGameObjectAdded(object sender, OnGameObjectAddedEventArgs args);
public delegate void OnGameObjectRemoved(object sender, OnGameObjectRemovedEventArgs args);
private Scene _scene;
public Dictionary<int, GameObject> Objects;
public GameObjectManager(Scene scene) {
Objects = new Dictionary<int, GameObject>();
_scene = scene;
}
public void Update(Microsoft.Xna.Framework.GameTime gt) {
// Iterate through each component and Update it.
for (var i = 0; i < Objects.Count; i++) {
if (Objects.Count >= i) {
Objects.ElementAt(i).Value.Update(gt);
}
}
}
public event OnGameObjectAdded OnGameObjectAddedEvent;
public event OnGameObjectRemoved OnGameObjectRemovedEvent;
public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch sb) {
// Iterate through each component and Draw it.
for (var i = 0; i < Objects.Count; i++) {
if (Objects.Count >= i) {
Objects.ElementAt(i).Value.Draw(sb);
}
}
}
#region Getting and adding objects;
public void AddGameObject(string name, GameObject gameObject) {
gameObject.Name = name;
Objects.Add(gameObject.Id,gameObject);
if (OnGameObjectAddedEvent != null) {
OnGameObjectAddedEvent(this, new OnGameObjectAddedEventArgs(gameObject));
}
// old system.
//gameObject.Id = Objects.Count;
//var name2 = "";
//if (Objects.ContainsKey(name)) {
// name2 = name + gameObject.Id;
// Objects.Add(name2, gameObject);
// if (OnGameObjectAddedEvent != null) {
// OnGameObjectAddedEvent(this, new OnGameObjectAddedEventArgs(gameObject));
// }
//} else {
// name2 = name;
// Objects.Add(name2, gameObject);
// if (OnGameObjectAddedEvent != null) {
// OnGameObjectAddedEvent(this, new OnGameObjectAddedEventArgs(gameObject));
// }
//}
//gameObject.Name = name2;
}
public IEnumerable<GameObject> ContainsTag(string tag) {
return HasTag(tag);
}
public IEnumerable<GameObject> HasTag(string tag) {
return
from entry in Objects
where entry.Value.Tags.Contains(tag)
select entry.Value;
}
public IEnumerable<GameObject> NameEndsWith(string i) {
return
from entry in Objects
where ((GameObject)entry.Value).Name.EndsWith(i)
select entry.Value;
}
public IEnumerable<GameObject> NameStartsWith(string i) {
return
from entry in Objects
where ((GameObject)entry.Value).Name.StartsWith(i)
select entry.Value;
}
public bool Exists(string name) {
return GetGameObject(name) != null;
}
public GameObject GetGameObject(string name) {
foreach (GameObject i in Objects.Values) {
if (i.Name == name) {
return i;
}
}
return null;
}
public void RemoveContainsTag(string tag) {
RemoveHasTag(tag);
}
public void RemoveHasTag(string tag) {
var i = HasTag(tag);
foreach (var go in i) {
RemoveObject(go.Id);
}
}
public void RemoveNameEndsWith(string i) {
var co = NameEndsWith(i);
foreach (var l in co) {
RemoveObject(l.Id);
}
}
public void RemoveNameStartsWith(string i) {
var co = NameStartsWith(i);
foreach (var l in co) {
RemoveObject(l.Id);
}
}
public void RemoveObject(int i) {
if (OnGameObjectRemovedEvent != null) {
OnGameObjectRemovedEvent(this, new OnGameObjectRemovedEventArgs(Objects[i]));
}
_scene.World.RemoveBody(Objects[i].Body);
Objects.Remove(i);
}
#endregion Getting and adding objects;
}
}