/// <summary> /// Moves the given map object to the given layer of the map. /// </summary> /// <param name="mapObject">The map object to move.</param> /// <param name="targetLayer">The target layer.</param> /// <remarks>If the map object is already in the given layer then this function has no effect.</remarks> internal void ChangeMapObjectLayer(MapObject mapObject, MapObjectLayerEnum targetLayer) { if (this.scenario.Read() == null) { throw new InvalidOperationException("This scenario element doesn't not belong to a scenario!"); } if (mapObject == null) { throw new ArgumentNullException("mapObject"); } if (!this.mapObjectsOfThisElement.ContainsKey(mapObject)) { throw new InvalidOperationException("The given map object doesn't belong to this scenario element!"); } MapObjectLayerEnum currentLayer = this.mapObjectsOfThisElement[mapObject]; if (currentLayer == targetLayer) { return; } this.mapObjectsOfThisElementByLayer[currentLayer].Remove(mapObject); this.mapObjectsOfThisElementByLayer[targetLayer].Add(mapObject); this.mapObjectsOfThisElement[mapObject] = targetLayer; this.mapContext.GetMapObjectLayer(currentLayer).DetachContent(mapObject); this.mapContext.GetMapObjectLayer(targetLayer).AttachContent(mapObject); }
/// <summary> /// Gets the scenario elements of the given type that are attached to at least one of the given layers of the map inside the given area. /// </summary> /// <typeparam name="T">The type of the scenario elements to get.</typeparam> /// <param name="area"> /// <param name="firstLayer">The first layer to search in.</param> /// <param name="furtherLayers">List of the further layers to search in.</param> /// The area to search. /// </param> /// <returns> /// A list that contains the scenario elements of the given type that are attached to at least one of the given layers of the map inside /// the given area. /// </returns> public RCSet <T> GetElementsOnMap <T>(RCNumRectangle area, MapObjectLayerEnum firstLayer, params MapObjectLayerEnum[] furtherLayers) where T : ScenarioElement { if (area == RCNumRectangle.Undefined) { throw new ArgumentNullException("area"); } if (furtherLayers == null) { throw new ArgumentNullException("furtherLayers"); } RCSet <T> retList = new RCSet <T>(); foreach (MapObject mapObj in this.mapObjects[firstLayer].GetContents(area)) { T elementAsT = mapObj.Owner as T; if (elementAsT != null) { retList.Add(elementAsT); } } foreach (MapObjectLayerEnum furtherLayer in furtherLayers) { foreach (MapObject mapObj in this.mapObjects[furtherLayer].GetContents(area)) { T elementAsT = mapObj.Owner as T; if (elementAsT != null) { retList.Add(elementAsT); } } } return(retList); }
/// <summary> /// Gets whether this scenario element has at least one map object in at least one of the given layers of the map. /// </summary> public bool HasMapObject(MapObjectLayerEnum firstLayer, params MapObjectLayerEnum[] furtherLayers) { if (this.mapObjectsOfThisElementByLayer[firstLayer].Count > 0) { return(true); } return(furtherLayers.Any(furtherLayer => this.mapObjectsOfThisElementByLayer[furtherLayer].Count > 0)); }
/// <summary> /// Gets the scenario element of the given type with the given ID that is attached to at least one of the given layers of the map. /// </summary> /// <typeparam name="T">The type of the scenario element.</typeparam> /// <param name="id">The ID of the scenario element.</param> /// <param name="firstLayer">The first layer to search in.</param> /// <param name="furtherLayers">List of the further layers to search in.</param> /// <returns> /// The scenario element with the given ID or null if no scenario element of the given type with the given ID is attached to at /// least one of the given layers of the map. /// </returns> public T GetElementOnMap <T>(int id, MapObjectLayerEnum firstLayer, params MapObjectLayerEnum[] furtherLayers) where T : ScenarioElement { if (furtherLayers == null) { throw new ArgumentNullException("furtherLayers"); } if (this.idToScenarioElementMap.ContainsKey(id)) { T retElement = this.idToScenarioElementMap[id] as T; return(retElement != null && retElement.HasMapObject(firstLayer, furtherLayers) ? retElement : null); } return(null); }
/// <summary> /// Creates a map object for this scenario element to the given location in the given layer of the map. /// </summary> /// <param name="location">The location of the created map object on the map.</param> /// <param name="layer">The layer of the created map object.</param> /// <returns>The created map object.</returns> protected MapObject CreateMapObject(RCNumRectangle location, MapObjectLayerEnum layer) { if (this.scenario.Read() == null) { throw new InvalidOperationException("This scenario element doesn't not belong to a scenario!"); } if (location == RCNumRectangle.Undefined) { throw new ArgumentNullException("location"); } MapObject mapObject = new MapObject(this); mapObject.SetLocation(location); this.mapContext.GetMapObjectLayer(layer).AttachContent(mapObject); this.mapObjectsOfThisElementByLayer[layer].Add(mapObject); this.mapObjectsOfThisElement.Add(mapObject, layer); return(mapObject); }
/// <summary> /// Gets the map objects inside the given area of the given layers. /// </summary> /// <param name="area">The area to search.</param> /// <param name="firstLayer">The first layer to search in.</param> /// <param name="furtherLayers">List of the further layers to search in.</param> /// <returns>A list that contains the map objects inside the given area of the given layers.</returns> public RCSet <MapObject> GetMapObjects(RCNumRectangle area, MapObjectLayerEnum firstLayer, params MapObjectLayerEnum[] furtherLayers) { if (area == RCNumRectangle.Undefined) { throw new ArgumentNullException("area"); } if (furtherLayers == null) { throw new ArgumentNullException("furtherLayers"); } RCSet <MapObject> retList = this.mapObjects[firstLayer].GetContents(area); foreach (MapObjectLayerEnum furtherLayer in furtherLayers) { retList.UnionWith(this.mapObjects[furtherLayer].GetContents(area)); } return(retList); }
/// <summary> /// Destroys the given map object of this scenario element. /// </summary> /// <param name="mapObject">The map object to destroy.</param> protected void DestroyMapObject(MapObject mapObject) { if (this.scenario.Read() == null) { throw new InvalidOperationException("This scenario element doesn't not belong to a scenario!"); } if (mapObject == null) { throw new ArgumentNullException("mapObject"); } if (!this.mapObjectsOfThisElement.ContainsKey(mapObject)) { throw new InvalidOperationException("The given map object doesn't belong to this scenario element!"); } MapObjectLayerEnum layer = this.mapObjectsOfThisElement[mapObject]; this.mapObjectsOfThisElementByLayer[layer].Remove(mapObject); this.mapObjectsOfThisElement.Remove(mapObject); this.mapContext.GetMapObjectLayer(layer).DetachContent(mapObject); mapObject.Dispose(); }
/// <summary> /// Gets a reference to the given layer of map objects. /// </summary> public ISearchTree <MapObject> GetMapObjectLayer(MapObjectLayerEnum layer) { return(this.mapObjectLayers[layer]); }
/// <summary> /// Gets the scenario elements of the given type that are attached to at least one of the given layers of the map inside the search area /// around the given position. /// </summary> /// <typeparam name="T">The type of the scenario elements to get.</typeparam> /// <param name="position">The given position.</param> /// <param name="searchRadius">The radius of the search area given in quadratic tiles.</param> /// <param name="firstLayer">The first layer to search in.</param> /// <param name="furtherLayers">List of the further layers to search in.</param> /// <returns> /// A list that contains the scenario elements of the given type that are attached to at least one of the given layers of the map inside /// the search area. /// </returns> public RCSet <T> GetElementsOnMap <T>(RCNumVector position, int searchRadius, MapObjectLayerEnum firstLayer, params MapObjectLayerEnum[] furtherLayers) where T : ScenarioElement { if (position == RCNumVector.Undefined) { throw new ArgumentNullException("position"); } if (searchRadius <= 0) { throw new ArgumentOutOfRangeException("searchRadius", "The radius of the search area shall be greater than 0!"); } if (furtherLayers == null) { throw new ArgumentNullException("furtherLayers"); } RCIntVector quadCoordAtPosition = this.Map.GetCell(position.Round()).ParentQuadTile.MapCoords; RCIntVector topLeftQuadCoord = quadCoordAtPosition - new RCIntVector(searchRadius - 1, searchRadius - 1); RCIntVector bottomRightQuadCoord = quadCoordAtPosition + new RCIntVector(searchRadius - 1, searchRadius - 1); RCIntRectangle quadRect = new RCIntRectangle(topLeftQuadCoord, bottomRightQuadCoord - topLeftQuadCoord + new RCIntVector(1, 1)); RCSet <T> retList = new RCSet <T>(); foreach (MapObject mapObj in this.mapObjects[firstLayer].GetContents((RCNumRectangle)this.Map.QuadToCellRect(quadRect) - new RCNumVector(1, 1) / 2)) { T elementAsT = mapObj.Owner as T; if (elementAsT != null) { retList.Add(elementAsT); } } foreach (MapObjectLayerEnum furtherLayer in furtherLayers) { foreach (MapObject mapObj in this.mapObjects[furtherLayer].GetContents((RCNumRectangle)this.Map.QuadToCellRect(quadRect) - new RCNumVector(1, 1) / 2)) { T elementAsT = mapObj.Owner as T; if (elementAsT != null) { retList.Add(elementAsT); } } } return(retList); }