In my latest tutorial, I gave an introduction to Finite State Machines, and how critical they are as far as AI programming is concerned. This capx-based tutorial offers an example of a platform follower AI, with and without using an "elegant" Finite State Machine. And if you like what I'm doing, you can follow me on twitter and on facebook!
The capx comes in 2 versions:
1- The first one offers one of the most basic platform follower behavior one can make, as well as a captor-based, more elegant alternative.
2- The second capx is simply a re-factored version of the captor approach, using rex's FSM behavior plugin (see links below). I invite you to take a look at it and see how it facilitates the encapsulation or organization of your code.
Installing rex's plugins
In order to open both capx, you'll need to copy the following plugins in your construct folder:
1- The FSM behavior from rexrainbow.
2- The Platform MoveTo behavior from rexrainbow.
Here's a 3 step guide to find and install the plugins:
1- In order to download them, you have to click on the "plugin" link in yellow on Rex's website.
2- Then, unzip the plugins and copy the extracted folders to C:\Program Files\Construct 2\exporters\html5\behaviors (The disk or install folder may be different on your machine)
3- You can now open both capx.
If you want to develop elaborate games using Construct 2, or simply make your design process easier when prototyping, I highly recommend that you check all of rex's plugins. He has made a ton of extremely useful tools for you to experiment with.
"Dissecting" the system
This capx example was made after @christina's request.
The AI has a simple behavior : it targets the player's position, and jumps when it "sees" or comes across an obstacle. It only works as long as the level design is rather horizontal. There is no gap, trap or path detection (although the first two can be added easily).
The AI has 2 states: idle and follow. When it's idle, it doesn't move. It enters the follow state if the player gets too far. When it's in the following state, the AI starts moving continuously towards the player on the X axis. A little object, called captor, is projected forward on the AI's path. This captor is used to detect obstacles when the follower is moving. If it overlaps an obstacle, the AI will simply jump. I invite you to look at the code directly in order to get per-section comments. The system is rather straightforward.
An alternative to the captor would have been to use the "overlapping at offset" condition in Construct 2. I favor the captor mostly as it offers a visual debugging tool throughout development, and can be re-purposed.
Would you like me to cover something specific? Drop me a message! You can follow me and ask me questions directly on twitter, facebook and google plus.
You can find all of my articles and tutorials about game design directly on my website: http://gamedesignquest.com/ ! Finally, you can find all of my Construct 2 tutorials on this forum post.
Unlock your full gamedev potential
Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!
Plus, it's got a lot of additional features that will help you save time and make more impressive games!