Graphics

Game Enemy AI
New Topic Add Reply Haunt!
Woadraiders
  • Posts: 95
  • Signup: 03/07/09
Game Enemy AI Quote 1 year ago
I'm curious as to what everybody else does for thier game's enemy AI. I'll share what I do first.

Take my Ninja game for example, I've got a super class called Enemy, and then the Ninja class, Archer class, and Shaman class inherit from Enemy.

GreenNinja and RedNinja inherit the Ninja class.

So then I can have a bunch of different behaviors for different enemy groups. Like RedNinja and GreenNinja are very ordinary enemies, and they have two hurt animations while everybody else has only one, so they're under the Ninja class. And instead of making Green Ninjas and Red Ninjas just use the Ninja class, I made them thier own classes because Green Ninjas are supposed to be dumber and have less health than Red Ninjas.

Archers and Shamans are obviously different from the Ninjas behavior-wise.

But they all do most of the same stuff, before they're "active" they follow a pattern given to them by whatever level thier on. It can look something like:
Code:

var testArcher:Archer = new Archer(474.5, 297.5, -1, dt)
testArcher.AddBehavior("WALK", 0, 300);
testArcher.AddBehavior("WAIT", 1200);
testArcher.AddBehavior("TURN");
testArcher.AddBehavior("WALK", 0, 480);
testArcher.AddBehavior("WAIT", 1200);
testArcher.AddBehavior("TURN");

master.enemies.push(testArcher);


This tells the archer to start facing left, at position (474.5, 297.5) and walk back and forth, waiting 1 and 1/5 second before turning and walking the other way.

Code:

new Archer(474.5, 297.5, -1, dt)
new Archer(startX:Number, startY:Number, startDir:Number, drawTarget: Stage)


Every enemy knows how to follow these directions because all of the code for this is in the Enemy class, so if I want the enemy to follow behaviors then in its Update function I simply write:
FollowBehaviors();

An update function can be as simple as this:
Code:

override public function Update():void
{
if (!this.visible) return;
this.walking = false;
this.xvel = 0;

FollowBehaviors();

if(activated)
{
//Summon our beast. Unless we've already summoned somebody.
if (!hasSummoned)
{
this.attacking = true; //Our 'attack' is to summon
this.hasSummoned = true; //We can do this here.
}
else if(!this.attacking)
{
//... Not sure what we want him to do if he already summoned somebody.
}
}

//Make sure we're on the ground and hitting walls
CheckCollisions();

//We want to play the right animation
UpdateAnimation();

//Make us face the right way
this.scaleX = -this.dir * Math.abs(this.scaleX);
}


This is from the Shaman class, he walks around until he's activated, and if he hasn't already summoned a beast, he'll do it. Then we have to make sure we're colliding with the enviroment, then we have to update our animation, and then make sure we're facing the right way.

CheckCollisions is a function from the Enemy class, and UpdateAnimation is overriden in the Shaman class to fit that specific enemy type.

What do you guys do?
Bestrafe mich.
Du darfst mein Bestrafer sein!
Der Herrgott nimmt.
Der Herrgott gibt.
BluMess
  • Name: Chris
  • Posts: 510
  • Signup: 15/12/08
Re: Game Enemy AI Quote 1 year ago
That's very impressive D:
Just...wow. I kinda learnt something from that

Being crap at actionscript, I normally animate it all.
Now though, still being crap at actionscript, I ask my coder to do it
(Thanks Cam!)
Loves everyone who posts in the forum
zrbng
  • Posts: 2
  • Signup: 04/07/09
Re: Game Enemy AI Quote 1 year ago
What do you guys do?

For starters, I don't use classes or AS3

Besides that I do it the easy AS2 way:
I have a behavior or current state string and just replace the string values when I need to and define each different values of the string. It works for me.
I'd show an example of my AI but don't have any recent examples (examples i have are from early 200.