Czoko_Smieciarka/Trunk/MonoGameView/DataModels/MapLoader.cs

57 lines
2.2 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;
2019-04-22 14:55:47 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
2019-04-22 14:17:44 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.DataModels
{
public class MapLoader
{
2019-04-22 14:55:47 +02:00
public void Load(out int size, out IDrawables[,] grid, string filename)
2019-04-22 14:17:44 +02:00
{
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);
2019-04-22 14:55:47 +02:00
grid = new IDrawables[size,size];
2019-04-22 14:17:44 +02:00
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;
}
}
}
}
}