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!
This commit is contained in:
Simon Holmberg
2012-09-29 01:45:18 +02:00
parent f694b0969c
commit cbb3a5aa4e
6 changed files with 39 additions and 21 deletions
@@ -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<GridTransform>().Position;
this.FindPath(this.Start, value);
@@ -21,7 +22,7 @@ namespace DepthsBelow.Component
}
private Point goal;
private List<AStar.PathFinderNode> path;
private List<AStar.PathFinderNode> 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!");
}
}
}
+6 -6
View File
@@ -25,7 +25,7 @@ namespace DepthsBelow
MouseInput mouseInput;
//public Soldier soldier;
public List<Soldier> 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<Map>("maps/Cave.Level1");
Map = Content.Load<Map>("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)
+3 -5
View File
@@ -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);
}
}
+8
View File
@@ -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<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
}
// Grid highlighting
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
+2 -3
View File
@@ -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);
}
@@ -215,10 +215,10 @@
</data>
</layer>
<objectgroup name="Object Layer 1" width="100" height="100">
<object name="Spawn1" type="SquadStart" gid="143" x="288" y="256"/>
<object name="Spawn1" type="SquadStart" gid="143" x="320" y="256"/>
<object name="Spawn2" type="SquadStart" gid="143" x="288" y="192"/>
<object name="Spawn4" type="SquadStart" gid="143" x="224" y="160"/>
<object name="Spawn3" type="SquadStart" gid="143" x="224" y="224"/>
<object name="Spawn5" type="SquadStart" gid="143" x="160" y="192"/>
<object name="Spawn3" type="SquadStart" gid="143" x="256" y="288"/>
<object name="Spawn5" type="SquadStart" gid="143" x="224" y="224"/>
</objectgroup>
</map>