void ReadTileLayers(ContentReader input, TiledMap map) { var layersCount = input.ReadInt32(); var layers = new TiledMapTileLayer[layersCount]; for (var i = 0; i < layersCount; i += 1) { var layer = new TiledMapTileLayer(); ReadLayer(input, layer); layer.Width = input.ReadInt32(); layer.Height = input.ReadInt32(); layer.TileWidth = map.TileWidth; layer.TileHeight = map.TileHeight; var tiles = new TiledMapTile[layer.Width][]; for (var x = 0; x < layer.Width; x += 1) { tiles[x] = new TiledMapTile[layer.Height]; } for (var y = 0; y < layer.Height; y += 1) { for (var x = 0; x < layer.Width; x += 1) { tiles[x][y] = ReadTile(input); } } layer.Tiles = tiles; layers[i] = layer; } map.TileLayers = layers; }
static TiledMapTileLayer ParseTileLayer(XmlNode layerXml) { var layer = new TiledMapTileLayer(); ParseBaseLayer(layerXml, layer); layer.Width = int.Parse(layerXml.Attributes["width"].Value); layer.Height = int.Parse(layerXml.Attributes["height"].Value); if (layerXml["data"].Attributes["encoding"].Value != "csv") { throw new NotSupportedException("Error while parsing layer " + layer.Name + ". Only CSV encoding is supported."); } // Parsing csv tile values. var tilemapValuesStr = layerXml["data"].InnerText.Split(','); var tilemapValues = new uint[tilemapValuesStr.Length]; for (var i = 0; i < tilemapValues.Length; i += 1) { tilemapValues[i] = uint.Parse(tilemapValuesStr[i]); } // Parsing csv tile values. // Initing tile array. /* * Pipeline cannot work with 2-dimensional arrays, * so we're stuck arrays of arrays. */ var tiles = new TiledMapTile[layer.Width][]; for (var x = 0; x < layer.Width; x += 1) { tiles[x] = new TiledMapTile[layer.Height]; } // Initing tile array. // Filling tilemap with tiles. for (var y = 0; y < layer.Height; y += 1) { for (var x = 0; x < layer.Width; x += 1) { tiles[x][y] = new TiledMapTile(); var tilemapValue = tilemapValues[y * layer.Width + x]; // Tile flip flags are stored in the tile value itself as 3 highest bits. tiles[x][y].FlipHor = ((tilemapValue & (uint)FlipFlags.FlipHor) != 0); tiles[x][y].FlipVer = ((tilemapValue & (uint)FlipFlags.FlipVer) != 0); tiles[x][y].FlipDiag = ((tilemapValue & (uint)FlipFlags.FlipDiag) != 0); tiles[x][y].GID = (int)(tilemapValue & (~(uint)FlipFlags.All)); } } // Filling tilemap with tiles. layer.Tiles = tiles; return(layer); }
void WriteTile(ContentWriter output, TiledMapTile tile) { output.Write(tile.GID); output.Write(tile.FlipHor); output.Write(tile.FlipVer); output.Write(tile.FlipDiag); }
/// <summary> /// Loads the tiled map and tileset and creates necessary GameObjects /// </summary> private void LoadMapContent() { if (Strategy == null) { Kazaam.XNAGame.Log("MapLoader has no Strategy loaded"); return; } foreach (TiledMapObjectLayer objectLayer in tiledMap.ObjectLayers) { try { map.StartingPosition = objectLayer.Objects[0].Position; } catch { } } foreach (TiledMapTileLayer tileLayer in tiledMap.TileLayers) { for (ushort x = 0; x < tileLayer.Width; x++) { for (ushort y = 0; y < tileLayer.Height; y++) { TiledMapTile?tileTry; tileLayer.TryGetTile(x, y, out tileTry); if (tileTry.HasValue) { TiledMapTile tile = (TiledMapTile)tileTry; if (tile.GlobalIdentifier > 0) { _strategy.Load(map, tileLayer.Name, x, y); } } } } } }
public static void ReadTiledMapLayer(ContentReader reader, ref TiledMap map) { int layernum = reader.ReadInt32(); map.Layer = new List <TiledMapLayer>(); for (int i = 0; i < layernum; i++) { var id = reader.ReadInt32(); var width = reader.ReadInt32(); var height = reader.ReadInt32(); var name = reader.ReadString(); map.Layer.Add(new TiledMapLayer { Id = id, Width = width, Height = height, Name = name }); map.Layer[i].Data = new TiledMapLayerData(); var tileCount = reader.ReadInt32(); map.Layer[i].Data.Tile = new List <TiledMapTile>(); for (int x = 0; x < tileCount; x++) { var tilegid = reader.ReadInt32(); var tiledata = new TiledMapTile() { Gid = tilegid }; map.Layer[i].Data.Tile.Add(tiledata); } } }
public string[] GetTileProperties(TiledMapTile tile, params string[] names) { var props = new string[names.Length]; var gid = tile.GlobalIdentifier; foreach (var tileset in this.Tiles.Tilesets) { if (tileset.ContainsGlobalIdentifier(gid)) { foreach (var tilesetTile in tileset.Tiles) { if (tilesetTile.LocalTileIdentifier == gid - tileset.FirstGlobalIdentifier) { for (var i = 0; i < props.Length; i++) { tilesetTile.Properties.TryGetValue(names[i], out props[i]); } break; } } break; } } return(props); }
TiledMapTile ReadTile(ContentReader input) { var tile = new TiledMapTile(); tile.GID = input.ReadInt32(); tile.FlipHor = input.ReadBoolean(); tile.FlipVer = input.ReadBoolean(); tile.FlipDiag = input.ReadBoolean(); return(tile); }
public FallingTile(Map map, TiledMapTile tile, Vector2 position) : base(map) { this.Position = position; var tilesetTile = tile.GetTilesetTile(map.Tiles); var tileset = tile.GetTileset(map.Tiles); this.texture = new TextureRegion(tileset.Texture, tileset.GetTextureRegion(tilesetTile)); this.light = map.CreateTileLight(position.X + 0.5F, position.Y + 0.5F, tilesetTile); if (this.light != null) { this.Map.Penumbra.Lights.Add(this.light); } }
private static PointF GetTilePosition(TiledMap map, TiledMapTile mapTile) { switch (map.Orientation) { case TiledMapOrientation.Orthogonal: return(TiledMapHelper.GetOrthogonalPosition(mapTile.X, mapTile.Y, map.TileWidth, map.TileHeight)); case TiledMapOrientation.Isometric: return(TiledMapHelper.GetIsometricPosition(mapTile.X, mapTile.Y, map.TileWidth, map.TileHeight)); default: throw new NotSupportedException($"{map.Orientation} Tiled Maps are not yet implemented."); } }
public static bool IsBlocking(this TiledMapTile Tile, IMap map) { var tiledMap = (map as TiledMooseMap)?.Map; if (tiledMap == null) { return(true); } var tileSet = tiledMap.GetTilesetByTileGlobalIdentifier(Tile.GlobalIdentifier); if (tileSet == null) { return(false); } var firstTile = tiledMap.GetTilesetFirstGlobalIdentifier(tileSet); var tileSetTile = tileSet.Tiles.FirstOrDefault(t => t.LocalTileIdentifier == Tile.GlobalIdentifier - firstTile); return(tileSetTile?.Properties.GetBoolProperty("blocking") ?? tileSet.Properties.GetBoolProperty("blocking") ?? false); }
public GameMap(TiledMap tiledMap) { TiledMap = tiledMap; tiles = new Tile[tiledMap.Width, tiledMap.Height]; foreach (var layer in tiledMap.TileLayers) { for (int x = 0; x < layer.Width; x++) { for (int y = 0; y < layer.Height; y++) { if (tiles[x, y] == null) { tiles[x, y] = new Tile(this, x, y); } layer.TryGetTile(x, y, out TiledMapTile? tiledMapTile); TiledMapTile layerTile = tiledMapTile.Value; if (layerTile.IsBlank) { continue; } TiledMapTileset tileset = TiledMap.GetTilesetByTileGlobalIdentifier(layerTile.GlobalIdentifier); TiledMapTilesetTile tilesetTile = tileset.Tiles.FirstOrDefault( t => t.LocalTileIdentifier == layerTile.GlobalIdentifier - tileset.FirstGlobalIdentifier); if (tilesetTile != null) { tilesetTile.Properties.TryGetValue("IsWalkable", out string isWalkable); tilesetTile.Properties.TryGetValue("IsPlaceable", out string isPlaceable); tilesetTile.Properties.TryGetValue("IsSpawnable", out string isSpawnable); tiles[x, y].IsWalkable = isWalkable == "true"; tiles[x, y].IsPlaceable = isPlaceable == "true"; tiles[x, y].IsSpawnable = isSpawnable == "true"; if (tiles[x, y].IsSpawnable) { spawnableTiles.Add(tiles[x, y]); } } } } } // Store references to each tile's adjacent tiles. foreach (var tile in tiles) { if (tile.Position.X > 0) { tile.AdjacentTiles.Add(this[tile.X - 1, tile.Y]); } if (tile.Position.X < TiledMap.Width - 1) { tile.AdjacentTiles.Add(this[tile.X + 1, tile.Y]); } if (tile.Position.Y > 0) { tile.AdjacentTiles.Add(this[tile.X, tile.Y - 1]); } if (tile.Position.Y < TiledMap.Height - 1) { tile.AdjacentTiles.Add(this[tile.X, tile.Y + 1]); } } }
protected void SetTilesFromTiledMap(TiledMap tiledMap) { Tiles = new List <Tile>(); List <TiledMapTile> tiledMapTiles = tiledMap.TileLayers.FirstOrDefault().Tiles.ToList(); TiledMapTileset tiledMapTileset = tiledMap.Tilesets.FirstOrDefault(); List <TiledMapTilesetTile> tiledMapTilesetTiles = tiledMapTileset.Tiles.ToList(); var test = tiledMapTiles.Where(t => t.GlobalIdentifier != 0).ToList(); int textureColumns = tiledMapTileset.Columns; Texture2D tilesetTexture = tiledMap.Tilesets.FirstOrDefault().Texture; int? rows = tiledMap.TileLayers.FirstOrDefault().Height; int? columns = tiledMap.TileLayers.FirstOrDefault().Width; int i = 0; for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { TiledMapTile tiledMapTile = tiledMapTiles[i]; Tile tile = new Tile { Container = this, Location = new Point(x, y), CollisionType = CollisionType.None, TileType = (TileType)tiledMapTile.GlobalIdentifier, MaxHealth = 0, CurrentHealth = 0, North = y == 0 ? null : columns * y - columns + x, NorthEast = y == 0 || x == columns - 1 ? null : columns * y - columns + x + 1, East = x == columns - 1 ? null : (int?)Tiles.Count() + 1, SouthEast = y == rows - 1 || x == columns - 1 ? null : columns * y + columns + x + 1, South = y == rows - 1 ? null : columns * y + columns + x, SouthWest = y == rows - 1 || x == 0 ? null : columns * y + columns + x - 1, West = x == 0 ? null : (int?)Tiles.Count() - 1, NorthWest = y == 0 || x == 0 ? null : columns * y - columns + x - 1, }; tile.Position = new Vector2(x * MainGame.TileSize, y * MainGame.TileSize) - Center + tile.TileCenter; Vector2 relativePosition = new Vector2(x * MainGame.TileSize, y * MainGame.TileSize); relativePosition -= Center; tile.Bounds = new Rectangle((int)relativePosition.X, (int)relativePosition.Y, MainGame.TileSize, MainGame.TileSize); // Set Image Data if (tiledMapTile.GlobalIdentifier > 0) { tile.Image = tilesetTexture; tile.ImageSource = new Rectangle( ((tiledMapTile.GlobalIdentifier - 1) % textureColumns) * Art.TileSize, ((tiledMapTile.GlobalIdentifier - 1) / textureColumns - 1 < 0 ? 0 : (tiledMapTile.GlobalIdentifier - 1) / textureColumns) * Art.TileSize, Art.TileSize, Art.TileSize); tile.Heading = tiledMapTile.IsFlippedDiagonally && tiledMapTile.IsFlippedHorizontally ? (float)Math.PI / 2 : tiledMapTile.IsFlippedDiagonally && tiledMapTile.IsFlippedVertically ? -(float)Math.PI / 2 : tiledMapTile.IsFlippedVertically ? (float)Math.PI : 0; } // Set Tileset Data TiledMapTilesetTile tiledMapTilesetTile = tiledMapTilesetTiles.FirstOrDefault(t => t.LocalTileIdentifier + 1 == tiledMapTile.GlobalIdentifier); if (tiledMapTilesetTile != null) { tile.CollisionType = tiledMapTilesetTile.Properties.ContainsKey("Collision") ? (CollisionType)int.Parse(tiledMapTilesetTile.Properties["Collision"]) : CollisionType.None; tile.MaxHealth = tiledMapTilesetTile.Properties.ContainsKey("Health") ? int.Parse(tiledMapTilesetTile.Properties["Health"]) : 0; tile.CurrentHealth = tile.MaxHealth; } Tiles.Add(tile); i++; } } }