AiTest/Firefly.cs
Download
aitest.zip
(11.27 KB | 19 June 2010 )
Sample project for implementing the AI of the Butterfly and Firefly sprites of the Boulderdash arcade game.
Donate
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Contents of aitest.zip/AiTest/Firefly.cs
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
}
}
