-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectHandler.cs
More file actions
298 lines (256 loc) · 11.2 KB
/
ObjectHandler.cs
File metadata and controls
298 lines (256 loc) · 11.2 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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Threading;
using System.Linq;
using ProjectGreco.Levels;
using ProjectGreco.GameObjects;
using ProjectGreco.GameObjects.Buttons;
//------------------------------------------------------------------------------ +
//Author: Aidan |
//Purpose: The object handler holds, updates, and detects collisions between all|
//game objects. |
//------------------------------------------------------------------------------ +
namespace ProjectGreco
{
public class ObjectHandler
{
public Dictionary<String, GameObject> objectDictionary;
public ObjectHandler()
{
objectDictionary = new Dictionary<string, GameObject>();
}
/// <summary>
/// The list of strings used to manage all of the objects that need to collide.
/// </summary>
public List<string> collisionList = new List<string>();
/// <summary>
/// This list of objects that are on screen
/// </summary>
public List<string> onScreenList = new List<string>();
/// <summary>
/// The objects that willl actually be collision checked
/// </summary>
public List<string> collisionCheckList = new List<string>();
/// <summary>
/// The level state of the game. This holds the levels variables and so forth.
/// </summary>
public BaseState currentState;
/// <summary>
/// This bool tells the central update loop whether or not to stop looping.
/// </summary>
public bool escapeBool = false;
/// <summary>
/// Adds a game object to the object Dictionary. If the object already exists, the name will be appeneded with a number
/// </summary>
/// <param name="name">Name of the object</param>
/// <param name="objectToAdd">The Game Object To add</param>
public void AddObjectToHandler(string name, GameObject objectToAdd)
{
int nameIndex = 2;
while (true)
{
if(objectDictionary.ContainsKey(name) == false)
{
objectDictionary.Add(name, objectToAdd);
collisionList.Add(name);
objectToAdd.dictionaryName = name;
return;
}
else
{
string[] temp = name.Split('_');
name = String.Format(temp[0] + "_" + "{0}", nameIndex);
nameIndex++;
}
}
}
/// <summary>
/// Adds a game object to the object Dictionary. If the object already exists, the name will be appeneded with a number
/// This function with a start index will add the index to the object initially and move on from that point if it does not work
/// </summary>
/// <param name="name">Name of the object</param>
/// <param name="objectToAdd">The Game Object To add</param>
/// <param name="startIndex",>The index that you would like to start the adding process at.
public void AddObjectToHandler(string name, GameObject objectToAdd, int startIndex)
{
int nameIndex = startIndex;
string[] temp = name.Split('_');
name = String.Format(temp[0] + "_" + "{0}", nameIndex);
nameIndex++;
while (true)
{
if (objectDictionary.ContainsKey(name) == false)
{
objectDictionary.Add(name, objectToAdd);
collisionList.Add(name);
objectToAdd.dictionaryName = name;
return;
}
else
{
temp = name.Split('_');
name = String.Format(temp[0] + "_" + "{0}", nameIndex);
nameIndex++;
}
}
}
/// <summary>
/// Detects all of the collisions that are currently happening on the game screen
/// </summary>
/// <returns>An array that contains the name of the objects that are colliding</returns>
public string[,] DetectCollisions()
{
string[,] collidedObjects = new string[collisionList.Count, collisionList.Count];
for (int x = 0; x < collisionCheckList.Count; x++)
{
objectDictionary[collisionCheckList[x]].ObjectBelow = false;
for (int y = 0; y < onScreenList.Count; y++)
{
if (collisionCheckList[x] != onScreenList[y])
{
float distanceY = objectDictionary[collisionCheckList[x]].CollisionBox.Y - objectDictionary[onScreenList[y]].CollisionBox.Y;
Vector2 distanceBetweenObjects = new Vector2(objectDictionary[collisionCheckList[x]].CollisionBox.X - objectDictionary[onScreenList[y]].CollisionBox.X, Math.Abs(distanceY));
if ((distanceBetweenObjects.X >= 0 && distanceBetweenObjects.X < objectDictionary[onScreenList[y]].CollisionBox.Width) || (distanceBetweenObjects.X <= 0 && Math.Abs(distanceBetweenObjects.X) < objectDictionary[collisionCheckList[x]].CollisionBox.Width))
{
if (distanceBetweenObjects.Y < objectDictionary[collisionCheckList[x]].CollisionBox.Height)
{
collidedObjects[x, y] = onScreenList[y];
}
else if (distanceBetweenObjects.Y < objectDictionary[onScreenList[y]].CollisionBox.Height && distanceY > 0)
{
collidedObjects[x, y] = onScreenList[y];
}
if (distanceY > -objectDictionary[collisionCheckList[x]].CollisionBox.Height - 5 && distanceY < 0)
{
if (objectDictionary[onScreenList[y]].ObjectType == "EdgeTile" || objectDictionary[onScreenList[y]].ObjectType == "OneWayBlock")
objectDictionary[collisionCheckList[x]].ObjectBelow = true;
}
}
}
}
}
return collidedObjects;
}
/// <summary>
/// Takes the collisions that were detected and calls of the the onColision functions of existing game objects
/// </summary>
public void TriggerCollisionEvents()
{
string[,] collidedObjects = DetectCollisions();
for (int x = 0; x < collisionCheckList.Count; x++)
{
bool collideCall = false;
for (int y = 0; y < onScreenList.Count; y++)
{
if (collidedObjects[x, y] != null )
{
if (objectDictionary.ContainsKey(collidedObjects[x, y]))
{
objectDictionary[collisionCheckList[x]].C_OnCollision(objectDictionary[collidedObjects[x, y]]);
collideCall = true;
}
}
}
if (collideCall == false)
{
try
{
objectDictionary[collisionCheckList[x]].C_NoCollisions();
}
catch
{
Console.WriteLine(collisionCheckList[x]);
}
}
}
}
/// <summary>
/// Updates every game object in the object Dictionary. Also collide the OnScreen List and the collision list
/// </summary>
public void Update()
{
onScreenList.Clear();
collisionCheckList.Clear();
for (int x = 0; x < objectDictionary.Count; x++)
{
objectDictionary[collisionList[x]].Update();
if (escapeBool == true)
{
escapeBool = false;
return;
}
if (objectDictionary[collisionList[x]].OnScreen == true)
{
onScreenList.Add(collisionList[x]);
if (objectDictionary[collisionList[x]].CheckForCollisions == true)
collisionCheckList.Add(collisionList[x]);
}
}
TriggerCollisionEvents();
}
/// <summary>
/// Draws every object in the object Dictionary
/// </summary>
/// <param name="spriteBatch"></param>
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < onScreenList.Count; x++)
{
if (objectDictionary[onScreenList[x]].OnScreen)
{
objectDictionary[onScreenList[x]].Draw(spriteBatch);
}
}
}
/// <summary>
/// Adds an object to the object dictionary, also adds the object to the collision list.
/// </summary>
/// <param name="name">Name of the object</param>
/// <param name="objectToadd">The object to add</param>
public void AddToObjectDictionary(string name, GameObject objectToadd)
{
objectDictionary.Add(name, objectToadd);
collisionList.Add(name);
}
/// <summary>
/// This function will change the state of the game to the passed in state
/// </summary>
/// <param name="level"></param>
public void ChangeState(BaseState level)
{
currentState = level;
try
{
objectDictionary = currentState.LevelObjectDictionary;
}
catch
{
level = new Level(currentState.LevelType, (level as Level).MyPlayer, true);
currentState = level;
objectDictionary = currentState.LevelObjectDictionary;
}
onScreenList = new List<string>();
collisionList = currentState.collisionList;
Game1.CAMERA_DISPLACEMENT = new Vector2(0, 0);
escapeBool = true;
BaseState.currLevel = level.LevelType;
}
/// <summary>
/// This function will sort the object dictionary and collision dictionary by the zOrder so that they will be drawn correctly
/// </summary>
public void SortByZorder()
{
objectDictionary = objectDictionary.OrderBy(x => x.Value.ZOrder).ToDictionary(x => x.Key, x => x.Value);
collisionList = new List<string>();
foreach (var key in objectDictionary)
{
collisionList.Add(key.Key);
}
}
}
}