what's the best way to program AI for a platform game?
specifically, right now I'm wondering how to make an enemy that moves alternatively left and right on a platform, falls off the platform if it goes off the edge, and walks up and down sloped ground.
I can give you some insight on the enemies from Bonesaw, I'm really tired but lets see...
You can make a platform engine based on moving each sprite around by 1 pixel in each direction, checking for a collision, and then moving it back to it's original position. You can then trigger flags for each collision direction, which your enemies and characters can use in later events.
So you need values like horizontal speed, vertical speed, and anything else you'll need for each object thats going to be in the platform engine. Then making enemies is as simple as making platform engine objects with self incrementing counters, and when the counter reaches certain values you can just tell the enemy to set its x speed to 2 or something to move right.
Thats all my brain can put out for now, hope I helped a little bit.
A lot of people seem to be having problems with doing platform movement AI, especially with multiple instances of an enemy moving around at the same time. So... I'm currently working on a small multi-enemy platform movement AI engine that I'll release as a public download as soon as I'm done with it. So, just be patient
I do a bit of a sucky enemy ai in my lil pirate engine.
Each type of enemy has their own collision detector to fit their shape. It's quite high up so they can walk up less steep slopes. They just move up or down depending on if they are in the air or inside the floor (the events also work for solid active objects).
the collision event is something like
IF
detector1 overlapping background
+detector1 overlapping enemy1
+enemy1 flag 0 on (flag 0 is used to toggle left and right movement)
Dustin Gunn Gnarly Tubular Way Cool Awesome Groovy Mondo
Registered 15/12/2004
Points 2659
15th February, 2008 at 13:37:38 -
I wouldn't recommend a detector, though it may seem simpler, connecting 2 objects together when there's more than one enemy isn't any more for beginners than doing it the proper way
This is certainly not for beginners either but I'll try to explain it in detail.
First, realize this checks every single pixel of movement, so if you're using fine detection and your enemy is animated, the pixels will go through the wall/floor and mess it up. You can practice with just having enemies as squares, but if you want to have fully animated enemies that don't require a detector, go into the animation editor and choose a direction you're not using, give it the exact same amount of frames, speeds, loops, etc as the real animations, and just make it a box.
Now at the top of all the events you have always - change ANIMATION direction (not change direction) to the box direction. Then you have all the platform AI events and collision tests, which will now test collisions for the box animation. After those, put an "always - restore animation direction" which if you changed animation direction earlier, will now revert to whatever you were using before, including being on the correct frame and everything. So it collides with the box direction and what you see is the drawn direction.
now the theory behind proper platform AI:
You can use fastloops or copy/pasted events over and over, but for each loop there should be:
* On loop "enemy move" If object's left speed is greater to loopindex("enemy move")
move object's x position to x position -1
* Is object over obstacle?
(perform slope test) set Y position to Y position -1
* Is it still over obstacle?
Set Y position to Y position +1, set X position to X position +1, set left speed to 0, whatever
Repeat that stuff for going right, and up and down (But with no slope testing for those 2, obviously)
I don't use 2 different values for left/right or up/down and instead have something like "X speed is less than (loopindex("enemy move")*-1)."
I also have a seperate routine for gravity which is similar in theory but pushes down only.