using System; using Microsoft.Xna.Framework.Graphics; /** * A human player in Pick up the Peanuts. * * @author Jim Glenn * @version 0.1 1/21/2009 */ namespace WindowsGame1 { public class Elephant : Sprite { /** * The size of an elephant. */ public const double DefaultSize = 0.025; /** * The number of peanuts this elephant has eaten. */ private int peanutsEaten; /** * The maximum number of peanuts an elephant can eat before... */ private const int maxPeanuts = 4; /** * The state elephants are in when they've eaten too many peanuts. */ public const int StateFull = 1; /** * The amount of time it takes for an elephant to recover after * eating too many peanuts. */ public const double RecoveryTime = 2.0; /** * The amount of time it takes from eating the last peanut to digging. */ private const double lastPeanutTime = 1.0; /** * Creates a new elephant at the given position. * The elephant will be gray. * * @param initX an x-coordinate within the world this elephant will be in * @param initY a y-coordinate within the world this elephant will be in */ public Elephant(double initX, double initY) : base(initX, initY, DefaultSize, Color.Gray) { peanutsEaten = 0; } /** * Returns the maximum speed of this elephant. * * @return the speed */ public override double GetMaximumSpeed() { return 0.2; } /** * Updates this elephant. * * @param t the time since the last update * @param m the model this player belongs to */ public override void Update(double t, GameModel m) { if (GetState() != StateFull) { double peanutX = (m as PeanutsModel).GetPeanut().GetX(); double peanutY = (m as PeanutsModel).GetPeanut().GetY(); // get direction to peanut double dx = peanutX - GetX(); double dy = peanutY - GetY(); // change to one of 8 compass directions if (Math.Abs(dx) < GetBoundingRadius() / 4) { dx = 0; } else { dx = Math.Sign(dx); } if (Math.Abs(dy) < GetBoundingRadius() / 4) { dy = 0; } else { dy = Math.Sign(dy); } SetVelocity(dx * GetMaximumSpeed(), dy * GetMaximumSpeed()); // check for too many peanuts if (peanutsEaten >= maxPeanuts && GetStateTime() > lastPeanutTime) { SetState(StateFull); } } else { // elephant is...busy SetVelocity(0, 0); if (GetStateTime() > RecoveryTime) { SetState(StateNormal); m.AddSprite(new Hole(GetX(), GetY())); peanutsEaten = 0; } } base.Update(t, m); } /** * Gives this elephant a peanut. */ public void EatPeanut() { peanutsEaten++; SetState(StateNormal); } public override void GetOutline() { } } }