public virtual void Update(GameTime gameTime) { foreach (KeyValuePair <string, T> baseObjectToAdd in ObjectsToAdd.Dictionary) { Add(baseObjectToAdd); } ObjectsToAdd.Clear(); foreach (KeyValuePair <string, T> baseObject in this.Dictionary) { if (!baseObject.Value.Alive) { RemoveObject(baseObject); } else { baseObject.Value.Update(gameTime); } } foreach (KeyValuePair <string, T> baseObjectToRemove in ObjectsToRemove.Dictionary) { Remove(baseObjectToRemove); } ObjectsToRemove.Clear(); }
/// <summary> /// Add all the objects in ObjectsToAdd to ActiveObjects, then update ActiveObjects before deleting objects in ObjectsToRemove /// </summary> /// <param name="elapsedGameTime">The seconds that have elapsed since the last update loop</param> public override void Update(float elapsedGameTime) { // Always call the super class' function - it will deal with whether it should run it itself base.Update(elapsedGameTime); // Add the objects and then clear the list - it is only a temporary holder ActiveObjects.AddRange(ObjectsToAdd); ObjectsToAdd.Clear(); // Loop through the active object foreach (T obj in ActiveObjects) { if (obj.ShouldUpdate) { // Update the object obj.Update(elapsedGameTime); } } // Remove all the objects that are no longer alive ActiveObjects.RemoveAll(x => x.IsAlive == false); // Remove all the objects we have marked to remove foreach (T obj in ObjectsToRemove) { ActiveObjects.Remove(obj); } ObjectsToRemove.Clear(); // Clear the list - it is only a temporary holder }
/// <summary> /// Returns the most recent child we added and is buffered to be inserted into ActiveObjects which is castable to the inputted type. /// Shouldn't really be called unless we have children waiting to be added. /// </summary> /// <returns>The most recent child we added</returns> public K LastChildToBeAdded <K>() where K : T { K lastChildOfType = ObjectsToAdd.FindLast(x => x is K) as K; DebugUtils.AssertNotNull(lastChildOfType); return(lastChildOfType); }
public void Add(T instance) { if (ObjectsToRemove.Contains(instance)) { ObjectsToRemove.Remove(instance); } if (!ObjectsToAdd.Contains(instance)) { ObjectsToAdd.Add(instance); } }
/// <summary> /// Because we add objects in the update loop, the current objects we need to load are in ObjectsToAdd /// Iterate through them and call LoadContent on each of them /// </summary> public override void LoadContent() { // Check to see whether we have already called LoadContent CheckShouldLoad(); foreach (T obj in ObjectsToAdd.FindAll(x => x.ShouldLoad)) { // Call load content on all objects which have not already been initialised obj.LoadContent(); } base.LoadContent(); }
public virtual void LoadContent() { foreach (T baseObject in ObjectsToAdd.Values) { Add(baseObject.Tag, baseObject); } ObjectsToAdd.Clear(); foreach (T baseObject in this.Values) { baseObject.LoadContent(); } }
/// <summary> /// Finds an object of the inputted name and casts to the inputted type K. /// First searches the ActiveObjects and then the ObjectsToAdd /// </summary> /// <typeparam name="K">The type we wish to return the found object as</typeparam> /// <param name="predicate">The predicate we will use to apply a condition for which we wish to find the object</param> /// <returns>Returns the object casted to K or null</returns> public K FindChild <K>(Predicate <T> predicate) where K : T { K obj = null; obj = ActiveObjects.Find(predicate) as K; if (obj != null) { return(obj); } obj = ObjectsToAdd.Find(predicate) as K; return(obj); }
/// <summary> /// Because we add objects in the update loop, the current objects we need to load are in ObjectsToAdd. /// Iterate through them, call Initialise on each of them and add to our ActiveObjects /// </summary> public override void Initialise() { // Check to see whether we have already called Initialise CheckShouldInitialise(); foreach (T obj in ObjectsToAdd.FindAll(x => x.ShouldInitialise)) { // Call initialise on all objects which have not already been initialised obj.Initialise(); } ActiveObjects.AddRange(ObjectsToAdd); ObjectsToAdd.Clear(); base.Initialise(); }
/// <summary> /// A function to add a child /// </summary> /// <typeparam name="K">The type of the child</typeparam> /// <param name="childToAdd">The child itself</param> /// <param name="load">A flag to indicate whether we wish to call LoadContent on the child</param> /// <param name="initialise">A flag to indicate whether we wish to call Initialise on the child</param> /// <returns></returns> public virtual K AddChild <K>(K childToAdd, bool load = false, bool initialise = false) where K : T { if (load) { childToAdd.LoadContent(); } if (initialise) { childToAdd.Initialise(); } ObjectsToAdd.Add(childToAdd); return(childToAdd); }
/// <summary> /// Iterate through all the objects in ActiveObjects and call Begin /// </summary> public override void Begin() { base.Begin(); // Do not need to check whether begun has already been called - this is guaranteed // Do another pass on our ObjectsToAdd to add any stragglers ActiveObjects.AddRange(ObjectsToAdd); ObjectsToAdd.Clear(); foreach (T obj in ActiveObjects.FindAll(x => !x.IsBegun)) { // Call begin on any object which has not already begun obj.Begin(); } }
public virtual void AddObject(T objectToAdd, string tag, bool load = false, bool linkWithObject = true) { if (load) { objectToAdd.LoadContent(); objectToAdd.Initialize(); } objectToAdd.Tag = tag; if (linkWithObject) { objectToAdd.ParentObjectManager = this as BaseObjectManager <BaseObject>; } ObjectsToAdd.Add(tag, objectToAdd); }
/// <summary> /// Returns whether an object satisfying the inputted predicate exists within the container. /// First serches the ActiveObjects and then the ObjectsToAdd. /// </summary> /// <param name="predicate">The predicate the object must satisfy</param> /// <returns>True if such an object exists and false if not</returns> public bool Exists(Predicate <T> predicate) { // Check our active objects for a valid object if (ActiveObjects.Exists(predicate)) { // Return true if one exists return(true); } // Check our objects to add for a valid object if (ObjectsToAdd.Exists(predicate)) { // Return true if one exists return(true); } // Otherwise no such object exists so return false return(false); }
public K GetObject <K>(string tag) where K : T { K item = GetItem <K>(tag); if (item == null) { if (ObjectsToAdd.Keys.Contains(tag)) { return((K)ObjectsToAdd.GetItem(tag)); } else { return(null); } } else { return(item); } }
/// <summary> /// adds a set of objects to a collection. /// </summary> public static void AddRange <T>(this ICollection <T> target, params T[] ObjectsToAdd) { ObjectsToAdd.ForEach(o => target.Add(o)); }
public void Clear() { ObjectsToAdd.Clear(); ObjectsToRemove.Clear(); ObjectsToUpdate.Clear(); }