Skip to content

Instantly share code, notes, and snippets.

@jdoig
Created July 29, 2011 09:57
Show Gist options
  • Save jdoig/1113540 to your computer and use it in GitHub Desktop.
Save jdoig/1113540 to your computer and use it in GitHub Desktop.
2D tilemap Level for pathfinding code
module Level
open Tools
type MapPoint =
{x:int;y:int;value:int} (* a point in 2d map space *)
(*Calculate distance to other map point*)
member this.Distance mp = sqrt (sqr(this.x+mp.x) + sqr(this.y+mp.y))
(*Simple construct to hold the 2D map data*)
type Map =
(* Width & Height of map and map data in 1D array *)
{width:int; height:int; map:int list}
(* function to wrap 1D array into 2D array to retrive map point *)
member this.GetElement x y = {x=x;y=y;value=this.map.[x % this.height + y * this.width]}
(* return list of map points that surround current map point *)
member this.GetNeighborsOf p =
[for y in p.y-1..p.y+1 do
for x in p.x-1..p.x+1 do
if ((y<>p.y || x <>p.x) (*bounds checking *)
&& y>=0 && x>=0
&& x<this.width
&& y<this.height)
then yield this.GetElement x y]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment