So I use Slick2D and I am making a game. It has a TiledMap and entities (as any other game) and I want a way to use A*. I don’t really know how to use it because I can’t find an explanation.
Just for those who don’t use Slick, it already has AStarPathFinding and TiledMap classes which I use.
Here is a simple example of how the A-star path finding in Slick2D works. In a real game you would probably have a more realistic implementation of the
TileBasedMapinterface which actually looks up the accessibility in whatever map structure your game uses. You may also return different costs based on for example your map terrain.In your game you may also wish to make your path finding character class implement the
Moverinterface so that you can pass that as a user data object instead ofnullto thefindPathcall. This will make that object available from theblockedandcostmethods throughctx.getMover(). That way you can have some movers that ignore some, otherwise blocking, obstacles etc. (Imagine a flying character or an amphibious vehicle that can move in water or above otherwise blocking walls.) I hope this gives a basic idea.EDIT
I now noticed that you mentioned specifically that you are using the
TiledMapclass. This class does not implement theTileBasedMapinterface and cannot be directly used with the A-star implementation in Slick2D. (A Tiled map does not by default have any concept of blocking which is key when performing path finding.) Thus, you will have to implement this yourself, using your own criteria for when a tile is blocking or not and how much it should cost to traverse them.EDIT 2
There are several ways that you could define the concept of a tile being blocking. A couple of relative straight forward ways are covered below:
Separate blocking layer
In the Tiled map format you can specify multiple layers. You could dedicate one layer for your blocking tiles only and then implement the
TileBasedMapaccording to something like this:Tile property based blocking
In the Tiled map format each tile type may optionally have user defined properties. You could easily add a
blockingproperty to the tiles which are supposed to be blocking and then check for that in yourTileBasedMapimplementation. For example:Other options
There are many other options. For example, instead of having a set layer id as blocking you could set properties for the layer itself that can indicate if it is a blocking layer or not.
Additionally, all of the above examples just take blocking vs. non-blocking tiles into consideration. Of course you may have blocking and non-blocking objects on the map as well. You could also have other players or NPCs etc. that are blocking. All of this would need to be handled somehow. This should however get you started.