Libraryaufruf in C#

This commit is contained in:
2014-05-06 11:15:32 +02:00
parent 992e523f0a
commit ba6b6c20f7
2 changed files with 47 additions and 0 deletions

46
src/Pathfinder.cs Normal file
View File

@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace WorldOfPeacecraft
{
class Pathfinder
{
private struct PathNode
{
[MarshalAs(UnmanagedType.U4)]
public uint X;
[MarshalAs(UnmanagedType.U4)]
public uint Y;
}
public static LinkedList<Coordinate> FindPath (uint posFrom, uint posTo, Map map)
{
Tile[,] tiles = map.GetTiles ();
uint mapWidth = tiles.GetLength (0);
uint mapHeight = tiles.GetLength (1);
bool[] boolMap = new bool[mapHeight * mapWidth];
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; y++) {
boolMap [x + y * mapWidth] = tiles [x, y].IsWalkable ();
}
}
PathNode[] result = new PathNode[mapHeight * mapWidth];
int noSteps = findPath (posFrom, posTo, mapWidth, mapHeight, boolMap, result);
LinkedList<Coordinate> mappedResult = new LinkedList<Coordinate>();
for (int i = 0; i < noSteps; i++) {
mappedResult.AddLast(new Coordinate(result[i].X, result[i].Y));
}
return mappedResult;
}
[DllImport("pathfinding.dll")]
[return: MarshalAs(UnmanagedType.U4)]
private static extern uint findPath(
[MarshalAs(UnmanagedType.U4)] uint posFrom,
[MarshalAs(UnmanagedType.U4)] uint posTo,
[MarshalAs(UnmanagedType.U4)] uint mapWidth,
[MarshalAs(UnmanagedType.U4)] uint mapHeight,
[MarshalAs(UnmanagedType.LPArray)] bool[] map,
[MarshalAs(UnmanagedType.LPArray)] PathNode[] result);
}
}