Help with Enemy platform AI

LALEU François

New member
Hello there !

I's like some help, please.
I'm making a 2D platform game, some sort of "New Zealand story" like game...
I'd like some enemies to roam on platforms without falling from them.

I declared them as
type enemy
aimove wander

so they wander randomly but they fall from platform.
How may I do, please ?

thanks by advance, guys ...  ;D
 
LALEU François said:
I'm making a 2D platform game, some sort of "New Zealand story" like game...
I'd like some enemies to roam on platforms without falling from them.

That's doable but you need to set this script in enemy's IDLE:
Code:
anim	idle
@script
    void self = getlocalvar("self");
    int Dir = getentityproperty(self, "direction");
    int x = getentityproperty(self, "x");
    int y = getentityproperty(self, "y");
    int z = getentityproperty(self, "z");
    void Plat = checkplatformbelow(x, z, y+2);
    void PlatA; void PlatB;
    int Rx = 24; int Vx = 1;

    if(Plat){ // on a platform
      if(Dir==1){
        PlatA = checkplatformbelow(x+Rx, z, y+2);
        PlatB = checkplatformbelow(x+Rx, z, y-2);
      } else {
        PlatA = checkplatformbelow(x-Rx, z, y+2);
        PlatB = checkplatformbelow(x-Rx, z, y-2);
      }

      if(Plat!=PlatA && PlatA==PlatB && Dir==1){
        changeentityproperty(self, "velocity", -Vx);
        changeentityproperty(self, "direction", 0);
      } else if(Plat!=PlatA && PlatA==PlatB && Dir==0){
        changeentityproperty(self, "velocity", Vx);
        changeentityproperty(self, "direction", 1);
      }
    }
@end_script
	loop	1
	delay	4
...

I have to include loop and delay setting to imply that the script requires looping and short delay for it to work properly
The script will regularly check enemy's position relative to platform it is moving on. If it's near platform edge, it will turn around and move the opposite direction.
Rx = 24 means the script will find platform's edge 24 pixels to the left or right depending on facing direction
Vx = 1 means after turning around, enemy will move with 1 pixel per centisecond velocity
You can adjust those two values to suit your enemy. Rx is usually taken from half of enemy's horizontal length but you can set longer or shorter if you want to

HTH

BTW I have used this script in some games such as Cherry On Top and Robo Magi. However if you need simpler example, I can make small demo showing how that script work :)

FYI aimove wander only makes enemy to wander instead of going to their target. It doesn't affect their movement against platforms, obstacles or walls
 
Back
Top Bottom