using Microsoft.Xna.Framework.Graphics; /** * A human player in Pick up the Peanuts. * * @author Jim Glenn * @version 0.2 2/10/2009 C# version (still needs XML comments) * @version 0.1 1/21/2009 */ namespace Peanuts { public class Player : Sprite { /** * The size of the human player. */ public const double DefaultSize = 0.025; /** * The amount of time, in seconds, that this player takes to recover * from being trampled. */ private const double RecoveryTime = 0.5; /** * The amount of time, in seconds, that this player takes to climb * out of an elephant hole. */ private const double ClimbingTime = 0.2; /** * Constants for state. */ private const int stateTrampled = 1; private const int needsToClimb = 2; /** * The direction this player will go when it recovers from being trampled. */ private int recoverDx; private int recoverDy; /** * The amount of time it will take this player to recover from * whatever mishap has befalled it. */ private double recoveryTime; /** * Creates a new human player at the given position. * The player will be red. * * @param initX an x-coordinate within the world this playwe will be in * @param initY a y-coordinate within the world this Player will be in */ public Player(double initX, double initY) : base(initX, initY, DefaultSize, Color.Red) { } /** * Returns the maximum speed of this player. * * @return the speed */ public override double GetMaximumSpeed() { return 0.2; } /** * Changes the velocity of this player so that it is moving in * the given direction. Its speed will be set to the maximum. * * @param dx -1, 0, or 1 * @param dy -1, 0, or 1 */ public void Move(int dx, int dy) { if (GetState() == StateNormal) { SetVelocity(GetMaximumSpeed() * dx, GetMaximumSpeed() * dy); } else { recoverDx = dx; recoverDy = dy; } } /** * Tramples this player. This player will not be able to move until * it recovers. */ public void Trample() { if (GetState() != stateTrampled) { SetState(stateTrampled); recoverDx = 0; recoverDy = 0; recoveryTime = RecoveryTime; } } /** * Puts the player in a hole. The player will not be able * to move until it climbs out. */ public void StepInHole() { // treat the same as a trampling, but with a different recovery time if (GetState() != stateTrampled) { SetState(stateTrampled); recoverDx = 0; recoverDy = 0; recoveryTime = ClimbingTime; } } /** * Climbs this player out of a hole. */ public void Climb() { if (GetState() == needsToClimb) { SetVelocity(recoverDx, recoverDy); SetState(StateNormal); } } /** * Updates this player. * * @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() == stateTrampled) { if (GetStateTime() > recoveryTime) { SetState(needsToClimb); } else { SetVelocity(0, 0); } } base.Update(t, m); // do wraparound if (x < GetBoundingRadius() / 2) { x = m.GetWidth() - GetBoundingRadius() / 2; } else if (x + GetBoundingRadius() / 2 > m.GetWidth()) { x = GetBoundingRadius() / 2; } if (y < GetBoundingRadius() / 2) { y = m.GetHeight() - GetBoundingRadius() / 2; } else if (y + GetBoundingRadius() / 2 > m.GetHeight()) { y = GetBoundingRadius() / 2; } } } }