home *** CD-ROM | disk | FTP | other *** search
- /* AnimatedImage.cs: Contains the AnimatedImage class, which is a
- * generic class capable of holding and doing animation logic for
- * an image. You can sub-class this object to customize its behavior.
- */
-
- using System;
- using System.Drawing;
-
- namespace GameClasses {
- public class AnimatedImage {
- // Constants
- public const int DEFAULTSTEP = 3;
- public const int SOUTHWEST = 1;
- public const int SOUTH = 2;
- public const int SOUTHEAST = 3;
- public const int EAST = 4;
- public const int NORTHEAST = 5;
- public const int NORTH = 6;
- public const int NORTHWEST = 7;
- public const int WEST = 8;
-
- // Member data
- public int imageWidth; // Width of the image
- public int imageHeight; // Height of the image
- public int imagePosX; // X Position in containing object
- public int imagePosY; // Y Position in containing object
- public Image imageData; // The image itself
-
- public int imageOffsetX; // facilitates "relative" positioning
- public int imageOffsetY; // by placing image at a specified
- // offset to PosX, PosY.
-
- public int animationStep; // How much the image should move/step
-
- public Rectangle constraintBox; // Our sandbox to play in
- public int direction; // the current direction the image is heading
- public bool isActive; // whether the image should be animated
- public AnimatedImage owner; // if it's controlled by another image
-
- #region Constructors
- public AnimatedImage() : this(0,0) {} // default constructor
-
- public AnimatedImage(int posX, int posY) {
- imagePosX = posX;
- imagePosY = posY;
- isActive = false;
- animationStep = DEFAULTSTEP;
- }
- #endregion
-
- // Animate() performs all position movements on the object.
- virtual public void Animate() {
- switch(direction) {
- case SOUTHWEST:
- this.imagePosX -= animationStep;
- this.imagePosY += animationStep;
- break;
- case SOUTH:
- this.imagePosY += animationStep;
- break;
- case SOUTHEAST:
- this.imagePosX += animationStep;
- this.imagePosY += animationStep;
- break;
- case WEST:
- this.imagePosX -= animationStep;
- break;
- case EAST:
- this.imagePosX += animationStep;
- break;
- case NORTHWEST:
- this.imagePosX -= animationStep;
- this.imagePosY -= animationStep;
- break;
- case NORTH:
- this.imagePosY -= animationStep;
- break;
- case NORTHEAST:
- this.imagePosX += animationStep;
- this.imagePosY -= animationStep;
- break;
- }
- }
-
-
- // Change the image to be scaled to width, height
- virtual public void RescaleImage(int width, int height) {
- imageWidth = width;
- imageHeight = height;
- }
-
-
- // Display the contained image using the given Graphics object
- virtual public void Display(Graphics g) {
- g.DrawImage(imageData, imagePosX + imageOffsetX,
- imagePosY + imageOffsetY,
- imageWidth, imageHeight);
- }
-
- // Determines if the point is within the confines of this image
- virtual public bool ContainsPoint(int x, int y) {
- return (x >= imagePosX && x <= imagePosX + imageWidth &&
- y >= imagePosY && y <= imagePosY + imageHeight);
- }
-
- // Determines if the given rectangle intersects this image
- virtual public bool Intersects(int x, int y,
- int width, int height) {
- // simple variable renaming
- int x2 = x + width, y2 = y + height, _x = imagePosX;
- int _y = imagePosY, _x2 = imagePosX + imageWidth;
- int _y2 = imagePosY + imageHeight;
-
- return ((x >= _x && x <= _x2) || (x2 >= _x && x2 <= _x2) ||
- (_x >= x && _x <= x2) || (_x2 >= x && _x2 <= x2)) &&
- ((y >= _y && y <= _y2) || (y2 >= _y && y2 <= _y2) ||
- (_y >= y && _y <= y2) || (_y2 >= y && _y2 <= y2));
- }
-
- virtual public bool Intersects(AnimatedImage img) {
- return Intersects(img.imagePosX, img.imagePosY,
- img.imageWidth, img.imageHeight);
- }
-
- // Moves the image to be within the specified boundary.
- // Returns whether any position changes were made.
- virtual public bool ConstrainToBox() {
- bool retVal = false;
-
- if (!isActive)
- return false;
-
- if (imagePosX < constraintBox.X) {
- imagePosX = constraintBox.X;
- retVal = true;
- } else if ((imagePosX + imageWidth) >
- (constraintBox.X + constraintBox.Width)) {
- imagePosX = constraintBox.X +
- constraintBox.Width - imageWidth;
- retVal = true;
- }
-
- if (imagePosY < constraintBox.Y) {
- imagePosY = constraintBox.Y;
- retVal = true;
- } else if ((imagePosY + imageHeight) >
- (constraintBox.Y + constraintBox.Height)) {
- imagePosY = constraintBox.Y +
- constraintBox.Height - imageHeight;
- retVal = true;
- }
-
- return retVal;
- }
-
- // Basic deflection
- // Returns whether any position changes were made.
- virtual public bool Deflect(int x, int y, int width, int height) {
- if (!isActive)
- return false;
-
- switch(direction) {
- case SOUTHWEST:
- if (imagePosX <= x) {
- direction = SOUTHEAST;
- } else {
- direction = NORTHWEST;
- }
- break;
- case SOUTH:
- direction = NORTH;
- break;
- case SOUTHEAST:
- if ((imagePosX + imageWidth) >= (x + width)) {
- direction = SOUTHWEST;
- } else {
- direction = NORTHEAST;
- }
- break;
- case WEST:
- direction = EAST;
- break;
- case EAST:
- direction = WEST;
- break;
- case NORTHWEST:
- if (imagePosY <= y) {
- direction = SOUTHWEST;
- } else {
- direction = NORTHEAST;
- }
- break;
- case NORTH:
- direction = SOUTH;
- break;
- case NORTHEAST:
- if (imagePosY <= y) {
- direction = SOUTHEAST;
- } else {
- direction = NORTHWEST;
- }
- break;
- }
- return true;
- }
-
- // Deflect override -- deflects off of a given AnimatedImage
- virtual public bool Deflect(AnimatedImage img) {
- return Deflect(img.imagePosX, img.imagePosY,
- img.imageWidth, img.imageHeight);
- }
-
- // Deflect override -- deflects off of default constraint box
- virtual public bool Deflect() {
- return Deflect(constraintBox.X, constraintBox.Y,
- constraintBox.Width, constraintBox.Height);
- }
-
- virtual public bool WrapInBox() {
- bool retVal = false;
-
- if (!isActive)
- return false;
-
- if (imagePosX < constraintBox.X) {
- imagePosX = constraintBox.X + constraintBox.Width - imageWidth;
- retVal = true;
- } else if ((imagePosX + imageWidth) >
- (constraintBox.X + constraintBox.Width)) {
- imagePosX = constraintBox.X;
- retVal = true;
- }
-
- if (imagePosY < constraintBox.Y) {
- imagePosY = constraintBox.Y + constraintBox.Height -
- imageHeight;
- retVal = true;
- } else if ((imagePosY + imageHeight) >
- (constraintBox.Y + constraintBox.Height)) {
- imagePosY = constraintBox.Y;
- retVal = true;
- }
-
- return retVal;
- }
-
- // reposition the object in a random place within its constraintBox
- public void RandomizePosition() {
- Random rnd = new Random();
- imagePosX = rnd.Next(constraintBox.Width - imageWidth);
- imagePosY = rnd.Next(constraintBox.Height - imageHeight);
- direction = rnd.Next(1, 8);
- }
-
- // Take whatever direction you are going and go the
- // opposite direction
- virtual public void ReverseDirection() {
- switch (direction) {
- case SOUTHWEST:
- direction = NORTHEAST;
- break;
- case SOUTH:
- direction = NORTH;
- break;
- case SOUTHEAST:
- direction = NORTHWEST;
- break;
- case WEST:
- direction = EAST;
- break;
- case EAST:
- direction = WEST;
- break;
- case NORTHWEST:
- direction = SOUTHEAST;
- break;
- case NORTH:
- direction = SOUTH;
- break;
- case NORTHEAST:
- direction = SOUTHWEST;
- break;
- }
- }
-
- // Rotate the direction by the specified amount
- // increment is in 45 degree steps
- virtual public void RotateDirection(int increment) {
- int result = increment + direction;
-
- if (result > 8) {
- result %= 8;
- if (result == 0)
- result = 8;
- } else if (result < 1) {
- result = (result % 8) + 8;
- }
-
- direction = result;
- }
- }
- }