This commit is contained in:
ryuga4 2019-04-23 06:32:35 +02:00
parent 3167789721
commit 39776ce06a
29 changed files with 464 additions and 249 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="imageFilePath" value="FOLDERPATH\input.bmp"/>
<add key="outputFilePath" value="FOLDERPATH\output.xml"/>
<add key="imageFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\map.bmp"/>
<add key="outputFilePath" value="C:\Users\micjan11\Projekty\Czoko_Smieciarka\Trunk\MonoGameView\bin\Windows\x86\Debug\map.xml"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />

View File

@ -22,7 +22,7 @@ namespace MapEditor.Helpers
return FieldType.House;
if (color == Color.FromArgb(255,255,0))
return FieldType.House;
return FieldType.Dump;
throw new NotImplementedException($"Conversion form {color.ToKnownColor()} has not been implemented.");

View File

@ -17,19 +17,36 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
{
public DFS()
{
}
int count = 0;
public List<Coords> Houses { get; set; }
public List<IStep> BestPath(ContentManager content, AGarbageCollector collector, object[,] grid)
public List<IStep> BestPath(ContentManager content, GarbageCollector collector, ICloneable[,] grid)
{
Houses = new List<Coords>();
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is House)
{
Houses.Add(new Coords(x,y));
}
}
}
var r=Search(content, collector, grid, 0).Key;
Console.WriteLine(count);
if (r == null) return new List<IStep>();
return r;
}
List<IStep> PossibleSteps(ContentManager content, AGarbageCollector collector, object[,] grid)
List<IStep> PossibleSteps(ContentManager content, AGarbageCollector collector, ICloneable[,] grid)
{
@ -44,14 +61,13 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
var filteredMoveSteps = new List<IStep>();
foreach (var item in moveSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone(content);
var copyGrid = (object[,])grid.Clone();
var copyCollector = (AGarbageCollector)collector.Clone();
try
{
item.Invoke(copyCollector, grid);
var gcx = copyCollector.Coords.X;
var gcy = copyCollector.Coords.Y;
if (grid[gcx, gcy] is IRoad1 || grid[gcx, gcy] is IGarbageLocalization || grid[gcx, gcy] is ADump)
if (grid[gcx, gcy] is Road1 || grid[gcx, gcy] is House || grid[gcx, gcy] is ADump)
{
result.Add(item);
}
@ -61,21 +77,78 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
}
}
if (grid[collector.Coords.X, collector.Coords.Y] is IGarbageLocalization)
{
var collectSteps = new List<IStep>()
{
new CollectStep(GarbageType.Glass),
new CollectStep(GarbageType.Organic),
new CollectStep(GarbageType.Paper),
new CollectStep(GarbageType.PlasticMetal)
};
foreach (var item in collectSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid);
try
{
item.Invoke(copyCollector, copyGrid);
result.Add(item);
}
catch (Exception e)
{
}
}
}
if (grid[collector.Coords.X, collector.Coords.Y] is ADump)
{
var collectSteps = new List<IStep>()
{
new SpillStep(GarbageType.Glass),
new SpillStep(GarbageType.Organic),
new SpillStep(GarbageType.Paper),
new SpillStep(GarbageType.PlasticMetal)
};
foreach (var item in collectSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid);
try
{
item.Invoke(copyCollector, copyGrid);
result.Add(item);
}
catch (Exception e)
{
}
}
}
return result;
}
KeyValuePair<List<IStep>, int> Search(ContentManager content, AGarbageCollector collector, object[,] grid, int length)
KeyValuePair<List<IStep>, int> Search(ContentManager content, GarbageCollector collector, ICloneable[,] grid, int length)
{
if (length > 200) return new KeyValuePair<List<IStep>, int>(null,length);
count++;
if (length > 40) return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
var possibleSteps = PossibleSteps(content, collector, grid);
if (Houses.All(c => (grid[c.X, c.Y] as IGarbageLocalization).TrashCans.All(j => j.FillPercent == 0.0))
&&
collector.TrashContainers.All(i => i.FillPercent == 0.0)
)
{
return new KeyValuePair<List<IStep>, int>(new List<IStep>(), length);
}
var possibleSteps = PossibleSteps(content, collector, grid);
foreach (var item in possibleSteps)
{
var copyCollector = (AGarbageCollector)collector.Clone(content);
var copyGrid = (object[,])grid.Clone();
var copyCollector = (GarbageCollector)collector.Clone();
var copyGrid = CopyGrid(grid);
//if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2();
item.Invoke(copyCollector, copyGrid);
@ -102,5 +175,17 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms
}
private ICloneable[,] CopyGrid(ICloneable[,] grid)
{
ICloneable[,] result = new ICloneable[grid.GetLength(0), grid.GetLength(1)];
for (int x=0;x< grid.GetLength(0); x++)
{
for (int y=0;y< grid.GetLength(1); y++)
{
result[x, y] = (ICloneable) grid[x, y].Clone();
}
}
return result;
}
}
}

View File

@ -8,7 +8,7 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage
{
public abstract class AGarbage : IGarbage
{
protected AGarbage(ITypeOfGarbage typeOfGarbage, int weight)
protected AGarbage(ITypeOfGarbage typeOfGarbage, double weight)
{
this.TypeOfGarbage = typeOfGarbage;
this.Weight = weight;
@ -16,11 +16,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage
public ITypeOfGarbage TypeOfGarbage { get; }
public virtual int Weight { get; set; }
public virtual double Weight { get; set; }
public virtual int Volume
public virtual double Volume
{
get { return this.Weight / TypeOfGarbage.Density; }
get { return (double) this.Weight / TypeOfGarbage.Density; }
}
protected abstract AGarbage Add(AGarbage garbageToAdd);
@ -40,9 +40,9 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage
#endregion
public object Clone()
public virtual object Clone()
{
return this.MemberwiseClone();
return new NotImplementedException("xD");
}
}
}

View File

@ -10,8 +10,8 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage
{
ITypeOfGarbage TypeOfGarbage { get; }
int Weight { get; }
double Weight { get; }
int Volume { get; }
double Volume { get; }
}
}

View File

@ -57,14 +57,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector
}
Coords.X += 1;
}
public virtual object Clone(ContentManager content)
{
throw new NotImplementedException();
}
public IEnumerable<AGarbageCollectorContainer> TrashContainers { get; }
public object Clone()
public virtual object Clone()
{
throw new NotImplementedException();
}

View File

@ -1,9 +1,10 @@
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces
{
public interface IDrawables
public interface IDrawables : ICloneable
{
void Draw(SpriteBatch spriteBatch, int size);
}

View File

@ -7,7 +7,7 @@ using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces
{
public interface ITypeOfGarbage
public interface ITypeOfGarbage : ICloneable
{
GarbageType GarbageType { get; }

View File

@ -1,8 +1,10 @@
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
{
public abstract class AGarbageCollectorContainer : ATrashCan
{
protected AGarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume)
protected AGarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume, AGarbage garbage) : base(typeOfGarbage, maxVolume,garbage)
{
}

View File

@ -1,14 +1,16 @@
using System;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
{
public abstract class ATrashCan
public abstract class ATrashCan : ICloneable
{
protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume)
{
this.MaxVolume = maxVolume;
this.TypeOfGarbage = typeOfGarbage;
Garbage = new BasicGarbage(typeOfGarbage, 0);
}
protected ATrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, AGarbage garbage)
{
@ -23,26 +25,34 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans
public AGarbage Garbage { get; private set; }
public int FillPercent
public double FillPercent
{
get { return Garbage.Volume / this.MaxVolume; }
get { return ((double)Garbage.Volume) / this.MaxVolume; }
}
public virtual bool AddGarbage(IGarbage garbage)
public virtual void AddGarbage(AGarbage garbage)
{
if (this.TypeOfGarbage != garbage.TypeOfGarbage)
if (this.TypeOfGarbage.GarbageType != garbage.TypeOfGarbage.GarbageType)
throw new Exception("You cannot add up different type garbage!");
var newGarbage = this.Garbage + Garbage;
var newGarbage = this.Garbage + garbage;
if (newGarbage.Volume > this.MaxVolume)
return false;
throw new Exception("Trash overload");
this.Garbage = newGarbage;
return true;
return;
}
public virtual object Clone()
{
throw new NotImplementedException();
}
public virtual AGarbage TakeGarbage()
{
if (this.Garbage.Weight == 0)
throw new Exception("Pusty śmietnik matole");
var result = (AGarbage)this.Garbage.Clone();
this.Garbage.Weight = 0;
return result;

View File

@ -1,114 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.DataModels
{
public class MapLoader
{
public void Load(out int size, out IDrawables[,] 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 IDrawables[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);
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);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using MonoGameView.DataModels.Models;
namespace MonoGameView.DataModels
{
public class MapLoader
{
public void Load(out int size, out IDrawables[,] 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 IDrawables[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;
var dupa = objectNode.SelectSingleNode("Type").InnerText.Trim();
switch (dupa)
{
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);
List<TrashCan> trashCans = new List<TrashCan>();
foreach (XmlNode trashCanNode in objectNode.SelectNodes("TrashCans/Can"))
{
GarbageType type;
switch (trashCanNode.SelectSingleNode("GarbageType").InnerText.Trim())
{
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);
grid[x, y] = house;
break;
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);
TypeOfGarbage canTypeOfGarbage = new TypeOfGarbage(type, canDensity,canProcessingTimePerUnit);
TrashCan trashCan = new TrashCan(canTypeOfGarbage, volume, new BasicGarbage(canTypeOfGarbage, volume));
trashCans.Add(trashCan);
}
House house = new House(new Coords(x,y), trashCans);
grid[x, y] = house;
break;
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.Trim())
{
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;
}
}
}
}
}
typeOfGarbage = new TypeOfGarbage(garbageType, density, processingTimePerUnit);
Dump dump = new Dump(typeOfGarbage, maxVolume, new Coords(x, y));
grid[x, y] = dump;
break;
}
}
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
{
public class BasicGarbage : AGarbage
{
public BasicGarbage(ITypeOfGarbage typeOfGarbage, double weight) : base(typeOfGarbage, weight)
{
}
protected override AGarbage Add(AGarbage garbageToAdd)
{
return new BasicGarbage(TypeOfGarbage, Weight + garbageToAdd.Weight);
}
protected override AGarbage Subtract(AGarbage garbageToSubtract)
{
return new BasicGarbage(TypeOfGarbage, Weight - garbageToSubtract.Weight);
}
public override object Clone()
{
return new BasicGarbage((ITypeOfGarbage)TypeOfGarbage.Clone(), Weight);
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{
public class Coords
public class Coords : ICloneable
{
public Coords(int x,int y)
{
@ -50,5 +50,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{
return base.GetHashCode();
}
public object Clone()
{
return new Coords(X, Y);
}
}
}

View File

@ -17,5 +17,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
{
batch.Draw(ImageContainer.GetImage(TypeOfGarbage.GarbageType.ToString()), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
public override object Clone()
{
return new Dump((ITypeOfGarbage)TypeOfGarbage.Clone(), MaxVolume, (Coords)Coords.Clone());
}
}
}

View File

@ -0,0 +1,37 @@
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MonoGameView.DataModels.Models
{
public class EmptyHouse : IDrawables, IGarbageLocalization, ICloneable
{
public Coords Coords { get; }
public EmptyHouse(Coords coords)
{
Coords = coords;
TrashCans = new List<ATrashCan>();
}
public void Draw(SpriteBatch batch, int size)
{
batch.Draw(ImageContainer.GetImage("road2"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
public object Clone()
{
return new House((Coords)Coords.Clone(), new List<TrashCan>());
}
public IEnumerable<ATrashCan> TrashCans { get; set; }
}
}

View File

@ -1,24 +0,0 @@
using System;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
{
public class Garbage : AGarbage
{
public Garbage(ITypeOfGarbage typeOfGarbage, int weight) : base(typeOfGarbage, weight)
{
}
protected override AGarbage Add(AGarbage garbageToAdd)
{
throw new NotImplementedException();
}
protected override AGarbage Subtract(AGarbage garbageToSubtract)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,34 +1,35 @@
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MonoGameView.DataModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{
public class GarbageCollector : AGarbageCollector, IDrawables
{
public GarbageCollector(Coords c, List<AGarbageCollectorContainer> l, int rows, int cols) : base(c,l,rows,cols)
{
}
public void Draw(SpriteBatch batch,int size)
{
batch.Draw(ImageContainer.GetImage("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White);
}
public override object Clone(ContentManager content)
{
var cloneList = new List<AGarbageCollectorContainer>();
return new GarbageCollector(new Coords(Coords.X,Coords.Y), cloneList, rows,columns);
}
}
}
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{
public class GarbageCollector : AGarbageCollector, IDrawables
{
public GarbageCollector(Coords c, List<GarbageCollectorContainer> l, int rows, int cols) : base(c,l,rows,cols)
{
}
public void Draw(SpriteBatch batch,int size)
{
batch.Draw(ImageContainer.GetImage("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White);
}
public override object Clone()
{
var cloneList = TrashContainers.Select(x=>(GarbageCollectorContainer)x.Clone()).ToList();
return new GarbageCollector((Coords)Coords.Clone(), cloneList, rows,columns);
}
}
}

View File

@ -5,8 +5,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
{
public class GarbageCollectorContainer : AGarbageCollectorContainer
{
public GarbageCollectorContainer(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume)
public GarbageCollectorContainer(TypeOfGarbage typeOfGarbage, int maxVolume, BasicGarbage garbage) : base(typeOfGarbage, maxVolume,garbage)
{
}
public override object Clone()
{
return new GarbageCollectorContainer((TypeOfGarbage)TypeOfGarbage.Clone(), MaxVolume,(BasicGarbage)Garbage.Clone());
}
}
}

View File

@ -20,9 +20,16 @@ namespace MonoGameView.DataModels.Models
Coords = coords;
}
public object Clone()
{
return new Grass((Coords)Coords.Clone());
}
public void Draw(SpriteBatch batch, int size)
{
batch.Draw(ImageContainer.GetImage("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using Microsoft.Xna.Framework;
@ -12,19 +13,25 @@ using MonoGameView.DataModels.Models;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
{
public class House : IDrawables, IGarbageLocalization
public class House : IDrawables, IGarbageLocalization, ICloneable
{
public Coords Coords { get; }
public House(Coords coords, IEnumerable<ATrashCan> trashCans)
public House(Coords coords, IEnumerable<TrashCan> trashCans)
{
Coords = coords;
TrashCans = trashCans;
}
public void Draw(SpriteBatch batch, int size)
{
batch.Draw(ImageContainer.GetImage("house"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);
}
public object Clone()
{
return new House((Coords) Coords.Clone(), TrashCans.Select(x => (TrashCan) x.Clone()).ToList());
}
public IEnumerable<ATrashCan> TrashCans { get; set; }
}
}

View File

@ -20,6 +20,11 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
Coords = coords;
}
public object Clone()
{
return new Road1((Coords)Coords.Clone());
}
public void Draw(SpriteBatch batch, int size)
{
batch.Draw(ImageContainer.GetImage("road1"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);

View File

@ -17,8 +17,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models
public Road2(Coords coords)
{
Coords = coords;
}
}
public object Clone()
{
return new Road2((Coords)Coords.Clone());
}
public void Draw(SpriteBatch batch, int size)
{
batch.Draw(ImageContainer.GetImage("road2"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White);

View File

@ -3,35 +3,47 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
using MonoGameView.DataModels.Models;
namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{
public class CollectStep : IStep
{
public CollectStep(ITypeOfGarbage typeOfGarbage)
public CollectStep(GarbageType typeOfGarbage)
{
this._typeOfGarbage = typeOfGarbage;
}
private ITypeOfGarbage _typeOfGarbage;
private GarbageType _typeOfGarbage;
public void Invoke(IGarbageCollector garbageCollector, object [,] grid)
public void Invoke(IGarbageCollector _garbageCollector, object [,] grid)
{
/*
var _garbageLocalization = (IGarbageLocalization) grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
if(_garbageCollector.Coords != _garbageLocalization.Coords)
throw new WrongPositionException("Śmieciarka nie jest w miejscu oderbania śmieci");
var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage == _typeOfGarbage);
var trashCans = _garbageLocalization.TrashCans.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage);
var garbage = trashCans.Select(t => t.TakeGarbage()).Aggregate((a,b)=>a+b);
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage))
throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage.GarbageType}.");
if (_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
throw new TrashContainerNotFound($"Nie znaleziono kontenera na {_typeOfGarbage}.");
_garbageCollector.TrashContainers.First(t => t.TypeOfGarbage == _typeOfGarbage).AddGarbage(garbage);
*/
_garbageCollector.TrashContainers.First(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage).AddGarbage(garbage);
for (int x= 0;x < grid.GetLength(0); x++){
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x,y));
}
}
if (_garbageLocalization.TrashCans.All(x => x.FillPercent == 0.0))
grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y] = new EmptyHouse(new Coords(_garbageCollector.Coords.X, _garbageCollector.Coords.Y));
}
}
}

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
@ -11,29 +13,37 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models.Steps
{
public class SpillStep : IStep
{
public SpillStep(ITypeOfGarbage typeOfGarbage)
public SpillStep(GarbageType typeOfGarbage)
{
this._typeOfGarbage = typeOfGarbage;
}
private ITypeOfGarbage _typeOfGarbage;
private GarbageType _typeOfGarbage;
public void Invoke(IGarbageCollector garbageCollector, object [,] grid)
{/*
public void Invoke(IGarbageCollector _garbageCollector, object [,] grid)
{
var _dump = (ADump)grid[_garbageCollector.Coords.X, _garbageCollector.Coords.Y];
if(_garbageCollector.Coords != _dump.Coords)
throw new WrongPositionException("Śmieciarka nie na terenie podanego wyspiska");
if(_dump.TypeOfGarbage != _typeOfGarbage)
throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage.GarbageType}");
if(_dump.TypeOfGarbage.GarbageType != _typeOfGarbage)
throw new TrashContainerNotFound($"Wysypisko nie przyjmuje smieci typu {_typeOfGarbage}");
if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage != _typeOfGarbage))
throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage.GarbageType}!");
if(_garbageCollector.TrashContainers.All(c => c.TypeOfGarbage.GarbageType != _typeOfGarbage))
throw new TrashContainerNotFound($"Smieciarka nie ma pojemnika na {_typeOfGarbage}!");
var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage == _typeOfGarbage)
var garbage = _garbageCollector.TrashContainers.Where(t => t.TypeOfGarbage.GarbageType == _typeOfGarbage)
.Select(t => t.TakeGarbage())
.Aggregate((a, b) => a + b);
_dump.AddGarbage(garbage);*/
_dump.AddGarbage(garbage);
for (int x = 0; x < grid.GetLength(0); x++)
{
for (int y = 0; y < grid.GetLength(1); y++)
{
if (grid[x, y] is Road2) grid[x, y] = new Road1(new Coords(x, y));
}
}
}
}
}

View File

@ -1,4 +1,5 @@
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.Garbage;
using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans;
namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
@ -8,8 +9,13 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume) : base(typeOfGarbage, maxVolume)
{
}
public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, Garbage garbage) : base(typeOfGarbage, maxVolume, garbage)
public TrashCan(ITypeOfGarbage typeOfGarbage, int maxVolume, BasicGarbage garbage) : base(typeOfGarbage, maxVolume, garbage)
{
}
public override object Clone()
{
return new TrashCan((TypeOfGarbage) TypeOfGarbage.Clone(), MaxVolume, (BasicGarbage) Garbage.Clone());
}
}
}

View File

@ -15,5 +15,10 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models
public GarbageType GarbageType { get; }
public int ProcessingTimePerUnit { get; }
public int Density { get; }
public object Clone()
{
return new TypeOfGarbage(GarbageType, Density, ProcessingTimePerUnit);
}
}
}

View File

@ -11,6 +11,8 @@ using System.Linq;
using CzokoŚmieciarka.MonoGameView.Algorithms;
using MonoGameView.DataModels;
using MonoGameView.DataModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.MonoGameView.DataModels.Enums;
namespace CzokoŚmieciarka.MonoGameView
{
@ -22,14 +24,10 @@ namespace CzokoŚmieciarka.MonoGameView
private static int size;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D road1;
Texture2D road2;
Texture2D grass;
Texture2D house;
MapLoader mapLoader = new MapLoader();
Vector2 roadPos;
float timer;
const float TIMER = 0.2f;
const float TIMER = 0.02f;
int stepN;
List<IStep> steps;
GarbageCollector collector;
@ -55,11 +53,18 @@ namespace CzokoŚmieciarka.MonoGameView
ImageContainer.InitContainer(Content);
// TODO: Add your initialization logic here
roadPos = new Vector2(0, 0);
timer = 0.2f;
timer = 5f;
mapLoader.Load(out size,out grid,"map.xml");
collector = new GarbageCollector(new Coords(0, 0), new List<AGarbageCollectorContainer>(), size, size);
var containers = new List<GarbageCollectorContainer>()
{
new GarbageCollectorContainer(
new TypeOfGarbage(GarbageType.Glass,50,1),
100,
new BasicGarbage(new TypeOfGarbage(GarbageType.Glass,50,1),1)
),
};
collector = new GarbageCollector(new Coords(0,0), containers, size, size);
stepN = 0;
@ -77,10 +82,6 @@ namespace CzokoŚmieciarka.MonoGameView
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
road1 = Content.Load<Texture2D>("road1");
road2 = Content.Load<Texture2D>("road2");
grass = Content.Load<Texture2D>("grass");
house = Content.Load<Texture2D>("house");
// TODO: use this.Content to load your game content here
}

View File

@ -65,7 +65,8 @@
<Compile Include="DataModels\MapLoader.cs" />
<Compile Include="DataModels\Models\Coords.cs" />
<Compile Include="DataModels\Models\Dump.cs" />
<Compile Include="DataModels\Models\Garbage.cs" />
<Compile Include="DataModels\Models\BasicGarbage.cs" />
<Compile Include="DataModels\Models\EmptyHouse.cs" />
<Compile Include="DataModels\Models\GarbageCollectorContainer.cs" />
<Compile Include="DataModels\Models\GarbageLocalization.cs" />
<Compile Include="DataModels\Models\Grass.cs" />

BIN
Trunk/MonoGameView/map.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB