Ejemplo n.º 1
0
        public Map(string xmlFile, bool convert)
        {
            Terrain lastTerrainAdded;
            TileRow lastTileRowAdded;
            terrainList = new List<Terrain>();
            XmlDocument xmlDoc = new XmlDocument();
            if (!File.Exists(xmlFile))
            {
                throw new FileNotFoundException();
            }
            xmlDoc.Load(xmlFile);

            rows = int.Parse(xmlDoc.SelectSingleNode("MAP/MAP_DIMENSIONS/Height").FirstChild.Value);
            columns = int.Parse(xmlDoc.SelectSingleNode("MAP/MAP_DIMENSIONS/Width").FirstChild.Value);
            tileSize = int.Parse(xmlDoc.SelectSingleNode("MAP/MAP_DIMENSIONS/Tile_Size").FirstChild.Value);

            walkable = new int[rows, columns];
            XmlNodeList walkableTileRows = xmlDoc.SelectNodes("MAP/LAYOUT/CollisionLayer/RowInfo");

            int x = 0, y = 0;

            // Setup Layers
            XmlNodeList terrains = xmlDoc.SelectNodes("MAP/LAYOUT/Layer");
            foreach (XmlNode terrain in terrains)
            {
                lastTerrainAdded = new Terrain();
                terrainList.Add(lastTerrainAdded);

                XmlNodeList tileRows = terrain.SelectNodes("RowInfo");
                y = 0;
                foreach (XmlNode tileRow in tileRows)
                {
                    lastTileRowAdded = new TileRow();
                    lastTerrainAdded.TileRowList.Add(lastTileRowAdded);

                    String[] tiles = tileRow.FirstChild.Value.Split(',');
                    x = 0;
                    foreach (String tile in tiles)
                    {
                        int tileCode = int.Parse(tile);
                        if (tileCode == -1)
                            tileCode = Map.TRANSPARENT_INDEX;
                        if (int.Parse(terrain.Attributes["Id"].Value) == Map.ENTITY_LAYER)
                        {
                            switch (tileCode)
                            {
                                case Map.ENEMEY_SPAWN_TILE_CODE1:
                                case Map.ENEMEY_SPAWN_TILE_CODE2:
                                    tileCode = Map.TRANSPARENT_INDEX;
                                    this.enemySpawnPoints.Add(new Vector2(x * tileSize, y * tileSize));
                                    break;
                                case Map.GOAL_SPAWN_TILE_POINT:
                                    tileCode = Map.TRANSPARENT_INDEX;
                                    this.goalSpawnPoints.Add(new Vector2(x * tileSize, y * tileSize));
                                    break;
                                case Map.PLAYER_START_TILE_CODE:
                                    tileCode = Map.TRANSPARENT_INDEX;
                                    this.playerSpawnPoint = new Vector2(x * tileSize, y * tileSize);
                                    break;
                            }
                        }
                        lastTileRowAdded.TileList.Add(new Tile(tileCode, 0));
                        x++;
                    }
                    y++;
                }
                //break;
            }

            // Setup Walkable
            y = 0;
            foreach (XmlNode walkableTileRow in walkableTileRows)
            {
                String[] walkableTiles = walkableTileRow.FirstChild.Value.Split(',');
                x = 0;
                foreach (String walkableTile in walkableTiles)
                {
                    walkable[y, x] = int.Parse(walkableTile);
                    x++;
                }
                y++;
            }
        }
Ejemplo n.º 2
0
        public Map(string xmlFile)
        {
            Terrain lastTerrainAdded;
            TileRow lastTileRowAdded;
            XmlDocument xmlDoc;
            int x, y;

            terrainList = new List<Terrain>();
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlFile);

            XmlNodeList terrains = xmlDoc.SelectNodes("Map/TerrainLayer");
            foreach (XmlNode terrain in terrains)
            {
                lastTerrainAdded = new Terrain();
                terrainList.Add(lastTerrainAdded);

                XmlNodeList tileRows = terrain.SelectNodes("TileRow");
                rows = tileRows.Count;
                foreach (XmlNode tileRow in tileRows)
                {
                    lastTileRowAdded = new TileRow();
                    lastTerrainAdded.TileRowList.Add(lastTileRowAdded);

                    XmlNodeList tiles = tileRow.SelectNodes("Tile");
                    columns = tiles.Count;
                    foreach (XmlNode tile in tiles)
                    {
                        lastTileRowAdded.TileList.Add(
                            new Tile(
                                int.Parse(tile.Attributes["Position"].Value),
                                int.Parse(tile.Attributes["Rotation"].Value)));
                    }

                    walkable = new int[rows, columns];
                    XmlNodeList walkableTileRows = xmlDoc.SelectNodes("Map/WalkableLayer/TileRow");

                    y = 0;
                    foreach (XmlNode walkableTileRow in walkableTileRows)
                    {
                        XmlNodeList walkableTiles = walkableTileRow.SelectNodes("Tile");
                        x = 0;
                        foreach (XmlNode walkableTile in walkableTiles)
                        {
                            walkable[y, x] = int.Parse(walkableTile.Attributes["Walkable"].Value);
                            x++;
                        }
                        y++;
                    }
                }
            }
        }