Class GoalMap
Implementation of a goal map system, also known as Dijkstra maps, based on this article
Inherited Members
Namespace: GoRogue.Pathing
Assembly: GoRogue.dll
Syntax
public class GoalMap : GridViewBase<double?>, IGridView<double?>
Remarks
This class encapsulates the work of building a goal map from your map level. You provide the constructor with a map view representing the map as GoalState values, and GoalMap will compute the goal map for the level. When the underlying circumstances of the level change, the GoalMap instance will need to be updated. Call Update() if obstacles have changed, or UpdatePathsOnly() if the goals have changed but not the obstacles. This class exposes the resulting goal map to you via indexers -- GoalMap implements IGridView<T>, where null indicates a square is an obstacle, and any other value indicates distance from the nearest goal. Thus, a value of 0 indicates a tile contains a goal. For items following the GoalMap, they can simply call GetDirectionOfMinValue(Point)
Constructors
View SourceGoalMap(IGridView<GoalState>, Distance)
Constructor. Takes a base map and a distance measurement to use for calculation.
Declaration
public GoalMap(IGridView<GoalState> baseMap, Distance distanceMeasurement)
Parameters
Type | Name | Description |
---|---|---|
IGridView<GoalState> | baseMap | A map view that represents the map as an IGridView<T> of GoalState. Must not change width/height after the goal map has been constructed. |
Distance | distanceMeasurement | The distance measurement (and implicitly the AdjacencyRule) to use for calculation. |
Fields
View SourceDistanceMeasurement
The distance measurement the GoalMap is using to calculate distance.
Declaration
public readonly Distance DistanceMeasurement
Field Value
Type | Description |
---|---|
Distance |
Properties
View SourceBaseMap
The map view of the underlying map used to determine where obstacles/goals are. Must not change width or height after the GoalMap has been constructed.
Declaration
public IGridView<GoalState> BaseMap { get; }
Property Value
Type | Description |
---|---|
IGridView<GoalState> |
Height
Height of the goal map.
Declaration
public override int Height { get; }
Property Value
Type | Description |
---|---|
int |
Overrides
this[Point]
Returns the goal-map value for the given position.
Declaration
public override double? this[Point pos] { get; }
Parameters
Type | Name | Description |
---|---|---|
Point | pos | The position to return the goal-map value for. |
Property Value
Type | Description |
---|---|
double? | The goal-map value for the given position. |
Overrides
Width
Width of the goal map.
Declaration
public override int Width { get; }
Property Value
Type | Description |
---|---|
int |
Overrides
Methods
View SourceGetDirectionOfMinValue(Point)
Gets the direction of the neighbor with the minimum goal-map value from the given position.
Declaration
public Direction GetDirectionOfMinValue(Point position)
Parameters
Type | Name | Description |
---|---|---|
Point | position | The position to get the minimum value for. |
Returns
Type | Description |
---|---|
Direction | The direction that has the minimum value in the goal-map, or None if the neighbors are all obstacles. |
GetDirectionOfMinValue(int, int)
Gets the direction of the neighbor with the minimum goal-map value from the given position.
Declaration
public Direction GetDirectionOfMinValue(int positionX, int positionY)
Parameters
Type | Name | Description |
---|---|---|
int | positionX | The x-value of the position to get the minimum value for. |
int | positionY | The y-value of the position to get the minimum value for. |
Returns
Type | Description |
---|---|
Direction | The direction that has the minimum value in the goal-map, or None if the neighbors are all obstacles. |
ToString()
Returns the goal-map values represented as a 2D grid-style string.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A string representing the goal map values. |
Overrides
View SourceToString(int)
Returns the goal-map values represented as a 2D-grid-style string, with the given field size.
Declaration
public string ToString(int fieldSize)
Parameters
Type | Name | Description |
---|---|---|
int | fieldSize | Number of characters allocated to each value in the string. |
Returns
Type | Description |
---|---|
string | A string representing the goal-map values. |
ToString(int, string)
Returns the goal-map values represented as a 2D-grid-style string, with the given field size, and any non-null values formatted using the given format string.
Declaration
public string ToString(int fieldSize, string formatString)
Parameters
Type | Name | Description |
---|---|---|
int | fieldSize | Number of characters allocated to each value in the string. |
string | formatString | Format string to use for non-null values. |
Returns
Type | Description |
---|---|
string | A string representing the goal-map values. |
ToString(string)
Returns the goal-map values represented as a 2D-grid-style string, where any value that isn't null is formatted as per the specified format string.
Declaration
public string ToString(string formatString)
Parameters
Type | Name | Description |
---|---|---|
string | formatString | Format string to use for non-null values. |
Returns
Type | Description |
---|---|
string | A string representing the goal-map values. |
Update()
Re-evaluates the entire goal map. Should be called when obstacles change. If the obstacles have not changed but the goals have, call UpdatePathsOnly() for better efficiency.
Declaration
public bool Update()
Returns
Type | Description |
---|---|
bool | False if no goals were produced by the evaluator, true otherwise |
UpdatePathsOnly()
Re-evaluates the walkable portion of the goal map. Should be called anytime the goals change but the obstacles haven't. If the obstacles have also changed, call Update() instead.
Declaration
public bool UpdatePathsOnly()
Returns
Type | Description |
---|---|
bool | False if no goals were produced by the evaluator, true otherwise |
Events
View SourceUpdated
Triggers when the GoalMap is updated.
Declaration
public event Action Updated
Event Type
Type | Description |
---|---|
Action |