Download aitest.zip, last updated 19/06/2010 (11.27 KB)

Download
  • md5: 9764f053887f43bd6b0b57f7db7496fa
using System.Drawing;

namespace AiTest
{
  /*
    Movement rules sourced from
    http://www.elmerproductions.com/sp/peterb/BDCFF/objects/0008.html

    if (space to the firefly's left is empty) then
        turn 90 degrees to firefly's left;
        move one space in this new direction; 
    } else if (space ahead is empty) then
        move one space forwards; 
    } else {
        turn 90 degrees to the firefly's right;
        _do not move_;
    }
   */

  class Firefly : Sprite
  {
		#region  Public Constructors  

    public Firefly()
    {
      this.Direction = Direction.Right;
    }

		#endregion  Public Constructors  

		#region  Public Overridden Methods  

    public override void Move()
    {
      Tile tile;

      // TODO: In this sample, we are only checking if a tile is non-solid. We aren't checking the positions of other sprites.

      // first see if we can move in our preferred direction, left
      tile = this.GetAdjacentTile(this.GetNewDirection(Direction.Left));
      if (!tile.Solid)
      {
        // we can move here, update our position and also set our new direction
        this.Location = tile.Location;
        this.Direction = this.GetNewDirection(Direction.Left);
      }
      else
      {
        // can't move in our preferred direction, so lets try the direction the sprite is facing
        tile = this.GetAdjacentTile(this.Direction);
        if (!tile.Solid)
        {
          // we can move here, update our position, but not the direction
          this.Location = tile.Location;
        }
        else
        {
          // can't move forwards either, so finally lets just turn right
          this.Direction = this.GetNewDirection(Direction.Right);
        }
      }
    }

		#endregion  Public Overridden Methods  

		#region  Public Properties  

    public override System.Drawing.Color Color
    {
      get { return Color.FromKnownColor(KnownColor.Firebrick); }
    }

		#endregion  Public Properties  
  }
}

Donate

Donate