///<summary> /// Adds an entity to the manager. ///</summary> ///<param name="e">Entity to add.</param> ///<exception cref="InvalidOperationException">Thrown if the entity already belongs to a states manager.</exception> public void Add(Entity e) { lock (InterpolatedStates.FlipLocker) { lock (ReadBuffers.FlipLocker) { if (e.BufferedStates.BufferedStatesManager == null) { e.BufferedStates.BufferedStatesManager = this; e.BufferedStates.motionStateIndex = entities.Count; entities.Add(e); if (ReadBuffers.Enabled) { ReadBuffers.Add(e); } if (InterpolatedStates.Enabled) { InterpolatedStates.Add(e); } } else { throw new InvalidOperationException("Entity already belongs to a BufferedStatesManager; cannot add."); } } } }
///<summary> /// Removes an entity from the manager. ///</summary> ///<param name="e">Entity to remove.</param> ///<exception cref="InvalidOperationException">Thrown if the entity does not belong to this manager.</exception> public void Remove(Entity e) { lock (InterpolatedStates.FlipLocker) { lock (ReadBuffers.FlipLocker) { if (e.BufferedStates.BufferedStatesManager == this) { int index = entities.IndexOf(e); int endIndex = entities.Count - 1; entities[index] = entities[endIndex]; entities.RemoveAt(endIndex); if (index < entities.Count) { entities[index].BufferedStates.motionStateIndex = index; } if (ReadBuffers.Enabled) { ReadBuffers.Remove(index, endIndex); } if (InterpolatedStates.Enabled) { InterpolatedStates.Remove(index, endIndex); } e.BufferedStates.BufferedStatesManager = null; } else { throw new InvalidOperationException("Entity does not belong to this BufferedStatesManager; cannot remove."); } } } }