//Check that a node can be created here private int GetTerrainCost(int x, int y) { //Check the boundary if (!mBoundary.Contains(x, y)) { return(-1); } x = Convert.ToInt32(x / mGrain) * mGrain; y = Convert.ToInt32(y / mGrain) * mGrain; TerrainNode node = (TerrainNode)mTerrain.Item(x, y); if (node == null) { return(0); } return(node.MovementCost); }
//Check the terrain between the two points private bool CheckTerrain(Point a, Point b) { if (mTerrain == null) { return(true); } int start; int end; if (a.X == b.X) { if (a.Y < b.Y) { start = a.Y; end = b.Y; } else { start = b.Y; end = a.Y; } } else { if (a.X < b.X) { start = a.X; end = b.X; } else { start = b.X; end = a.X; } } //Round the start and end onto the grid start = Convert.ToInt32(start / mGrain) * mGrain; end = Convert.ToInt32(end / mGrain) * mGrain; //Round the other coordinate of the pair int div = (a.X == b.X) ? a.X : a.Y; div = Convert.ToInt32(div / mGrain) * mGrain; //Get the starting movement cost TerrainNode node = (TerrainNode)mTerrain.Item(div, start); int cost = (node == null) ? 0 : node.MovementCost; //Loop through the terrain checking for a node with movement cost for (int i = start; i < end; i += mGrain) { node = null; if (a.X == b.X) { node = (TerrainNode)mTerrain.Item(div, i); } else { node = (TerrainNode)mTerrain.Item(i, div); } if (node != null && node.MovementCost != cost) { return(false); } } return(true); }