From cbb3a5aa4e196881b6d11b23a3e409102afdceec Mon Sep 17 00:00:00 2001 From: Simon Holmberg Date: Sat, 29 Sep 2012 01:45:18 +0200 Subject: [PATCH] Pathfinder library didn't work as I expected; is now instanced once for every PathFinder component. Now with selection and move orders by right clicking the map! --- .../DepthsBelow/Component/PathFinder.cs | 21 +++++++++++++++---- DepthsBelow/DepthsBelow/Core.cs | 12 +++++------ DepthsBelow/DepthsBelow/Map.cs | 8 +++---- DepthsBelow/DepthsBelow/MouseInput.cs | 8 +++++++ DepthsBelow/DepthsBelow/Soldier.cs | 5 ++--- .../DepthsBelowContent/maps/Cave.Level1.tmx | 6 +++--- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/DepthsBelow/DepthsBelow/Component/PathFinder.cs b/DepthsBelow/DepthsBelow/Component/PathFinder.cs index b51f4a8..072d55b 100755 --- a/DepthsBelow/DepthsBelow/Component/PathFinder.cs +++ b/DepthsBelow/DepthsBelow/Component/PathFinder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using Microsoft.Xna.Framework; @@ -13,7 +14,7 @@ namespace DepthsBelow.Component { get { return goal; } set - { + { this.goal = value; this.Start = this.Parent.GetComponent().Position; this.FindPath(this.Start, value); @@ -21,7 +22,7 @@ namespace DepthsBelow.Component } private Point goal; - private List path; + private List path; public PathFinder(Entity parent) : base(parent) @@ -45,11 +46,23 @@ namespace DepthsBelow.Component public void FindPath(Point start, Point goal) { + AStar.PathFinderFast pathFinder = new AStar.PathFinderFast(Core.Map.GetCollisionMap()); + pathFinder.Formula = AStar.HeuristicFormula.Manhattan; + pathFinder.Diagonals = false; + pathFinder.HeavyDiagonals = false; + pathFinder.HeuristicEstimate = 2; + pathFinder.PunishChangeDirection = true; + pathFinder.TieBreaker = false; + pathFinder.SearchLimit = 50000; + pathFinder.DebugProgress = false; + pathFinder.DebugFoundPath = false; + var _start = new System.Drawing.Point(start.X, start.Y); var _goal = new System.Drawing.Point(goal.X, goal.Y); - path = Core.PathFinder.FindPath(_start, _goal); + //path = Core.PathFinder.FindPath(_start, _goal); + path = pathFinder.FindPath(_start, _goal); if (path == null) - Console.WriteLine("Path not found!"); + Debug.WriteLine(this.Parent.ToString() + ": Path not found!"); } } } diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 0b4f558..4773f88 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -25,7 +25,7 @@ namespace DepthsBelow MouseInput mouseInput; //public Soldier soldier; public List Squad; - private Map map; + public static Map Map; public Core() { @@ -36,7 +36,7 @@ namespace DepthsBelow graphics.PreferredBackBufferHeight = 720; graphics.IsFullScreen = false; - graphics.SynchronizeWithVerticalRetrace = false; + //graphics.SynchronizeWithVerticalRetrace = false; this.IsFixedTimeStep = false; graphics.ApplyChanges(); @@ -67,9 +67,9 @@ namespace DepthsBelow // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); - map = Content.Load("maps/Cave.Level1"); + Map = Content.Load("maps/Cave.Level1"); - PathFinder = new AStar.PathFinderFast(map.GetCollisionMap()); + PathFinder = new AStar.PathFinderFast(Map.GetCollisionMap()); PathFinder.Formula = AStar.HeuristicFormula.Manhattan; PathFinder.Diagonals = false; PathFinder.HeavyDiagonals = false; @@ -80,7 +80,7 @@ namespace DepthsBelow PathFinder.DebugProgress = false; PathFinder.DebugFoundPath = false; - map.ParseObjects(this); + Map.ParseObjects(this); // TODO: use this.Content to load your game content here Soldier.LoadContent(this); @@ -134,7 +134,7 @@ namespace DepthsBelow Camera.Transform); // Draw the level - map.Draw(spriteBatch); + Map.Draw(spriteBatch); // Draw units // TODO: Foreach etc... foreach (var soldier in Squad) diff --git a/DepthsBelow/DepthsBelow/Map.cs b/DepthsBelow/DepthsBelow/Map.cs index 62ae2d4..1103769 100755 --- a/DepthsBelow/DepthsBelow/Map.cs +++ b/DepthsBelow/DepthsBelow/Map.cs @@ -69,17 +69,15 @@ namespace DepthsBelow foreach (var mapObject in objectLayer.Objects) { - Console.WriteLine(mapObject.Type); - Console.WriteLine(mapObject.Bounds.Left + "," + mapObject.Bounds.Top); - if (mapObject.Type == "SquadStart") { var soldier = new Soldier(core); - var mapObjectPos = new Vector2(mapObject.Bounds.Left, mapObject.Bounds.Top); + // HACK: For some reason, the tile object coordinates are offset by one tile on the Y-axis in the Tiled map file (https://github.com/bjorn/tiled/issues/91) + var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize); var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos); soldier.X = mapObjectGridPos.X; soldier.Y = mapObjectGridPos.Y; - + core.Squad.Add(soldier); } } diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 1aa25f2..db5ef78 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -76,6 +76,14 @@ namespace DepthsBelow selectionRectangle = Rectangle.Empty; } + // Send orders with right click + if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) + { + foreach (var unit in core.Squad) + if (unit.Selected) + unit.GetComponent().Goal = Grid.WorldToGrid(mouseWorldPos); + } + // Grid highlighting Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos); Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos); diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index b3c2286..bda0099 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -21,8 +21,8 @@ namespace DepthsBelow LoadContent(core); pixelTransform.Origin = new Vector2(16, 16); - gridTransform.Position = new Point(7, 3); - pixelTransform.Position = gridTransform.ToWorld(); + //gridTransform.Position = new Point(7, 3); + //pixelTransform.Position = gridTransform.ToWorld(); this.Color = Color.White; var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White}; @@ -32,7 +32,6 @@ namespace DepthsBelow AddComponent(cc); var pfc = new PathFinder(this); - pfc.Goal = new Point(8, 39); AddComponent(pfc); } diff --git a/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx b/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx index b2db441..a4d2fd8 100755 --- a/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx +++ b/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx @@ -215,10 +215,10 @@ - + - - + +