First things first:
We'll need a world to 'path find' our way around. Lets start off with defining some simple utility functions. Below is my Tools module that contains functions to:
- Square ints to a float
- Remove items from lists
- Load ints from a file (2D arrays of numbers will serve as our tile maps, collision maps, monster & item locations, etc)
The map:
Ok we still need to describe the world we want to navigate: In our world a 0 represents an open space and a 1 represents an area we can not enter. For example a 4 unit long corridor, running left to right, would look like this:
* Wall 1 1 1 1
* Void 0 0 0 0
* Wall 1 1 1 1
Here is my level module that describes:
- Map point: an x,y location in our 2D space, plus the value of the tile at that location and a function to calculate distance from another map point
- Map: a list of map points and a function to get the neighboring tiles of any given tile
Level 1: Now let's create a file to represent our world. Open up a text editor and and create a delimited 2D array of 0's and 1's with a definite path between 2 points. like so:
* 0; 0; 0; 0; 0;
* 0; 0; 0; 1; 0;
* 0; 0; 0; 1; 0;
* 0; 1; 1; 1; 1;
* 0; 0; 0; 0; 0;
* 1; 1; 1; 1; 0;
* 0; 0; 0; 0; 0;
* 1; 0; 1; 1; 1;
* 0; 0; 0; 0; 0;
(I've highlighted my start tile green and end tile red for reference)
...Save this text file some where on your hard drive for later.
The Results:
The code bellow shows the pathfinding in action:
No comments:
Post a Comment