|
• Help Needed in AI in 3D Games
Think I can help you on your way...
For the following, I'm assuming that you are already able to make the enemy walk and turn.
Easiest way I can think of, is using a waypoint system and a state machine.
Waypoint system: You specify locations (waypoints) in your environment (ie. in a level editor), and for each location you specify the connections to other (nearby) waypoints. This way you can create a grid or paths your enemy can walk on. Good thing about this is that you don't have to worry about collision detection (at least not with the static environment)
State machine: Can be implemented as a switch/case statement. You will need about three states: Guard, run, attack...
A short (pseudo code):
switch (currentState)
{
case STATE_GUARD:
{
// Do nothing but standing
// Check whether to change to another state
if (distanceToPlayer < 5.0f)
{
currentState = STATE_ATTACK;
}
}
break;
case STATE_ATTACK:
{
// Shoot gun/use blade/whatever
if (distanceToPlayer < 3.0f)
{
currentState = STATE_RETREAT;
}
}
break;
case STATE_RETREAT:
{
// Shoot gun/use blade/whatever
WalkAway();
// WalkAway should disable state machine until a certain distance (ie > 5.0f)
currentState = STATE_GUARD;
}
break;
}
Note that the above piece of code is missing something trivial: Pathfinding. Easiest is to implement WalkAway something like this.
void WalkAway()
{
for (int i=0;i<currentWaypoint.NumConnections;i++)
{
if (GetDistanceBetween(currentWaypoint, currentWaypoint.Connection[i]) > DistanceToPlayer)
{
// Do your enemy walk code here
WalkToWaypoint(currentWaypoint.Connection[i]);
return;
}
}
}
Hope this makes any sense!
If not, drop me a mail on sweex@yahoo.com
|