using System;
using Microsoft.Xna.Framework.Graphics;
/**
* A moving object in a game. Sprites have positions, velocities, and
* sizes. The size is given as the radius of a circle that surrounds the
* sprite. Such a size is intended only for an approximate determination
* if two sprites collide.
*
* @author Jim Glenn
* @version 0.1 1/19/2009 from Liberation's Sprite.java of 1/8/2009
*/
namespace WindowsGame1
{
public class Sprite
{
/**
* The x-coordinate of this sprite.
*/
protected double x;
/**
* The y-coordinate of this sprite.
*/
protected double y;
/**
* The x-component of this sprite's velocity.
*/
protected double vx;
/**
* The y-component of this sprite's velocity.
*/
protected double vy;
/**
* The size of a circle that encloses this sprite.
*/
protected double radius;
/**
* The color of this sprite.
*/
protected Color color;
/**
* The state of this sprite.
*/
protected int state;
/**
* The normal state of a sprite. This constant is defined to be 0.
*/
public const int StateNormal = 0;
/**
* The amount of time this sprite has spent in its current state.
* Measured in seconds.
*/
private double stateTime;
/**
* Creates a new sprite at the given position.
* The sprite will be black and stationary.
*
* @param initX the x-coordinate of the new sprite
* @param initY the y-coordinate of the new sprite
* @param r the radius of a circle that bounds the new sprite
*/
public Sprite(double initX, double initY, double r)
: this(initX, initY, r, 0.0, 0.0, Color.Black)
{
}
/**
* Creates a new sprite at the given position.
* The sprite will be stationary.
*
* @param initX the x-coordinate of the new sprite
* @param initY the y-coordinate of the new sprite
* @param r the radius of a circle that bounds the new sprite
* @param c the color of the new sprite
*/
public Sprite(double initX, double initY, double r, Color c)
: this(initX, initY, r, 0.0, 0.0, c)
{
}
/**
* Creates a new sprite at the given position.
*
* @param initX the x-coordinate of the new sprite
* @param initY the y-coordinate of the new sprite
* @param r the radius of a circle that bounds the new sprite
* @param initVx the x-component of the velocity of the new sprite
* @param initVy the y-component of the velocity of the new sprite
* @param c the color of the new sprite
*/
public Sprite(double initX, double initY, double r, double initVx, double initVy, Color c)
{
x = initX;
y = initY;
radius = r;
vx = initVx;
vy = initVy;
color = c;
state = StateNormal;
stateTime = 0.0;
}
/**
* Returns the x-coordinate of this sprite.
*
* @return the x-coordinate of this sprite
*/
public double GetX()
{
return x;
}
/**
* Returns the y-coordinate of this sprite.
*
* @return the y-coordinate of this sprite
*/
public double GetY()
{
return y;
}
/**
* Returns the color of this sprite.
*
* @return the color of this sprite
*/
public virtual Color GetColor()
{
return color;
}
/**
* Returns the maximum speed of this sprite. This implementation
* returns positive infinity, indicating no limit. Subclasses should
* override this method so that update will take
* a maximum speed into account.
*/
public virtual double GetMaximumSpeed()
{
return Double.PositiveInfinity;
}
/**
* Sets the velocity of this sprite. If the given velocity exceeds the
* maximum, it will be reduced so that it equals the maximum. In such
* a case the direction will remain the same.
*
* @param vx the new velocity in the horizontal direction
* @param vy the new velocity in the vertical direction
*/
public void SetVelocity(double vxNew, double vyNew)
{
vx = vxNew;
vy = vyNew;
// limit speed
double v = Math.Sqrt(vx * vx + vy * vy);
double maxV = GetMaximumSpeed();
if (v > maxV)
{
vx /= v / maxV;
vy /= v / maxV;
}
}
/**
* Returns the radius of a circle that encloses this sprite.
*
* @return the bounding radius of this sprite
*/
public double GetBoundingRadius()
{
return radius;
}
/**
* Returns the polygonal outline of this Sprite. This implementation
* returns the square inscribed in the bounding circle as defined
* by getBoundingRadius and that has sides parallel to the
* axes of the coordinate system. Subclasses should override this
* method for sprites of other shapes.
*
* @return the outline of this sprite
*/
public virtual void GetOutline()
{
}
/**
* Determines if this sprite collides with the given sprite.
* Two sprites are considered in collision if their outlines intersect.
* (Note that this ignores the special case of one sprite completely
* inside the other.)
*
* @return true if and only if the two sprites are in collision
*/
public bool CollidesWith(Sprite s)
{
return false;
}
/**
* Returns the list of points on the outline of this sprite. The
* starting point is on the list at the start and at the end.
* This implementation currently returns an ArrayList
* in order to facilitate random access to the points.
*
* @return a list of points on the outline of this sprite
*/
private void GetPointsList()
{
}
/**
* Updates this sprite's position and velocity. The velocity will be
* adjusted so that it does not exceed the maximum speed as specified by
* the getMaximumSpeed method.
*
* @param t the time since the last update, in seconds
* @param w the world this sprite belongs to
*/
public virtual void Update(double t, GameModel m)
{
stateTime += t;
// update position
x += vx * t;
y += vy * t;
}
/**
* Returns the state of this sprite.
*
* @return the current state
*/
public int GetState()
{
return state;
}
/**
* Sets the state of this sprite. The state timer is reset to 0.
*
* @param s the new state
*/
public void SetState(int s)
{
state = s;
stateTime = 0.0;
}
/**
* Returns the amount of time this sprite has spent in its current state.
*
* @return the time in the current state
*/
public double GetStateTime()
{
return stateTime;
}
}
}