Action Points Movement Perfected

This commit is contained in:
Niklas Ulf Anders Cullberg
2012-10-29 16:44:59 +01:00
parent 82ad94eb4f
commit d1b3661eed
4 changed files with 786 additions and 759 deletions
+191 -183
View File
@@ -1,183 +1,191 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using AStar; using AStar;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
namespace DepthsBelow.Component namespace DepthsBelow.Component
{ {
/// <summary> /// <summary>
/// Component that handles pathfinding. /// Component that handles pathfinding.
/// </summary> /// </summary>
public class PathFinder : Component public class PathFinder : Component
{ {
/// <summary> /// <summary>
/// A pathfinder node. /// A pathfinder node.
/// </summary> /// </summary>
public class Node public class Node
{ {
/// <summary> /// <summary>
/// Grid position of the node. /// Grid position of the node.
/// </summary> /// </summary>
public Point Position; public Point Position;
/// <summary> /// <summary>
/// A* F-score /// A* F-score
/// </summary> /// </summary>
public int F; public int F;
/// <summary> /// <summary>
/// A* G-score /// A* G-score
/// </summary> /// </summary>
public int G; public int G;
/// <summary> /// <summary>
/// Explicit conversion between A* library node and Node object. /// Explicit conversion between A* library node and Node object.
/// </summary> /// </summary>
/// <param name="aStarNode"></param> /// <param name="aStarNode"></param>
/// <returns></returns> /// <returns></returns>
public static explicit operator Node(AStar.PathFinderNode aStarNode) public static explicit operator Node(AStar.PathFinderNode aStarNode)
{ {
return new Node return new Node
{ {
Position = new Point(aStarNode.X, aStarNode.Y), Position = new Point(aStarNode.X, aStarNode.Y),
F = aStarNode.F, F = aStarNode.F,
G = aStarNode.G G = aStarNode.G
}; };
} }
} }
/// <summary> /// <summary>
/// Start position of the path. /// Start position of the path.
/// </summary> /// </summary>
public Point Start { get; private set; } public Point Start { get; private set; }
/// <summary> /// <summary>
/// A collision bytemap. /// A collision bytemap.
/// 0 = unwalkable node /// 0 = unwalkable node
/// 1 = walkable node /// 1 = walkable node
/// </summary> /// </summary>
public byte[,] CollisionMap; public byte[,] CollisionMap;
private Point goal; private Point goal;
/// <summary> /// <summary>
/// Sets a goal point and finds a path to it. /// Sets a goal point and finds a path to it.
/// After a path is created, call Next() go get the next node in the path. /// After a path is created, call Next() go get the next node in the path.
/// </summary> /// </summary>
public Point Goal public Point Goal
{ {
get { return goal; } get { return goal; }
set set
{ {
this.goal = value; this.goal = value;
this.FindPath(this.Parent.GetComponent<Transform>().Grid.Position, value, null); this.FindPath(this.Parent.GetComponent<Transform>().Grid.Position, value, null);
} }
} }
/// <summary> /// <summary>
/// Is a path in progress? /// Is a path in progress?
/// </summary> /// </summary>
public bool IsMoving public bool IsMoving
{ {
get get
{ {
return path != null && path.Count() != 0; return path != null && path.Count() != 0;
} }
} }
private AStar.PathFinderFast pathFinder; public int Length
private List<AStar.PathFinderNode> path; {
get
public PathFinder(Entity parent) {
: base(parent) return (path != null) ? path.Count : 0;
{ }
}
}
private AStar.PathFinderFast pathFinder;
public override void Update(GameTime gameTime) private List<AStar.PathFinderNode> path;
{
/*if (path != null && path.Count != 0) public PathFinder(Entity parent)
{ : base(parent)
var nextNode = path.Last(); {
var nodePos = new Point(nextNode.X, nextNode.Y);
Parent.Transform.Grid.Position = nodePos; }
if (Parent.Transform.World == (Vector2)Parent.Transform.Grid)
path.Remove(nextNode); public override void Update(GameTime gameTime)
}*/ {
/*if (path != null && path.Count != 0)
base.Update(gameTime); {
} var nextNode = path.Last();
var nodePos = new Point(nextNode.X, nextNode.Y);
/// <summary> Parent.Transform.Grid.Position = nodePos;
/// Gets the next node of the current path. if (Parent.Transform.World == (Vector2)Parent.Transform.Grid)
/// </summary> path.Remove(nextNode);
/// <returns>The next node in the path.</returns> }*/
public Node Next()
{ base.Update(gameTime);
if (path == null) }
return null;
/// <summary>
if (path.Count == 0) /// Gets the next node of the current path.
return null; /// </summary>
/// <returns>The next node in the path.</returns>
var nextNode = path.Last(); public Node Next()
path.Remove(nextNode); {
return (Node)nextNode; if (path == null)
} return null;
/// <summary> if (path.Count == 0)
/// Cancels the current path in memory. return null;
/// </summary>
public void Stop() var nextNode = path.Last();
{ path.Remove(nextNode);
path = null; return (Node)nextNode;
} }
/// <summary> /// <summary>
/// Finds a path between two points. /// Cancels the current path in memory.
/// </summary> /// </summary>
/// <param name="start">The start point.</param> public void Stop()
/// <param name="goal">The goal point.</param> {
/// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the original collision map.</param> path = null;
/// <returns></returns> }
private List<AStar.PathFinderNode> FindPath(Point start, Point goal, List<Point> appendCollisionMap)
{ /// <summary>
byte[,] mapCollisionMap = Core.Map.GetCollisionMap(); /// Finds a path between two points.
/// </summary>
if (appendCollisionMap != null) /// <param name="start">The start point.</param>
foreach (var point in appendCollisionMap) /// <param name="goal">The goal point.</param>
mapCollisionMap[point.X, point.Y] = 0; /// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the original collision map.</param>
/// <returns></returns>
pathFinder = new AStar.PathFinderFast(mapCollisionMap); private List<AStar.PathFinderNode> FindPath(Point start, Point goal, List<Point> appendCollisionMap)
pathFinder.Formula = AStar.HeuristicFormula.Manhattan; {
pathFinder.Diagonals = false; byte[,] mapCollisionMap = Core.Map.GetCollisionMap();
pathFinder.HeavyDiagonals = false;
pathFinder.HeuristicEstimate = 2; if (appendCollisionMap != null)
pathFinder.PunishChangeDirection = false; foreach (var point in appendCollisionMap)
pathFinder.TieBreaker = false; mapCollisionMap[point.X, point.Y] = 0;
pathFinder.SearchLimit = 50000;
pathFinder.DebugProgress = false; pathFinder = new AStar.PathFinderFast(mapCollisionMap);
pathFinder.DebugFoundPath = false; pathFinder.Formula = AStar.HeuristicFormula.Manhattan;
pathFinder.Diagonals = false;
var _start = new System.Drawing.Point(start.X, start.Y); pathFinder.HeavyDiagonals = false;
var _goal = new System.Drawing.Point(goal.X, goal.Y); pathFinder.HeuristicEstimate = 2;
//path = Core.PathFinder.FindPath(_start, _goal); pathFinder.PunishChangeDirection = false;
path = pathFinder.FindPath(_start, _goal); pathFinder.TieBreaker = false;
if (path == null) pathFinder.SearchLimit = 50000;
Debug.WriteLine(this.Parent.ToString() + ": Path not found!"); pathFinder.DebugProgress = false;
pathFinder.DebugFoundPath = false;
//this.CollisionMap = new byte[100, 100];
var _start = new System.Drawing.Point(start.X, start.Y);
return path; var _goal = new System.Drawing.Point(goal.X, goal.Y);
} //path = Core.PathFinder.FindPath(_start, _goal);
path = pathFinder.FindPath(_start, _goal);
/// <summary> if (path == null)
/// Recreate the current path in memory with a list of unwalkable points that will be joined with the original collision map. Debug.WriteLine(this.Parent.ToString() + ": Path not found!");
/// </summary>
/// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the collision map.</param> //this.CollisionMap = new byte[100, 100];
public void RecreatePath(List<Point> appendCollisionMap)
{ return path;
FindPath(this.Parent.GetComponent<Transform>().Grid.Position, this.Goal, appendCollisionMap); }
}
} /// <summary>
} /// Recreate the current path in memory with a list of unwalkable points that will be joined with the original collision map.
/// </summary>
/// <param name="appendCollisionMap">A list of unwalkable points that will be joined with the collision map.</param>
public void RecreatePath(List<Point> appendCollisionMap)
{
FindPath(this.Parent.GetComponent<Transform>().Grid.Position, this.Goal, appendCollisionMap);
}
}
}
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -140,7 +140,7 @@ namespace DepthsBelow
{ {
foreach (var unit in core.Squad) foreach (var unit in core.Squad)
{ {
unit.AP = 0; unit.AP = unit.MaxActionPoints;
unit.Fired = false; unit.Fired = false;
} }
@@ -227,10 +227,10 @@ namespace DepthsBelow
cost = rocketCost; cost = rocketCost;
type = "Rocket"; type = "Rocket";
} }
if (soldier.AP <= soldier.NumberOfActionPoints - cost) if (soldier.AP >= cost)
{ {
core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent<Component.Stat>(), type)); core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent<Component.Stat>(), type));
soldier.AP += cost; soldier.AP -= cost;
soldier.Fired = true; soldier.Fired = true;
} }
else else
+11 -11
View File
@@ -35,16 +35,16 @@ namespace DepthsBelow
} }
} }
public int NumberOfActionPoints = 10; public int MaxActionPoints = 10;
int spentActionPoints = 0; int actionPointsLeft = 0;
public int AP public int AP
{ {
get { return spentActionPoints; } get { return actionPointsLeft; }
set set
{ {
spentActionPoints = value; actionPointsLeft = value;
GetComponent<Component.Stat>().GetStep = spentActionPoints; GetComponent<Component.Stat>().GetStep = actionPointsLeft;
} }
} }
@@ -174,17 +174,17 @@ namespace DepthsBelow
if (Transform.World == nodeWorldPos) if (Transform.World == nodeWorldPos)
{ {
if (AP < NumberOfActionPoints) //if (AP < NumberOfActionPoints)
{ //{
AP++; //AP++;
lastLastNode = lastNode; lastLastNode = lastNode;
lastNode = nextNode; lastNode = nextNode;
nextNode = GetComponent<PathFinder>().Next(); nextNode = GetComponent<PathFinder>().Next();
} //}
else /*else
{ {
GetComponent<PathFinder>().Stop(); GetComponent<PathFinder>().Stop();
} }*/
} }
} }
} }