/// <summary> /// Creates a tile layer by reading the contentManager we got from the SharpTiles library. This will create our tile layers of type "Floor" and "Objects". /// </summary> /// <param name="layerContent"></param> /// <param name="tileSets"></param> /// <returns></returns> private TileLayer CreateTileLayer(LayerContent layerContent, IEnumerable <TileSetContent> tileSets) { if (layerContent == null) { throw new ArgumentNullException("layerContent"); } if (tileSets == null) { throw new ArgumentNullException("tileSets"); } TileLayerContent tileLayerContent = layerContent as TileLayerContent; TileLayerType tileLayerType = GetTileLayerType(layerContent); TileLayer tileLayer = new TileLayer(layerContent.Name, tileLayerContent.Width, tileLayerContent.Height, tileLayerType); foreach (uint tileID in tileLayerContent.Data) { uint flippedHorizontallyFlag = 0x80000000; uint flippedVerticallyFlag = 0x40000000; int tileIndex = (int)(tileID & ~(flippedVerticallyFlag | flippedHorizontallyFlag)); Tile tile = CreateTile(tileIndex, tileSets, tileLayerType); tileLayer.AddTile(tile); } return(tileLayer); }
private static TileLayer LoadTileLayer(Map map, TileLayerContent content) { var chunks = new List <Chunk>(); if (content.RawData != null) { } foreach (var chunkContent in content.Chunks) { if (chunkContent.RawData is JArray array) { var tiles = array.Select(x => x.Value <uint>()).ToArray(); var chunk = new Chunk(chunkContent.X, chunkContent.Y, chunkContent.Width, chunkContent.Height, tiles); chunks.Add(chunk); } } return(new TileLayer(content.Name, content.Width, content.Height, map.TileWidth, map.TileHeight, chunks, content.OffsetX, content.OffsetY, content.Opacity, content.Visible)); }
/// <summary>Default constructor creates a map from a .tmx file and creates any associated tileset textures by using the passed renderer. /// </summary> /// <param name="filePath">Path to the .tmx file to load</param> /// <param name="renderer">Renderer object used to load tileset textures</param> public TiledMap(string filePath, Renderer renderer, string contentRoot = "") { MapContent mapContent = new MapContent(filePath, renderer, contentRoot); TileWidth = mapContent.TileWidth; TileHeight = mapContent.TileHeight; Width = mapContent.Width * TileWidth; Height = mapContent.Height * TileHeight; foreach (LayerContent layer in mapContent.Layers) { if (layer is TileLayerContent) { TileLayerContent tileLayerContent = layer as TileLayerContent; TileLayer tileLayer = new TileLayer(tileLayerContent.Width, tileLayerContent.Height); for (int i = 0; i < tileLayerContent.Data.Length; i++) { // strip out the flipped flags from the map editor to get the real tile index uint tileID = tileLayerContent.Data[i]; uint flippedHorizontallyFlag = 0x80000000; uint flippedVerticallyFlag = 0x40000000; int tileIndex = (int)(tileID & ~(flippedVerticallyFlag | flippedHorizontallyFlag)); Tile tile = CreateTile(tileIndex, mapContent.TileSets); tileLayer.AddTile(tile); } tileLayers.Add(tileLayer); } else if (layer is ObjectLayerContent) { ObjectLayerContent objectLayerContent = layer as ObjectLayerContent; bool isCollidable = false; if (objectLayerContent.Name == "Collidables") { isCollidable = true; } MapObjectLayer mapObjectLayer = new MapObjectLayer(objectLayerContent.Name); foreach (ObjectContent objectContent in objectLayerContent.MapObjects) { MapObject mapObject = new MapObject(objectContent.Name, objectContent.Bounds, isCollidable, mapContent.Orientation); mapObjectLayer.AddMapObject(mapObject); } mapObjectLayers.Add(mapObjectLayer); } } CalculateTilePositions(mapContent.Orientation); }
private static TileLayerContent CreateTileLayer(TiledMap source, MapContent map, TiledTileLayer layer, int id) { var content = new TileLayerContent(); content.Id = id; content.Name = layer.Name; content.Opacity = layer.Opacity; for (var gridIndex = 0; gridIndex < layer.Tiles.Length; gridIndex++) { var tileIndex = layer.Tiles[gridIndex]; if (tileIndex == 0) { // The tile with index 0 is reserved (nothing). continue; } // Find witch tiled tileset the index belongs to... var tiledTileset = source.Tilesets.Where(t => t.FirstId <= tileIndex).OrderByDescending(t => t.FirstId).FirstOrDefault(); if (tiledTileset == null) { tiledTileset = source.Tilesets.Where(t => t.FirstId >= tileIndex).OrderByDescending(t => t.FirstId).FirstOrDefault(); if (tiledTileset == null) { throw new InvalidOperationException("Could not find tileset."); } } // Find the tileset reference. var tileset = map.Tilesets[source.Tilesets.IndexOf(tiledTileset)]; var tileContent = new TileContent(); tileContent.GridIndex = gridIndex; tileContent.TileSetId = tileset.Index; tileContent.TilesetIndex = tileIndex - tiledTileset.FirstId; content.Tiles.Add(tileContent); } return(content); }
private static void WriteTileLayer(BinaryWriter writer, TileLayerContent layer) { // Write layer information. writer.Write(layer.Id); writer.Write(layer.Name); writer.Write(layer.Opacity); // Write all tiles. writer.Write(layer.Tiles.Count); foreach (var tile in layer.Tiles) { writer.Write7BitEncodedInteger(tile.TileSetId); writer.Write7BitEncodedInteger(tile.TilesetIndex); writer.Write7BitEncodedInteger(tile.GridIndex); } // Write all entities. writer.Write(layer.Entities.Count); foreach (var entity in layer.Entities) { writer.Write(entity.Name); writer.Write(entity.Type); writer.Write(entity.Position.X); writer.Write(entity.Position.Y); writer.Write(entity.Size.Width); writer.Write(entity.Size.Height); // Write properties. writer.Write(entity.Properties.Count); foreach (var property in entity.Properties) { writer.Write(property.Name); writer.Write(property.Value); } } }
public override HauntedHouseMapContent Process(MapContent input, ContentProcessorContext context) { // build the textures //TiledHelpers.BuildTileSetTextures(input, context); // generate source rectangles TiledHelpers.GenerateTileSourceRectangles(input); // now build our output, first by just copying over some data HauntedHouseMapContent output = new HauntedHouseMapContent { TileWidth = input.TileWidth, TileHeight = input.TileHeight, }; // iterate all the layers of the input foreach (LayerContent layer in input.Layers) { // we only care about tile layers in our demo TileLayerContent tileLayerContent = layer as TileLayerContent; MapObjectLayerContent mapObjectLayerContent = layer as MapObjectLayerContent; if (tileLayerContent != null) { // create the new layer HauntedHouseTileLayerContent outLayer = new HauntedHouseTileLayerContent { Width = tileLayerContent.Width, Height = tileLayerContent.Height, LayerName = tileLayerContent.Name, }; // we need to build up our tile list now outLayer.Tiles = new HauntedHouseMapTileContent[tileLayerContent.Data.Length]; for (int i = 0; i < tileLayerContent.Data.Length; i++) { // get the ID of the tile uint tileID = tileLayerContent.Data[i]; // use that to get the actual index as well as the SpriteEffects int tileIndex; SpriteEffects spriteEffects; TiledHelpers.DecodeTileID(tileID, out tileIndex, out spriteEffects); // figure out which tile set has this tile index in it and grab // the texture reference and source rectangle. ExternalReference <Texture2DContent> textureContent = null; Rectangle sourceRect = new Rectangle(); bool isShadowCaster = new bool(); bool exists = new bool(); String imageSource = "NotDefined"; exists = true; // iterate all the tile sets foreach (var tileSet in input.TileSets) { if (tileIndex != 0) { // if our tile index is in this set if (tileIndex - tileSet.FirstId < tileSet.Tiles.Count) { imageSource = tileSet.Image; // store the texture content and source rectangle textureContent = tileSet.Texture; sourceRect = tileSet.Tiles[(int)(tileIndex - tileSet.FirstId)].Source; if (tileSet.Tiles[(int)(tileIndex - tileSet.FirstId)].Properties.ContainsKey("IsShadowCaster")) { isShadowCaster = Convert.ToBoolean(tileSet.Tiles[(int)(tileIndex - tileSet.FirstId)].Properties["IsShadowCaster"]); } // and break out of the foreach loop break; } } else { exists = false; } } // now insert the tile into our output outLayer.Tiles[i] = new HauntedHouseMapTileContent { TileSource = imageSource, Exists = exists, //Texture = textureContent, SourceRectangle = sourceRect, SpriteEffects = spriteEffects, IsShadowCaster = isShadowCaster, GridSize = new Vector2(output.TileWidth, output.TileHeight), Position = new Vector2((float)i % tileLayerContent.Width * output.TileWidth, (float)((Math.Floor((double)i / tileLayerContent.Width) * output.TileHeight))) }; } // add the layer to our output output.TileLayers.Add(outLayer); } if (mapObjectLayerContent != null) { // create the new layer HauntedHouseObjectLayerContent outLayer = new HauntedHouseObjectLayerContent { Width = mapObjectLayerContent.Width, Height = mapObjectLayerContent.Height, LayerName = mapObjectLayerContent.Name, }; //Variables Dictionary <String, String> properties = null; Rectangle bounds = new Rectangle(); String name = null; String type = null; // we need to build up our tile list now outLayer.Objects = new HauntedHouseMapObjectContent[mapObjectLayerContent.Objects.Count]; MapObjectContent[] mapLayerObjects = mapObjectLayerContent.Objects.ToArray(); for (int i = 0; i < outLayer.Objects.Length; i++) { if (mapLayerObjects[i].Properties != null) { properties = new Dictionary <string, string>(mapLayerObjects[i].Properties); } bounds = mapLayerObjects[i].Bounds; name = mapLayerObjects[i].Name; type = mapLayerObjects[i].Type; // now insert the tile into our output outLayer.Objects[i] = new HauntedHouseMapObjectContent { Properties = properties, EntityName = name, EntityType = type, EntityBounds = bounds }; } output.ObjectLayers.Add(outLayer); } } // return the output object. because we have ContentSerializerRuntimeType attributes on our // objects, we don't need a ContentTypeWriter and can just use the automatic serialization. return(output); }
public LevelContent(ProjectContent project, XmlDocument document) { this.Project = project; XmlNode levelNode = document["level"]; // Level values/attributes foreach (ValueTemplateContent value in project.Values) { XmlNode attribute = null; if ((attribute = levelNode.Attributes[value.Name]) != null) { if (value is BooleanValueTemplateContent) { this.Values.Add(new BooleanValueContent(value.Name, bool.Parse(attribute.Value))); } else if (value is IntegerValueTemplateContent) { this.Values.Add(new IntegerValueContent(value.Name, int.Parse(attribute.Value, CultureInfo.InvariantCulture))); } else if (value is NumberValueTemplateContent) { this.Values.Add(new NumberValueContent(value.Name, float.Parse(attribute.Value, CultureInfo.InvariantCulture))); } else if (value is StringValueTemplateContent) { this.Values.Add(new StringValueContent(value.Name, attribute.Value)); } } } // Height this.Height = int.Parse(levelNode.SelectSingleNode("height").InnerText, CultureInfo.InvariantCulture); // Width this.Width = int.Parse(levelNode.SelectSingleNode("width").InnerText, CultureInfo.InvariantCulture); // Layers // Here we'll construct an XPath query of all possible layer names so we can just extract the nodes all // at once. string[] layerNames = (from x in project.LayerSettings select x.Name).ToArray <string>(); string layerXPath = string.Join("|", layerNames); foreach (XmlNode layerNode in levelNode.SelectNodes(layerXPath)) { // Attempt to extract the settings for this layer. LayerSettingsContent[] s = (from x in project.LayerSettings where x.Name.Equals(layerNode.Name) select x).ToArray <LayerSettingsContent>(); if (!(s.Length > 0)) { continue; } LayerSettingsContent layerSettings = s[0]; // We have a grid layer. if (layerSettings is GridLayerSettingsContent) { GridLayerSettingsContent settings = layerSettings as GridLayerSettingsContent; GridLayerContent gridLayer = new GridLayerContent(layerNode, this, settings); if (gridLayer != null) { this.Layers.Add(gridLayer); } } else if (layerSettings is TileLayerSettingsContent) { TileLayerSettingsContent settings = layerSettings as TileLayerSettingsContent; TileLayerContent tileLayer = new TileLayerContent(layerNode, this, settings); if (tileLayer != null) { this.Layers.Add(tileLayer); } } else if (layerSettings is ObjectLayerSettingsContent) { ObjectLayerContent objectLayer = new ObjectLayerContent(layerNode, this); if (objectLayer != null) { this.Layers.Add(objectLayer); } } } }
public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context) { // build the textures TiledHelpers.BuildTileSetTextures(input, context, "maps"); // generate source rectangles TiledHelpers.GenerateTileSourceRectangles(input, "maps"); // now build our output, first by just copying over some data MapContent output = new MapContent { TileWidth = input.TileWidth, TileHeight = input.TileHeight, }; // iterate all the layers of the input foreach (LayerContent layer in input.Layers) { // Get layer objects var molc = layer as TiledLib.MapObjectLayerContent; if (molc != null) { //System.Diagnostics.Debugger.Launch(); // create the new layer MapObjectLayerContent outLayer = new MapObjectLayerContent { Width = molc.Width, Height = molc.Height, Name = molc.Name }; outLayer.Objects = new MapObjectContent[molc.Objects.Count]; for (int i = 0; i < molc.Objects.Count; i++) { var layerObject = molc.Objects[i]; outLayer.Objects[i] = new MapObjectContent { //ObjectType = layerObject.ObjectType, Name = layerObject.Name, Type = layerObject.Type, Bounds = layerObject.Bounds, Points = layerObject.Points }; } output.Layers.Add(outLayer); } // we only care about tile layers in our demo TileLayerContent tlc = layer as TileLayerContent; if (tlc != null) { // create the new layer MapTileLayerContent outLayer = new MapTileLayerContent { Width = tlc.Width, Height = tlc.Height, Name = tlc.Name }; // we need to build up our tile list now outLayer.Tiles = new MapTileContent[tlc.Data.Length]; for (int i = 0; i < tlc.Data.Length; i++) { // get the ID of the tile uint tileID = tlc.Data[i]; // if the tile is empty, let the tile be null if (tileID == 0) { continue; } // use that to get the actual index as well as the SpriteEffects int tileIndex; SpriteEffects spriteEffects; TiledHelpers.DecodeTileID(tileID, out tileIndex, out spriteEffects); // figure out which tile set has this tile index in it and grab // the texture reference and source rectangle. ExternalReference <Texture2DContent> textureContent = null; Rectangle sourceRect = new Rectangle(); // iterate all the tile sets foreach (var tileSet in input.TileSets) { // if our tile index is in this set if (tileIndex - tileSet.FirstId < tileSet.Tiles.Count) { // store the texture content and source rectangle textureContent = tileSet.Texture; sourceRect = tileSet.Tiles[(int)(tileIndex - tileSet.FirstId)].Source; // and break out of the foreach loop break; } } // now insert the tile into our output outLayer.Tiles[i] = new MapTileContent { LocalId = tileIndex, Texture = textureContent, SourceRectangle = sourceRect, SpriteEffects = spriteEffects }; } // add the layer to our output output.Layers.Add(outLayer); } } // return the output object. because we have ContentSerializerRuntimeType attributes on our // objects, we don't need a ContentTypeWriter and can just use the automatic serialization. return(output); }