/
ObjectManager.cs
64 lines (60 loc) · 2.13 KB
/
ObjectManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PointFHelp;
namespace Hunt_the_Wumpus
{
static class ObjectManager
{
private const int LengthOfDictionary = 50;
public static Dictionary<string, GameObject> Objects = new Dictionary<string, GameObject>(LengthOfDictionary); //We start this at a higher than default value for optimization purposes
//This decreases the number of hash collisions.
static List<GameObject> ObjectsToAdd = new List<GameObject>(); //The objects to add at the beginning of the next frame
static List<GameObject> ObjectsToRemove = new List<GameObject>(); //The objects to remove at the beginning of the next frame
public static void Update()
{
foreach (GameObject obj in ObjectsToAdd) //Add all of the objects that need adding
{
if (!Objects.Keys.Contains(obj.Name))
Objects.Add(obj.Name, obj);
}
foreach (GameObject obj in ObjectsToRemove) //Remove all of the objects that need removing
Objects.Remove(obj.Name);
ObjectsToAdd.Clear(); //Clear the lists
ObjectsToRemove.Clear();
}
public static void Clear()
{
Objects.Clear();
ObjectsToAdd.Clear();
ObjectsToRemove.Clear();
}
public static void AddGameObject(GameObject obj) //Queues a GameObject for adding into the Objects dictionary
{
if (obj != null)
{
ObjectsToAdd.Add(obj);
}
}
public static void RemoveGameObject(GameObject obj) //Queues a GameObject for removal from the Objects dictionary
{
if (obj != null)
{
ObjectsToRemove.Add(obj);
}
}
public static GameObject GetObjectByName(string name) //Gets an object by name
{
try
{
return Objects[name];
}
catch
{
return null;
}
}
}
}