Czoko_Smieciarka/Trunk/MonoGameView/DataModels/MapLoader.cs

115 lines
5.7 KiB
C#
Raw Permalink 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;
2019-04-22 16:50:40 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
2019-04-22 14:17:44 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
2019-04-22 16:50:40 +02:00
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
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);
2019-04-22 16:50:40 +02:00
List<ATrashCan> trashCans = new List<ATrashCan>();
foreach (XmlNode trashCanNode in objectNode.SelectSingleNode("TrashCans"))
{
GarbageType type;
switch (trashCanNode.SelectSingleNode("GarbageType").InnerText)
{
case "Glass":
type = GarbageType.Glass;
break;
case "Organic":
type = GarbageType.Organic;
break;
case "Paper":
type = GarbageType.Paper;
break;
default:
type = GarbageType.PlasticMetal;
break;
}
int canDensity = Convert.ToInt32(trashCanNode.SelectSingleNode("Density").InnerText);
int canProcessingTimePerUnit = Convert.ToInt32(trashCanNode.SelectSingleNode("ProcessingTimePerUnit").InnerText);
int volume = Convert.ToInt32(trashCanNode.SelectSingleNode("Volume").InnerText);
TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, canDensity,canProcessingTimePerUnit);
TrashCan trashCan = new TrashCan(canTypeOfGarbage, volume);
trashCans.Add(trashCan);
}
House house = new House(new Coords(x,y), trashCans);
2019-04-22 14:17:44 +02:00
grid[x, y] = house;
break;
2019-04-22 16:50:40 +02:00
case "Dump":
positionNode = objectNode.SelectSingleNode("Position");
x = Convert.ToInt32(positionNode.SelectSingleNode("X").InnerText);
y = Convert.ToInt32(positionNode.SelectSingleNode("Y").InnerText);
XmlNode garbageNode = objectNode.SelectSingleNode("Garbage");
TypeOfGarbage typeOfGarbage;
GarbageType garbageType;
switch (garbageNode.SelectSingleNode("GarbageType").InnerText)
{
case "Glass":
garbageType = GarbageType.Glass;
break;
case "Organic":
garbageType = GarbageType.Organic;
break;
case "Paper":
garbageType = GarbageType.Paper;
break;
default:
garbageType = GarbageType.PlasticMetal;
break;
}
int density = Convert.ToInt32(garbageNode.SelectSingleNode("Density").InnerText);
int processingTimePerUnit = Convert.ToInt32(garbageNode.SelectSingleNode("ProcessingTimePerUnit").InnerText);
int maxVolume = Convert.ToInt32(objectNode.SelectSingleNode("Volume").InnerText);
typeOfGarbage = new TypeOfGarbage(garbageType, density, processingTimePerUnit);
Dump dump = new Dump(typeOfGarbage, maxVolume, new Coords(x, y));
grid[x, y] = dump;
break;
2019-04-22 14:17:44 +02:00
}
}
}
}
}