Example #1
0
 public void AddTile(Vector2 tilePosition, Tile t)
 {
     if (!collidableTiles.Contains(t))
     {
         collidableTiles.Add(t);
         hashmap.Add(new TaggedVector { Position = new Vector2(tilePosition.X * TileWidth, tilePosition.Y * TileHeight),Identifier = t });
     }
 }
Example #2
0
        public static Tilemap Load(string path)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(new StreamReader(File.Open(path, FileMode.Open)));

            XmlNode meta = xDoc.GetElementsByTagName("map")[0];
            int width = int.Parse(meta.Attributes["width"].InnerText);
            int height = int.Parse(meta.Attributes["height"].InnerText);

            XmlNode data = xDoc.GetElementsByTagName("layer")[0].FirstChild;
            byte[] decodedData = Convert.FromBase64String(data.InnerText);

            int[] tileData = new int[decodedData.Length / 4];

            for (int i = 0; i < tileData.Length; i++)
            {
                tileData[i] = (int)BitConverter.ToUInt32(decodedData, 4 * i) - 1;
            }

            Tilemap map = new Tilemap(width, height);

            for (int x = 0; x < width; x++)
                for (int y = 0; y < height; y++)
                {
                    if (tileData[(y * width) + x] == 1)
                    {
                        if (map.SpawnTilePosition == -Vector2.One)
                            map.SpawnTilePosition = new Vector2(x, y);
                    }
                    else if (tileData[(y * width) + x] == 2)
                    {
                        map.EscapeDoor = new Vector2(x, y);
                    }
                    else if (tileData[(y * width) + x] == 3)
                    {
                        map.EnemySpawnPositions.Add(new Vector2(x, y));
                    }

                    if (tileData[(y * width) + x] == 0)
                    {
                        map[x, y] = new Tile { Collidable = true, TextureID = 0 };
                        map.CollidableTiles.Add(new TaggedVector { Position = new Vector2(x, y), Identifier = map[x, y] });
                    }
                    else if ((tileData[(y * width) + x] > 8 && tileData[(y * width) + x] < 19) || tileData[(y * width) + x] == 7)
                    {
                        map[x, y] = new Tile { Collidable = true, TextureID = tileData[(y * width) + x], KillOnCollide = true };
                        map.CollidableTiles.Add(new TaggedVector { Position = new Vector2(x, y), Identifier = map[x, y] });
                    }
                    else
                        map[x, y] = new Tile { Collidable = false, TextureID = tileData[(y * width) + x] };
                }

            return map;
        }
Example #3
0
 public void RemoveTile(Tile t)
 {
     if (collidableTiles.Contains(t))
     {
         collidableTiles.Remove(t);
         hashmap.Remove(t);
     }
 }