Pathfinding

by @Pattentrick

So you want to do pathfinding, eh? Okay, but before we start we should cover some basics. First of, pathfinding works best if the entity is <= 3x the tilesize. Also, if you are using a pathfinding map, it must match the tilesize of your collision map. You don't have to require the pathfinding module manually. If you are using an entity that extends a character abstractity, like creatures or the player, pathfinding is already built in.

The moveTo method

To use pathfinding, just call the moveTo method on the entity, which should move it to another position or entity, like this:

this.moveTo({
    x: 200,
    y: 10
}, {
    avoidEntities: false
});

The moveTo method expects two object literals as parameters. The first one tells the entity where to go, based on x and y coordinates. You could also use a reference to another entity as destination, instead of x and y coordinates, if you want to. The second one is optional and allows us to declare some settings for pathfinding. In the example above I decided to avoid other entities.

Pathfinding maps

But what if your entity should avoid certain tiles, or prefer some others? For that case there are pathfinding maps. Open the Weltmeister level editor and add a new layer called 'pathfinding' to your level. As stated before, make sure that it has the same tilesize as the collision map.

pathfindinglayer

There are some tilesets in the media folder that came with impact++ called something like "pathfindingtiles_plusplus_8". The number at the end of the file name represents the tilesize. So if the tilesize of your collison map is 8, select "pathfindingtiles_plusplus_8" for example. Once you selected the pathfinding tileset and you clicked on the "Apply Changes" button, hitting space will now bring up your new pathfinding tiles. You are now ready to paint some awesome pathfinding tiles!

tiles

The idea behind those tiles is rather simple. The colours represent how likely your entity will use a tile, where light green ones are the most preferred and the red one will be avoided.

That's it! As complicated as pathfinding sounds, it's just about calling the moveTo method and painting some pathfinding tiles to your pathfinding map. Impact ++ will take care of the rest.

Learn more!

The moveTo method
Creature Class
Character Class
Config file