Wednesday, March 21, 2012

Assignment 3 - AI

This is a rough state diagram for the AI controlling one of our game's minor enemies, a stationary shooting enemy:

For "fire:" this is supposed to represent the enemy firing a stream of force in the direction of the player, not killing them but knocking them back. Mechanically speaking, it is drawing a straight line and altering the speed of whatever the line collides with, and the line remains active for five seconds (this is an example time; we may alter the amount of time firing or the pause time in the final implementation).

The enemy does not move from its position, but it quickly aims at and locks onto the player by rotating in the appropriate direction. The rotation speed will likely be high and its aim precise. The enemy's only real weakness is that once it starts firing, it is immobile for the duration, so this is what the player will need to learn to take advantage of. It is worth noting that if the player is clever with their timing, they can actually use this enemy's blast to their advantage, launching them past obstacles and toward their goal.

To function, the class controlling the enemy needs the following data as input at least:
  • the player's current position (which can be used to calculate the desired angle of rotation)
  • elapsed time
Internally, it will have the following data at least:
  • desired angle (see above)
  • current angle
  • rotation speed
  • range
  • force (how much the player's speed is affected if hit by the enemy's blast)
  • a boolean value to indicate which of its two states the AI is currently in
Here is a pseudo-code example to show how the enemy AI might be implemented:

If (!firing) // firing is the boolean value mentioned above
     Aim();
else
     Fire();

function Aim()
{
     if (player is visible, not behind a wall)
     {
          desired angle = new angle calculated from player's current X/Y position;
          if (desired angle > current angle)
               current angle += rotation speed;
          else if (desired angle < current angle)
               current angle -= rotation speed;
          else if (desired angle == current angle AND calculated distance to player < range)
               firing = true;
     }
}
function Fire()
{
     animate sprite charging graphic (if applicable);
     if (pause time has elapsed)
     {
           animate firing effect at current angle;
           if (player collides with line at current angle)
                player speed -= force; //we may make this affect other objects as well, but likely will not
     }
     if (total firing time has elapsed)
          firing = false;
}

No comments:

Post a Comment