From 9668e23646c3ca62b20bb67e34e3e5a524bc944b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dulski?= Date: Mon, 22 Apr 2019 14:17:44 +0200 Subject: [PATCH] =?UTF-8?q?=C5=82o=20panie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Trunk/MonoGameView/Algorithms/DFS.cs | 22 +++----- .../GarbageCollector/AGarbageCollector.cs | 7 ++- .../DataModels/Interfaces/IDrawables.cs | 5 +- Trunk/MonoGameView/DataModels/MapLoader.cs | 55 +++++++++++++++++++ .../DataModels/Models/GarbageCollector.cs | 19 ++----- Trunk/MonoGameView/DataModels/Models/Grass.cs | 28 ++++++++++ Trunk/MonoGameView/DataModels/Models/House.cs | 15 ++++- Trunk/MonoGameView/DataModels/Models/Road1.cs | 14 +++++ Trunk/MonoGameView/Game1.cs | 28 +++------- Trunk/MonoGameView/MonoGameView.csproj | 2 + 10 files changed, 145 insertions(+), 50 deletions(-) create mode 100644 Trunk/MonoGameView/DataModels/MapLoader.cs create mode 100644 Trunk/MonoGameView/DataModels/Models/Grass.cs diff --git a/Trunk/MonoGameView/Algorithms/DFS.cs b/Trunk/MonoGameView/Algorithms/DFS.cs index 79e5766..a7cf1a7 100644 --- a/Trunk/MonoGameView/Algorithms/DFS.cs +++ b/Trunk/MonoGameView/Algorithms/DFS.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Xna.Framework.Content; namespace CzokoŚmieciarka.MonoGameView.Algorithms { @@ -20,14 +21,14 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms int count = 0; - public List BestPath(AGarbageCollector collector, object[,] grid) + public List BestPath(ContentManager content, AGarbageCollector collector, object[,] grid) { - var r=Search(collector, grid, 0).Key; + var r=Search(content, collector, grid, 0).Key; Console.WriteLine(count); return r; } - List PossibleSteps(AGarbageCollector collector, object[,] grid) + List PossibleSteps(ContentManager content, AGarbageCollector collector, object[,] grid) { @@ -42,7 +43,7 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms var filteredMoveSteps = new List(); foreach (var item in moveSteps) { - var copyCollector = (AGarbageCollector)collector.Clone(); + var copyCollector = (AGarbageCollector)collector.Clone(content); var copyGrid = (object[,])grid.Clone(); try { @@ -63,26 +64,21 @@ namespace CzokoŚmieciarka.MonoGameView.Algorithms return result; } - KeyValuePair, int> Search(AGarbageCollector collector, object[,] grid, int length) + KeyValuePair, int> Search(ContentManager content, AGarbageCollector collector, object[,] grid, int length) { count++; if (length > 40) return new KeyValuePair, int>(new List(), length); - var possibleSteps = PossibleSteps(collector, grid); + var possibleSteps = PossibleSteps(content, collector, grid); foreach (var item in possibleSteps) { - var copyCollector = (AGarbageCollector)collector.Clone(); + var copyCollector = (AGarbageCollector)collector.Clone(content); var copyGrid = (object[,])grid.Clone(); - - - - - //if (copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] is IRoad1) copyGrid[copyCollector.Coords.X, copyCollector.Coords.Y] = new Road2(); item.Invoke(copyCollector, copyGrid); - var s = Search(copyCollector, copyGrid, length + 1); + var s = Search(content, copyCollector, copyGrid, length + 1); if (s.Key != null) { s.Key.Insert(0, item); diff --git a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs index 87c1de3..4c0d953 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/GarbageCollector/AGarbageCollector.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using CzokoŚmieciarka.MonoGameView.DataModels.Exceptions; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.TrashCans; using CzokoŚmieciarka.MonoGameView.DataModels.Models; +using Microsoft.Xna.Framework.Content; namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector { @@ -57,11 +58,15 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces.GarbageCollector Coords.X += 1; } - public virtual object Clone() + public virtual object Clone(ContentManager content) { throw new NotImplementedException(); } public IEnumerable TrashContainers { get; } + public object Clone() + { + throw new NotImplementedException(); + } } } diff --git a/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs b/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs index 9d4bb4a..b212af0 100644 --- a/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs +++ b/Trunk/MonoGameView/DataModels/Interfaces/IDrawables.cs @@ -1,9 +1,10 @@ -using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; namespace CzokoŚmieciarka.MonoGameView.DataModels.Interfaces { public interface IDrawables { - void Draw(SpriteBatch spriteBatch, int size); + void Draw(ContentManager content, SpriteBatch spriteBatch, int size); } } diff --git a/Trunk/MonoGameView/DataModels/MapLoader.cs b/Trunk/MonoGameView/DataModels/MapLoader.cs new file mode 100644 index 0000000..77c61eb --- /dev/null +++ b/Trunk/MonoGameView/DataModels/MapLoader.cs @@ -0,0 +1,55 @@ +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; + } + } + } + } +} diff --git a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs index c51feca..206cf26 100644 --- a/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs +++ b/Trunk/MonoGameView/DataModels/Models/GarbageCollector.cs @@ -14,27 +14,20 @@ namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { public class GarbageCollector : AGarbageCollector, IDrawables { - public Texture2D Image { get; set; } - public GarbageCollector(ContentManager content, Coords c, List l, int rows, int cols) : base(c,l,rows,cols) + public GarbageCollector(Coords c, List l, int rows, int cols) : base(c,l,rows,cols) { - Image = content.Load("collector"); } - public GarbageCollector(Texture2D image, Coords c, List l, int rows, int cols) : base(c, l, rows, cols) + + public void Draw(ContentManager content, SpriteBatch batch,int size) { - Image = image; + batch.Draw(content.Load("collector"), new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White); } - public void Draw(SpriteBatch batch,int size) - { - batch.Draw(Image, new Rectangle(Coords.X*500/size, Coords.Y*500/size, 500/size, 500/size), Color.White); - } - - - public override object Clone() + public override object Clone(ContentManager content) { var cloneList = new List(); - return new GarbageCollector(Image, new Coords(Coords.X,Coords.Y), cloneList, rows,columns); + return new GarbageCollector(new Coords(Coords.X,Coords.Y), cloneList, rows,columns); } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Grass.cs b/Trunk/MonoGameView/DataModels/Models/Grass.cs new file mode 100644 index 0000000..d450ab9 --- /dev/null +++ b/Trunk/MonoGameView/DataModels/Models/Grass.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using CzokoŚmieciarka.MonoGameView.DataModels.Models; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGameView.DataModels.Models +{ + public class Grass : IDrawables + { + private Coords Coords; + + public Grass(Coords coords) + { + Coords = coords; + } + + public void Draw(ContentManager content, SpriteBatch batch, int size) + { + batch.Draw(content.Load("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); + } + } +} diff --git a/Trunk/MonoGameView/DataModels/Models/House.cs b/Trunk/MonoGameView/DataModels/Models/House.cs index 4bd9368..66fe936 100644 --- a/Trunk/MonoGameView/DataModels/Models/House.cs +++ b/Trunk/MonoGameView/DataModels/Models/House.cs @@ -4,10 +4,23 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using CzokoŚmieciarka.MonoGameView.DataModels.Interfaces; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { - public class House : IHouse + public class House : IDrawables { + private Coords Coords; + + public House(Coords coords) + { + Coords = coords; + } + public void Draw(ContentManager content, SpriteBatch batch, int size) + { + batch.Draw(content.Load("house"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); + } } } diff --git a/Trunk/MonoGameView/DataModels/Models/Road1.cs b/Trunk/MonoGameView/DataModels/Models/Road1.cs index 8887581..399dbec 100644 --- a/Trunk/MonoGameView/DataModels/Models/Road1.cs +++ b/Trunk/MonoGameView/DataModels/Models/Road1.cs @@ -4,10 +4,24 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; namespace CzokoŚmieciarka.MonoGameView.DataModels.Models { public class Road1 :IRoad1 { + private Coords Coords; + + public Road1(Coords coords) + { + Coords = coords; + } + + public void Draw(ContentManager content, SpriteBatch batch, int size) + { + batch.Draw(content.Load("grass"), new Rectangle(Coords.X * 500 / size, Coords.Y * 500 / size, 500 / size, 500 / size), Color.White); + } } } diff --git a/Trunk/MonoGameView/Game1.cs b/Trunk/MonoGameView/Game1.cs index 8d36e9c..1426aba 100644 --- a/Trunk/MonoGameView/Game1.cs +++ b/Trunk/MonoGameView/Game1.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; using System.Linq; using CzokoŚmieciarka.MonoGameView.Algorithms; +using MonoGameView.DataModels; namespace CzokoŚmieciarka.MonoGameView { @@ -17,20 +18,21 @@ namespace CzokoŚmieciarka.MonoGameView /// public class Game1 : Game { - private static int size = 20; + 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; int stepN; List steps; GarbageCollector collector; - object[,] grid = new object[size, size]; + object[,] grid; public Game1() { graphics = new GraphicsDeviceManager(this); @@ -52,23 +54,14 @@ namespace CzokoŚmieciarka.MonoGameView // TODO: Add your initialization logic here roadPos = new Vector2(0, 0); timer = 0.2f; - for (int x=0;x(), size, size); + mapLoader.Load(out size,out grid,"map.xml"); + collector = new GarbageCollector(new Coords(0, 0), new List(), size, size); stepN = 0; var dfs = new DFS(); - steps = dfs.BestPath(collector, grid); + steps = dfs.BestPath(Content, collector, grid); base.Initialize(); } @@ -119,11 +112,6 @@ namespace CzokoŚmieciarka.MonoGameView } } // TODO: Add your update logic here - - - - - base.Update(gameTime); } @@ -147,7 +135,7 @@ namespace CzokoŚmieciarka.MonoGameView else spriteBatch.Draw(grass, new Rectangle(x * 500 / size, y * 500 / size, 500 / size, 500 / size), Color.White); } } - collector.Draw(spriteBatch, size); + collector.Draw(Content, spriteBatch, size); spriteBatch.End(); // TODO: Add your drawing code here diff --git a/Trunk/MonoGameView/MonoGameView.csproj b/Trunk/MonoGameView/MonoGameView.csproj index 944c79e..3c34106 100644 --- a/Trunk/MonoGameView/MonoGameView.csproj +++ b/Trunk/MonoGameView/MonoGameView.csproj @@ -62,11 +62,13 @@ + +