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:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
@@ -45,11 +46,23 @@ namespace DepthsBelow.Component
|
|||||||
|
|
||||||
public void FindPath(Point start, Point goal)
|
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 _start = new System.Drawing.Point(start.X, start.Y);
|
||||||
var _goal = new System.Drawing.Point(goal.X, goal.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)
|
if (path == null)
|
||||||
Console.WriteLine("Path not found!");
|
Debug.WriteLine(this.Parent.ToString() + ": Path not found!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ namespace DepthsBelow
|
|||||||
MouseInput mouseInput;
|
MouseInput mouseInput;
|
||||||
//public Soldier soldier;
|
//public Soldier soldier;
|
||||||
public List<Soldier> Squad;
|
public List<Soldier> Squad;
|
||||||
private Map map;
|
public static Map Map;
|
||||||
|
|
||||||
public Core()
|
public Core()
|
||||||
{
|
{
|
||||||
@@ -36,7 +36,7 @@ namespace DepthsBelow
|
|||||||
graphics.PreferredBackBufferHeight = 720;
|
graphics.PreferredBackBufferHeight = 720;
|
||||||
graphics.IsFullScreen = false;
|
graphics.IsFullScreen = false;
|
||||||
|
|
||||||
graphics.SynchronizeWithVerticalRetrace = false;
|
//graphics.SynchronizeWithVerticalRetrace = false;
|
||||||
this.IsFixedTimeStep = false;
|
this.IsFixedTimeStep = false;
|
||||||
graphics.ApplyChanges();
|
graphics.ApplyChanges();
|
||||||
|
|
||||||
@@ -67,9 +67,9 @@ namespace DepthsBelow
|
|||||||
// Create a new SpriteBatch, which can be used to draw textures.
|
// Create a new SpriteBatch, which can be used to draw textures.
|
||||||
spriteBatch = new SpriteBatch(GraphicsDevice);
|
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.Formula = AStar.HeuristicFormula.Manhattan;
|
||||||
PathFinder.Diagonals = false;
|
PathFinder.Diagonals = false;
|
||||||
PathFinder.HeavyDiagonals = false;
|
PathFinder.HeavyDiagonals = false;
|
||||||
@@ -80,7 +80,7 @@ namespace DepthsBelow
|
|||||||
PathFinder.DebugProgress = false;
|
PathFinder.DebugProgress = false;
|
||||||
PathFinder.DebugFoundPath = false;
|
PathFinder.DebugFoundPath = false;
|
||||||
|
|
||||||
map.ParseObjects(this);
|
Map.ParseObjects(this);
|
||||||
|
|
||||||
// TODO: use this.Content to load your game content here
|
// TODO: use this.Content to load your game content here
|
||||||
Soldier.LoadContent(this);
|
Soldier.LoadContent(this);
|
||||||
@@ -134,7 +134,7 @@ namespace DepthsBelow
|
|||||||
Camera.Transform);
|
Camera.Transform);
|
||||||
|
|
||||||
// Draw the level
|
// Draw the level
|
||||||
map.Draw(spriteBatch);
|
Map.Draw(spriteBatch);
|
||||||
// Draw units
|
// Draw units
|
||||||
// TODO: Foreach etc...
|
// TODO: Foreach etc...
|
||||||
foreach (var soldier in Squad)
|
foreach (var soldier in Squad)
|
||||||
|
|||||||
@@ -69,13 +69,11 @@ namespace DepthsBelow
|
|||||||
|
|
||||||
foreach (var mapObject in objectLayer.Objects)
|
foreach (var mapObject in objectLayer.Objects)
|
||||||
{
|
{
|
||||||
Console.WriteLine(mapObject.Type);
|
|
||||||
Console.WriteLine(mapObject.Bounds.Left + "," + mapObject.Bounds.Top);
|
|
||||||
|
|
||||||
if (mapObject.Type == "SquadStart")
|
if (mapObject.Type == "SquadStart")
|
||||||
{
|
{
|
||||||
var soldier = new Soldier(core);
|
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);
|
var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
|
||||||
soldier.X = mapObjectGridPos.X;
|
soldier.X = mapObjectGridPos.X;
|
||||||
soldier.Y = mapObjectGridPos.Y;
|
soldier.Y = mapObjectGridPos.Y;
|
||||||
|
|||||||
@@ -76,6 +76,14 @@ namespace DepthsBelow
|
|||||||
selectionRectangle = Rectangle.Empty;
|
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
|
// Grid highlighting
|
||||||
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
|
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
|
||||||
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
|
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ namespace DepthsBelow
|
|||||||
LoadContent(core);
|
LoadContent(core);
|
||||||
|
|
||||||
pixelTransform.Origin = new Vector2(16, 16);
|
pixelTransform.Origin = new Vector2(16, 16);
|
||||||
gridTransform.Position = new Point(7, 3);
|
//gridTransform.Position = new Point(7, 3);
|
||||||
pixelTransform.Position = gridTransform.ToWorld();
|
//pixelTransform.Position = gridTransform.ToWorld();
|
||||||
|
|
||||||
this.Color = Color.White;
|
this.Color = Color.White;
|
||||||
var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White};
|
var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White};
|
||||||
@@ -32,7 +32,6 @@ namespace DepthsBelow
|
|||||||
AddComponent(cc);
|
AddComponent(cc);
|
||||||
|
|
||||||
var pfc = new PathFinder(this);
|
var pfc = new PathFinder(this);
|
||||||
pfc.Goal = new Point(8, 39);
|
|
||||||
AddComponent(pfc);
|
AddComponent(pfc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -215,10 +215,10 @@
|
|||||||
</data>
|
</data>
|
||||||
</layer>
|
</layer>
|
||||||
<objectgroup name="Object Layer 1" width="100" height="100">
|
<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="Spawn2" type="SquadStart" gid="143" x="288" y="192"/>
|
||||||
<object name="Spawn4" type="SquadStart" gid="143" x="224" y="160"/>
|
<object name="Spawn4" type="SquadStart" gid="143" x="224" y="160"/>
|
||||||
<object name="Spawn3" type="SquadStart" gid="143" x="224" y="224"/>
|
<object name="Spawn3" type="SquadStart" gid="143" x="256" y="288"/>
|
||||||
<object name="Spawn5" type="SquadStart" gid="143" x="160" y="192"/>
|
<object name="Spawn5" type="SquadStart" gid="143" x="224" y="224"/>
|
||||||
</objectgroup>
|
</objectgroup>
|
||||||
</map>
|
</map>
|
||||||
|
|||||||
Reference in New Issue
Block a user