Author Topic: [SOLVED] Enemy AI - Jumping up to a platform  (Read 2067 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
[SOLVED] Enemy AI - Jumping up to a platform
« on: Thu, Jul 22, 2010 »
I'm trying to implement some basic enemy AI into my small project. I already have a lot of aggressive AI set up into my enemies so that they chase after the player when the player enters a certain "aggro radius".

Now I'm trying to have my enemies jump up a tileblock if they are chasing the player and they run into one.

Any suggestions? I'm really stumped on this one.
« Last Edit: Fri, Jul 23, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

xkoster

  • Guest
Re: Enemy AI - Jumping up to a platform
« Reply #1 on: Thu, Jul 22, 2010 »
try something like
Code: [Select]
override public function hitLeft(Contact:FlxObject, Velocity:Number):void
{
if (player.x < x)
jump();

super.hitLeft(Contact, Velocity);
}

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Enemy AI - Jumping up to a platform
« Reply #2 on: Thu, Jul 22, 2010 »
try something like
Code: [Select]
override public function hitLeft(Contact:FlxObject, Velocity:Number):void
{
if (player.x < x)
jump();

super.hitLeft(Contact, Velocity);
}

I'm hoping for something a little more predictive. Is there any way I can check if my enemy sprite is colliding with a specific tile index or tile block?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

xkoster

  • Guest
Re: Enemy AI - Jumping up to a platform
« Reply #3 on: Thu, Jul 22, 2010 »
try something like
Code: [Select]
override public function hitLeft(Contact:FlxObject, Velocity:Number):void
{
if (player.x < x)
jump();

super.hitLeft(Contact, Velocity);
}

I'm hoping for something a little more predictive. Is there any way I can check if my enemy sprite is colliding with a specific tile index or tile block?

Code: [Select]
var tileX:uint = x / tileWidth;
var tileY:uint = y / tileHeight;

if (tilemap.getTile(tileX - 1, tileY) == wallBlockId && player.x < x)
jump();
else if (tilemap.getTile(tileX + 1, tileY) == wallBlockId && player.x > x)
jump();

zez

  • Active Member
  • ***
  • Posts: 203
  • Karma: +0/-0
    • View Profile
    • Devlog
Re: Enemy AI - Jumping up to a platform
« Reply #4 on: Thu, Jul 22, 2010 »
Thats pretty much exactly how you would do it, although you probably want to check and make sure the spot they will land is legit (Meaning, you want
if (getTile((x/map.tileWidth) + or - 1, (y/map.tileHeight) - (jumpheight in tiles)) < map.collideIndex && something to figure out where the player is in there to make sure its appropriate to jump){do your jumping code;})
You also need to make sure to pass your map to the enemy when you spawn them, for obvious reasons.

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Enemy AI - Jumping up to a platform
« Reply #5 on: Thu, Jul 22, 2010 »
Okay I overrode the hitSide function like so:

Code: [Select]
override public function hitSide(Contact:FlxObject, Velocity:Number):void
{

super.hitSide(Contact, Velocity);

velocity.y = -100;

}

I tested it and the enemy does nothing when coming in contact with a wall. Am I missing something?


Edit:

I've been running some tests. I believe that hitSide() will only trigger if neither hitTop() nor hitBottom() are being triggered. I overrode hitBottom() to give a small upward velocity every time the enemy touched the ground, and then hitSide() triggered properly.

So I took your code, xkoster, modified it a tad and put it into my update function. Worked like a charm. Thanks!

Here's the code:
Code: [Select]
var tileX:uint = x / 24; // my tiles are 24x24
var tileY:uint = y / 24;

if (_map.getTile(tileX - 1, tileY) && velocity.x < 0)
jump();
else if (_map.getTile(tileX + 2, tileY) && velocity.x > 0)
jump();
« Last Edit: Fri, Jul 23, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery