/// <summary> /// Adds the given element to this scenario. /// </summary> /// <param name="element">The scenario element to be added.</param> public void AddElementToScenario(ScenarioElement element) { if (this.addRemoveElementForbidden) { throw new InvalidOperationException("Adding element to this scenario is currently forbidden!"); } if (element == null) { throw new ArgumentNullException("element"); } int id = this.nextID.Read(); this.nextID.Write(id + 1); this.idToScenarioElementMap.Add(id, element); if (!this.updateInProgress) { this.scenarioElements.Add(element); } else { this.elementsToRemoveAfterUpdate.Remove(element); this.elementsToAddAfterUpdate.Add(element); } this.addRemoveElementForbidden = true; element.OnAddedToScenario(this, id, new ScenarioMapContext(this.mapObjects, this.fixedEntities)); this.addRemoveElementForbidden = false; }
/// <summary> /// Remove the given element from this scenario. /// </summary> /// <param name="element">The element to be removed.</param> /// <remarks> /// Disposing the removed element is always the responsibility of the caller, except when the element is removed during a /// scenario update procedure. /// </remarks> public void RemoveElementFromScenario(ScenarioElement element) { if (this.addRemoveElementForbidden) { throw new InvalidOperationException("Removing element from this scenario is currently forbidden!"); } if (element == null) { throw new ArgumentNullException("element"); } this.idToScenarioElementMap.Remove(element.ID.Read()); if (!this.updateInProgress) { this.scenarioElements.Remove(element); } else { this.elementsToRemoveAfterUpdate.Add(element); this.elementsToAddAfterUpdate.Remove(element); } this.addRemoveElementForbidden = true; element.OnRemovedFromScenario(); this.addRemoveElementForbidden = false; }
/// <summary> /// Constructs a MapObject instance. /// </summary> /// <param name="owner">Reference to the scenario element that owns this map object.</param> public MapObject(ScenarioElement owner) { if (owner == null) { throw new ArgumentNullException("owner"); } if (owner.ElementType.AnimationPalette == null) { throw new ArgumentException("The type of the given scenario element has no animation palette defined!", "owner"); } this.currentAnimations = new AnimationPlayer[owner.ElementType.AnimationPalette.Count]; this.quadraticPositionCache = new CachedValue <RCIntRectangle>(() => { RCIntVector topLeft = this.location.Location.Round(); RCIntVector bottomRight = (this.location.Location + this.location.Size).Round(); RCIntRectangle cellRect = new RCIntRectangle(topLeft.X, topLeft.Y, Math.Max(1, bottomRight.X - topLeft.X), Math.Max(1, bottomRight.Y - topLeft.Y)); return(this.owner.Scenario.Map.CellToQuadRect(cellRect)); }); this.quadraticShadowPositionCache = new CachedValue <RCIntRectangle>(() => { if (this.owner.ElementType.ShadowOffset == RCNumVector.Undefined || this.shadowTransition == new RCNumVector(0, 0)) { return(RCIntRectangle.Undefined); } RCNumRectangle shiftedLocation = this.location + (this.owner.ElementType.ShadowOffset + this.shadowTransition - this.location.Size / 2); RCIntVector topLeft = shiftedLocation.Location.Round(); RCIntVector bottomRight = (shiftedLocation.Location + shiftedLocation.Size).Round(); RCIntRectangle cellRect = new RCIntRectangle(topLeft.X, topLeft.Y, Math.Max(1, bottomRight.X - topLeft.X), Math.Max(1, bottomRight.Y - topLeft.Y)); return(this.owner.Scenario.Map.CellToQuadRect(cellRect)); }); this.owner = owner; this.location = RCNumRectangle.Undefined; this.shadowTransition = new RCNumVector(0, 0); }