Czoko_Smieciarka/Trunk/MonoGameView/DataModels/MapLoader.cs

56 lines
2.1 KiB
C#
Raw Normal View History

2019-04-22 14:17:44 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.DataModels
{
public class MapLoader
{
public void Load(out int size, out object[,] grid, string filename)
{
XmlDocument xml = new XmlDocument();
xml.Load(filename);
XmlNode node = xml.GetElementsByTagName("Map").Item(0);
XmlNode sizeNode = node.SelectSingleNode("/Map/Size");
size = Convert.ToInt32(sizeNode.InnerText);
grid = new object[size,size];
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
grid[x, y] = new Grass(new Coords(x,y));
}
}
foreach(XmlNode objectNode in node.SelectNodes("/Map/Objects/Object"))
{
XmlNode positionNode;
int x;
int y;
switch (objectNode.SelectSingleNode("Type").InnerText)
{
case "Road":
positionNode = objectNode.SelectSingleNode("Position");
x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText);
y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText);
Road1 road = new Road1(new Coords(x,y));
grid[x, y] = road;
break;
case "House":
positionNode = objectNode.SelectSingleNode("Position");
x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText);
y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText);
House house = new House(new Coords(x,y));
grid[x, y] = house;
break;
}
}
}
}
}