Maze /Classes/Object/ObjectContainer.cs

Language C# Lines 308
MD5 Hash 0cd6651faae9861d8f847aed719662bc Estimated Cost $4,724 (why?)
Repository git://github.com/Concliff/Maze.git View Raw File View Project SPDX
  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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maze.Classes
{
    /// <summary>
    /// Represents the storage of all existing Objects in the game.
    /// </summary>
    public sealed class ObjectContainer
    {
        #region Singleton Part

        private static ObjectContainer instance;

        /// <summary>
        /// Gets reference to the ObjectContainer instance.
        /// </summary>
        public static ObjectContainer Instance
        {
            get
            {
                if (instance == null)
                    instance = new ObjectContainer();

                return instance;
            }
            private set { ;}
        }

        #endregion

        public static uint GUIDCounter;

        /// <summary>
        /// Safety check for changing objects while they are under Update.
        /// </summary>
        private bool isUpdating;
        private List<Object> objects;
        private Stack<Object> objectsToRemove;
        private Stack<Object> objectsToAdd;
        private Stack<uint> releasedGUIDs;

        /// <summary>
        /// Contains Unit instances by their location.
        /// </summary>
        private Dictionary<GridLocation, List<Unit>> unitContainer;
        /// <summary>
        /// Contains GridObject instances by their location.
        /// </summary>
        private Dictionary<GridLocation, List<GridObject>> gridObjectContainer;

        private ObjectContainer()
        {
            this.objects = new List<Object>();
            this.objectsToRemove = new Stack<Object>();
            this.objectsToAdd = new Stack<Object>();
            this.releasedGUIDs = new Stack<uint>();
            GUIDCounter = 0;
            this.isUpdating = false;

            this.unitContainer = new Dictionary<GridLocation, List<Unit>>();
            this.gridObjectContainer = new Dictionary<GridLocation, List<GridObject>>();
        }

        /// <summary>
        /// Inserts new object into ObjectContainer and defines its GUID.
        /// </summary>
        /// <param name="newObject">Reference to the recentry created object.</param>
        /// <returns>New object GUID</returns>
        public uint CreateObject(Object newObject)
        {
            if (this.isUpdating)
                this.objectsToAdd.Push(newObject);
            else
            {
                AddNewObject(newObject);
            }

            // Define GUID of the new object
            if (this.releasedGUIDs.Count > 0)
                return this.releasedGUIDs.Pop();
            else
                return ++GUIDCounter;
        }

        /*
        // Temporary Disable this method
        public Object GetObjectByGUID(uint GUID)
        {
            for (int i = 0; i < this.objects.Count; ++i)
                if (this.objects[i].GetGUID() == GUID)
                    return this.objects[i];
            return null;
        }*/

        /// <summary>
        /// Returns all Objects at specified location.
        /// </summary>
        public List<Object> GetObjects(GridLocation location)
        {
            // Get List<Unit> and List<GridObject>
            // Cast them to List<Object>
            // And then add to summary list
            List<Object> objects = new List<Object>();
            objects.AddRange(GetGridObjects(location).Cast<Object>());
            objects.AddRange(GetUnits(location).Cast<Object>());

            return objects;
        }

        /// <summary>
        /// Returns all GridObjects at specified location.
        /// </summary>
        public List<GridObject> GetGridObjects(GridLocation location)
        {
            List<GridObject> objects;
            if (this.gridObjectContainer.TryGetValue(location, out objects))
            {
                return objects;
            }

            return new List<GridObject>();
        }

        /// <summary>
        /// Returns all Units at specified location.
        /// </summary>
        public List<Unit> GetUnits(GridLocation location)
        {
            List<Unit> objects;
            if (this.unitContainer.TryGetValue(location, out objects))
            {
                return objects;
            }

            return new List<Unit>();
        }

        /// <summary>
        /// Updates all Objects on current Map.
        /// </summary>
        /// <param name="timeP">Timer Tick Time.</param>
        public void UpdateState(int timeP)
        {
            this.isUpdating = true;

            foreach (Object obj in this.objectsToAdd)
            {
                AddNewObject(obj);
            }
            this.objectsToAdd.Clear();

            // Update each object or add to RemoveStack
            foreach (Object objectF in this.objects)
            {
                if (objectF.ObjectState == ObjectStates.Removed)
                {
                    this.objectsToRemove.Push(objectF);
                }
                else
                {
                    objectF.UpdateState(timeP);
                }
            }

            RemoveTaggedObjects();

            this.isUpdating = false;
        }

        /// <summary>
        /// Delete all objects, which is tagged as 'Removed'.
        /// </summary>
        private void RemoveTaggedObjects()
        {
            // Delete all removed objects
            int removeCount = this.objectsToRemove.Count;
            if (removeCount == 0)       // Skip deletion
                return;

            for (int i = 0; i < removeCount; ++i)
            {
                Object objectToRemove = this.objectsToRemove.Pop();
                // Store free GUIDs
                releasedGUIDs.Push(objectToRemove.GUID);

                // Remove from common List
                this.objects.Remove(objectToRemove);
                // Remove from specific container
                if (objectToRemove.ObjectType == ObjectTypes.GridObject)
                    RemoveObject<GridObject>(this.gridObjectContainer, objectToRemove.Position.Location, (GridObject)objectToRemove);
                else
                    RemoveObject<Unit>(this.unitContainer, objectToRemove.Position.Location, (Unit)objectToRemove);
            }

            this.objectsToRemove.Clear();
        }

        /// <summary>
        /// Places object into appropriate container and common objects List
        /// </summary>
        private void AddNewObject(Object newObject)
        {
            // Add GridObject
            if (newObject.ObjectType == ObjectTypes.GridObject)
            {
                AddNewObject<GridObject>(this.gridObjectContainer, (GridObject)newObject);
            }
            // Add Unit
            else if (newObject.ObjectType == ObjectTypes.Slug || newObject.ObjectType == ObjectTypes.Unit)
            {
                AddNewObject<Unit>(this.unitContainer, (Unit)newObject);
            }
            // Do not add any other object types
            else
                return;

            // Place object into common list
            this.objects.Add(newObject);

            newObject.LocationChanged += OnObjectLocationChanged;
        }

        private void AddNewObject<T>(Dictionary<GridLocation, List<T>> container, T newObject) where T : Object
        {
            List<T> objects;
            // Exists at this Location
            if (container.TryGetValue(newObject.Position.Location, out objects))
            {
                objects.Add((T)newObject);
            }
            // Add new
            else
            {
                objects = new List<T>() { newObject };
                container.Add(newObject.Position.Location, objects);
            }
        }

        private void RemoveObject<T>(Dictionary<GridLocation, List<T>> container, GridLocation location, T obj)
        {
            List<T> objects;
            if (container.TryGetValue(location, out objects))
            {
                objects.Remove(obj);
                // List is empty
                // Remove from dictionary
                if (objects.Count == 0)
                    container.Remove(location);
            }
        }



        /// <summary>
        /// Calls Unit.StartMotion for every unit object.
        /// </summary>
        public void StartMotion()
        {
            foreach (KeyValuePair<GridLocation, List<Unit>> kvp in this.unitContainer)
            {
                foreach(Unit unit in kvp.Value)
                    unit.StartMotion();
            }
        }
        /// <summary>
        /// Removes all Objects except Slug
        /// </summary>
        public void ClearEnvironment(bool isRemoveSlug)
        {
            foreach (Object objectF in this.objects)
            {
                if (objectF.ObjectType == ObjectTypes.Slug && !isRemoveSlug)
                    continue;

                this.objectsToRemove.Push(objectF);
            }
        }

        private void OnObjectLocationChanged(object sender, PositionEventArgs e)
        {
            // Relocate object, changing its Key in the container.
            if (((Object)sender).ObjectType == ObjectTypes.GridObject)
            {
                RelocateObject<GridObject>(this.gridObjectContainer, e.PrevPosition.Location, e.NewPosition.Location, (GridObject)sender);
            }
            else if (((Object)sender).ObjectType == ObjectTypes.Slug || ((Object)sender).ObjectType == ObjectTypes.Unit)
            {
                RelocateObject<Unit>(this.unitContainer, e.PrevPosition.Location, e.NewPosition.Location, (Unit)sender);
            }
        }

        /// <summary>
        /// Move Object to another location Key within its container
        /// </summary>
        private void RelocateObject<T>(Dictionary<GridLocation, List<T>> container, GridLocation prevLocation, GridLocation newLocation, T obj) where T : Object
        {
            // Remove from prev location
            RemoveObject<T>(container, prevLocation, obj);

            // Add this object as a new one
            AddNewObject<T>(container, obj);
        }
    }
}
Back to Top