I changed the name to 'Simple Enemy AI'. I think that is more appropriate.
A behavior with 6 different modes for chasing or following a target. It based on the 4 different Ghosts of PacMan, so it's a very simple AI. There are only a few things you can adjust.
Enable or Disable the behavior.
There are no stop or move actions, you have to use 'set enable' as an action.
How many pixels in one second the object moves.
This is only an action. Normally the sprite (enemy) would never move backwards. The only exception is if it stuck on a dead end. This should avoid that the enemy is jitter from one direction to another.
With reset direction you can force to recalculate the directions inclusive backwards. This can be helpful if your enemy should flee for something (like the PacMan Ghost when PacMan gets a pill). It could be very bad if the enemy have to move to the next cross-way , before change the direction.
The direction the sprite is moving at this moment.
1= up; 2=right; 3=down; 4=left;
Helpful for the animation of the sprite.
getpx, getpy are for debug ignore them.
For each chasing modes you have to set different links to other objects. This are all actions that you should put in 'On start of Layout'.
All need a Tilemap with a path of tiles to follow. It's up to you how the path looks and the size of the tiles, but this has a big influence of how good the AI works. The tiles have not to be transparent or on the top layer. You can also put the layer as bottom layer. There is no need that this layer is visible at runtime. If you put at at the bottom this could have a positive effect to the performance if you have a really high resolution and a big tilemap.
The other objects are all sprites:
Target - The one that should be attacked.
Hideoutt - It's like the second Target, but more for retreat.
Cohunter - It's only needed for the encircling mode, best is to use a sprite with the follow mode.
I recommend to use not an instance of the main enemy sprite but rather a new sprite-object.
It will only choose the first instance of objects, so it could be possible that the cohunter is the same instance like the main hunter (enemy).
The modes are:
retreat and follow
This are practical the same.The enemy follows direct their target. The difference is that you can give each mode a different target, then you can switch targets at runtime. You could do the same if you switch the target in one of the modes at runtime. It's up to you how you do that.
A Tilemap and a Target.
Tries to get in front of the target.
A Tilemap and a Target.
Needs a second sprite as a 2nd hunter and tries to encircle the target.
A Tilemap, a Target and CoHunter.
Will follow the target, but if it get too near (6 tiles in every direction) it will flee to his second target.
A Tilemap, a Target and hideout.
The opposite of 'shy away'. Guards a target by circling around it. If an sprite comes too near (6 tiles in every direction) it will attack this sprite as long as the sprite is in 6 tiles range.
This behavior allows only 90° turns.
If you forget to link a needed object for the mode the sprite won't move.
This is a first beta. It's highly possible that this behavior causes crashes. So be very careful and don't use it in big projects. It's more for testing at the moment.
In the example you can see how to use the behavior.
Put the behavior (only 'simpleenemyai') in: Program Files\Construct 2\exporters\html5\behaviors
A behavior with the 4 AIs of the 4 different Ghosts of PacMan: Blinky (red), Pinky(pink), Inky(light blue/turquois), and Clyde (brown).
It uses a tilemap as path for chasing a target. Each Ghost has a different way to chase his target.
Chases his target direct.
Tries to perform an ambush.
Inky uses Blinky to encircle his target.
Chases his target like Blinky. If he comes to close to his targets, he flees.
This based all on the work of Toru Iwatani in 1980. It's not really an AI. All Ghost have more or less a simple behavior to chase PacMan. So it looked more like chaos than really an intelligent chase. The advantage is the very low cpu usage.
A made a little demo to show how the ghost are chasing PacMan. PacMan hasn't really an intelligent behavior. This will only show how the ghost chases. So no collision detection is implemented.
At the moment it's in a very early stage, so no download link. If is there any interest in this behavior I will made a usable version.