Advertisement

I need an idea on how to implement AI

Started by May 13, 2006 06:04 PM
1 comment, last by adam23 18 years, 6 months ago
First look at this link: http://www.geocities.com/adam_autum/TheJourney.JPG The dragon at the top of the screen should attack the player . Currently I have divided his AI into projects stop, attack, and walk. I need some advice on the best way to implement this. I have him following my character around like a side kick, but that isn't really that convincing. Does anyone have a good link on very simple enemy AI. The dragon attack at close distance, so he has to approach the player and stop then attack. Here is what I have so far:

void enemies::update(int X, int Y, int direction)
{

	//Variables Y, X, and direction are the location of the player
	if(!spottedPlayer)
	{
		if(Y-(enemy.y+128)<150 || enemy.y-(Y-96)>150)
		{
			if(enemy.x - X <100 || X - enemy.x <100)
			{
				spottedPlayer = true;
				currentProject = 2;
			}
		}
	}
	if(currentProject == 2)
	{
		if(X-12!=enemy.x)
		{
			if(enemy.x<X-12)
			{
				enemy.movex = 3;
				enemy.movey = 0;
				if(!chasing)
				{
					currentDirection = 1;
					if(currentDirection != lastDirection)
					{
						start = walkRight[0];
						stop = walkRight[1];
						lastDirection = currentDirection;
						enemy.curframe = start;
					}
				}
			}
			else if(enemy.x>X-12)
			{
				enemy.movex = -3;
				enemy.movey = 0;
				if(!chasing)
				{
					currentDirection = 8;
					if(currentDirection != lastDirection)
					{
						start = walkLeft[0];
						stop = walkLeft[1];
						lastDirection = currentDirection;
						enemy.curframe = start;
					}
				}
			}
		}
		else if(enemy.y-Y > 5)
		{
			enemy.movey = -3;
			currentDirection = 2; //Up
			enemy.movex = 0;
			if(currentDirection != lastDirection)
			{
				start = walkUp[0];
				stop = walkUp[1];
				lastDirection = currentDirection;
				enemy.curframe = start;
			}
		}
		else if(enemy.y-Y<5)
		{
			enemy.movey = 3;
			currentDirection = 5; //Down
			enemy.movex = 0;
			if(currentDirection != lastDirection)
			{
				start = walkDown[0];
				stop = walkDown[1];
				lastDirection = currentDirection;
				enemy.curframe = start;
			}
		}
				
	}

	//Checks for collision then animates enemy
	if(!Collision(enemy,X,Y,96))
	{
		enemy.x += enemy.movex;
		enemy.y += enemy.movey;
		chasing = false;
		if(enemy.movex != 0 || enemy.movey != 0)
		{
			if(++enemy.animcount > enemy.animdelay)
			{
				enemy.animcount = 0;
				if(++enemy.curframe > stop)
				{
					enemy.curframe = start;
				}
			}
		}
	}
	else
	{
		if(enemy.x == X-12)
		{
			if(::abs(enemy.y-Y)<100)
			{
				//Do nothing there is a collision
			}
		}
		else if(enemy.y !=Y)
		{
			if(Y<enemy.y)
			{
				enemy.movey = -3;
				enemy.movex = 0;
				currentDirection = 2;//Up
				chasing = true;
				if(currentDirection!= lastDirection)
				{
					start = walkUp[0];
					stop = walkUp[1];
					enemy.curframe = start;
					lastDirection = currentDirection;
				}
			}
			else if(Y>enemy.y)
			{
				enemy.movey = 3;
				enemy.movex = 0;
				currentDirection = 5;//Down
				chasing = true;
				if(currentDirection!=lastDirection)
				{
					start = walkDown[0];
					stop = walkDown[1];
					enemy.curframe = start;
					lastDirection = currentDirection;
				}
			}
			else
			{
				enemy.movey = 0;
				enemy.movex = 0;
				chasing = false;

			}
			enemy.x += enemy.movex;
			enemy.y += enemy.movey;
			if(enemy.movex != 0 || enemy.movey != 0)
			{
				if(++enemy.animcount > enemy.animdelay)
				{
					enemy.animcount = 0;
					if(++enemy.curframe > stop)
					{
						enemy.curframe = start;
					}
				}
			}
		}


			
	}

}

Should I scrap all of this and start over?
Adamhttp://www.allgamedevelopment.com
I'd vote for scrap and start over...

First, you should take a look at Mat Buckland's article on State Driven Game Agents.

It should help you clear your code a bit ;)

Also, you might wanna go more generic than having your update function take the player's X, Y and direction... how about having each character have a pointer to its current target? As long as its null, the enemy could be looking for one, and when it finds one (based on their abilities, you might wanna have bigger creature see the player in a longer range or something), it stores a pointer to it and tracks it.

You should also break up your update loop into functions. And maybe opt for a pathfinder...

Also, about ::abs(enemy.y-Y), did you really define an abs function in your Enemies class and you're calling the global one? I don't see why enemies should have their own abs function.

You should also avoid "magic numbers". When possible use enums for things like "going down, going up, etc.." and use constants or even per entities parameters for things such as displacement speed.

Hope this helps

Eric
Advertisement
Thank you for the help, I think it will really help me get the what I need done. That is an awesome link :)

I figured the code I had was pretty much junk. The abs function is the one in the std namespace it is not one I defined.
Adamhttp://www.allgamedevelopment.com

This topic is closed to new replies.

Advertisement