public void Update(GameTime gameTime) { animationTimer.Update(gameTime); if (animationTimer.result) { for (int layersCounter = 0; layersCounter < layers.Count; layersCounter++) { Layer layer = layers[layersCounter]; for (int y = 1; y < height - 1; y++) { MapRow row = layer.Rows[y]; for (int x = 1; x < width - 1; x++) { MapCell cell = row.Columns[x]; if (cell.autotile) { if (cell.autotileAnimationCount == 3) { cell.texture -= 3 * 3; cell.autotileAnimationCount = 0; } else { cell.texture += 3; cell.autotileAnimationCount++; } animationTimer.Reset(); } } } } } }
public MapCell GetTileByPosition(Vector2 position, int currentLayer) { Layer layer = layers[currentLayer]; for (int y = 1; y < height - 1; y++) { MapRow row = layer.Rows[y]; for (int x = 1; x < width - 1; x++) { MapCell cell = row.Columns[x]; int cellPositionX = (int)(cell.position / new Vector2(Tile.size)).X; int cellPositionY = (int)(cell.position / new Vector2(Tile.size)).Y; int positionX = (int)(position / new Vector2(Tile.size)).X; int positionY = (int)(position / new Vector2(Tile.size)).Y; if (cellPositionX == positionX) { if (cellPositionY == positionY) { return(cell); } } } } return(null); }
public List <MapCell> GetTilesByTag(string tag) { List <MapCell> result = new List <MapCell>(); for (int layersCounter = 0; layersCounter < layers.Count; layersCounter++) { Layer layer = layers[layersCounter]; for (int y = 1; y < height - 1; y++) { MapRow row = layer.Rows[y]; for (int x = 1; x < width - 1; x++) { MapCell cell = row.Columns[x]; if (cell.tags.Contains(tag)) { result.Add(cell); } } } } return(result); }
public TileMap(string path, bool debugTileset = false) { this.debugTileset = debugTileset; tmxMap = new TmxMap(path); config = new TmxMap("Maps\\" + configName + ".tmx"); // 1. intialize tilesets = new Texture2D[tmxMap.Tilesets.Count]; for (int i = 0; i < tmxMap.Tilesets.Count; i++) { string name = Path.GetFileNameWithoutExtension(tmxMap.Tilesets[i].Image.Source); tilesets[i] = (Game.content.Load <Texture2D>("Textures\\Tilesets\\" + name)); tilesets[i].Name = name; } this.width = tmxMap.Width; //todo: /32 this.height = tmxMap.Height; for (int counter = 0; counter < tmxMap.Layers.Count; counter++) { layers.Add(new Layer()); } mapRect = new Rectangle(0, 0, width * Tile.size, height * Tile.size); // 2. build blank map, or if you want - intializing all layers foreach (Layer layer in layers) { for (int y = 0; y < height; y++) { //create new row MapRow newRow = new MapRow(); //add to its columns new cells for (int x = 0; x < width; x++) { newRow.Columns.Add(new MapCell()); } //add the new row to the current layer layer.Rows.Add(newRow); } } // 3. add map design for (int layersCounter = 0; layersCounter < layers.Count; layersCounter++) { Layer layer = layers[layersCounter]; for (int y = 0; y < height; y++) { MapRow row = layer.Rows[y]; for (int x = 0; x < width; x++) { MapCell cell = row.Columns[x]; int currentCell = y * (height) + x; TmxLayerTile tmxCell = tmxMap.Layers[layersCounter].Tiles[currentCell]; //texture and tileset if (tmxCell.Gid == 0) { cell.empty = true; } cell.texture = tmxCell.Gid; cell.tileset = tilesets[0]; int tilesSoFar = tmxMap.Tilesets[0].Tiles.Count; for (int tilesetsCounter = 0; tilesetsCounter < tmxMap.Tilesets.Count - 1; tilesetsCounter++) { if (tmxCell.Gid > tilesSoFar) { cell.texture = tmxCell.Gid - tilesSoFar; cell.tileset = tilesets[tilesetsCounter + 1]; } tilesSoFar += tmxMap.Tilesets[tilesetsCounter + 1].Tiles.Count; } //position cell.position = new Vector2(tmxCell.X * Tile.size, tmxCell.Y * Tile.size); //if autotile if (FindTileset(cell.tileset.Name).Properties["Autotile"] == "true") { cell.autotile = true; } //tile properties TmxTilesetTile tileResult = GetMapTile(cell); if (tileResult != null) { //collision if (tileResult.Properties["Passable"] == "X") { CreateCollisionObject(cell, tileResult, out cell.customCollisionSize); } //height if (tileResult.Properties["Height"] == "1") { cell.high = true; highCells.Add(cell); } else { mapCellsList.Add(cell); } //tags string tag = tileResult.Properties["Tag"]; if (tag != "") { cell.tags.Add(tag); } } } } } //check for autotiles for (int layersCounter = 0; layersCounter < layers.Count; layersCounter++) { Layer layer = layers[layersCounter]; for (int y = 1; y < height - 1; y++) { MapRow row = layer.Rows[y]; for (int x = 1; x < width - 1; x++) { MapCell cell = row.Columns[x]; int currentCell = y * (height) + x; TmxLayerTile tmxCell = tmxMap.Layers[layersCounter].Tiles[currentCell]; //same layer collision if (layersCounter == 0) //do it for just one time { if (CheckLayersPassableTag(tmxMap, y, x)) { List <GameObject> SameLayerCollisionList = collisionObjectList.FindAll(collisionObject => collisionObject.position == cell.position); foreach (GameObject gameObject in SameLayerCollisionList) { collisionObjectList.Remove(gameObject); } } } //check for autotiles if (cell.autotile) { if (row.Columns[x - 1].autotile && row.Columns[x + 1].autotile) { if (layer.Rows[y - 1].Columns[x].autotile && layer.Rows[y + 1].Columns[x].autotile) { //corners //upper left if (!layer.Rows[y - 1].Columns[x - 1].autotile) { cell.autotileCorner = MapCell.Corner.topLeft; } //upper right else if (!layer.Rows[y - 1].Columns[x + 1].autotile) { cell.autotileCorner = MapCell.Corner.topRight; } //lower left else if (!layer.Rows[y + 1].Columns[x - 1].autotile) { cell.autotileCorner = MapCell.Corner.bottomLeft; } //lower right else if (!layer.Rows[y + 1].Columns[x + 1].autotile) { cell.autotileCorner = MapCell.Corner.bottomRight; } //center } else { //down link if (layer.Rows[y - 1].Columns[x].autotile) { cell.texture += cell.tilesetWidth; } //up link if (layer.Rows[y + 1].Columns[x].autotile) { cell.texture -= cell.tilesetWidth; } } } else if (row.Columns[x - 1].autotile) { //right link if (layer.Rows[y - 1].Columns[x].autotile) { //down link cell.texture += cell.tilesetWidth; } if (layer.Rows[y + 1].Columns[x].autotile) { //up link cell.texture -= cell.tilesetWidth; } cell.texture++; } else if (row.Columns[x + 1].autotile) { //left link if (layer.Rows[y - 1].Columns[x].autotile) { //down link cell.texture += cell.tilesetWidth; } if (layer.Rows[y + 1].Columns[x].autotile) { //up link cell.texture -= cell.tilesetWidth; } cell.texture--; } } } } } }