using System; using System.Collections.Generic; using System.Text; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Apache { public class entity { //Constructor public entity() { color = System.Drawing.Color.White; x = 0; prevX = 0; y = 0; prevY = 0; setWidth(1); setHeight(1); alive = true; firstFrame = 0; curFrame = 0; totalFrames = 0; animDelay = 1; animCount = 0; columns = 1; spritePoint.X = x; spritePoint.Y = y; sourceRectangle = new System.Drawing.Rectangle(0, 0, 1, 1); } //Setters/Getters public float getX2(){return x + width;} public float getY2(){return y + height;} public void setRotationAngle(float angle) { if (angle > 2 * Math.PI) angle = 0.0f; if (angle < 0) angle = 2 * (float)Math.PI; rotationAngle = angle; } //Methods //---------------------------------------------------------------------------- // Method: loadTexture // Description: Loads a texture for the sprite //---------------------------------------------------------------------------- public bool loadTexture(string filename, Device device) { spriteBitmap = TextureLoader.FromFile(device, filename); //spriteBitmap = TextureLoader.FromFile(device, filename, width, height, 24, 0, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); if (spriteBitmap == null) return false; return true; } //---------------------------------------------------------------------------- // Method: update // Description: Updates game logic //---------------------------------------------------------------------------- public void update() { prevX = x; prevY = y; //calculates the velocities based on the direction they are facing and length of the vector. velX = length * (float)Math.Cos((double)rotationAngle); velY = length * (float)Math.Sin((double)rotationAngle); //updates x += velX; y += velY; spritePoint.X = x; spritePoint.Y = y; //updates rotation point but doesn't actually need to be updated everytime. Really //just needs to be updated when you update width and height only RotationPoint.X = this.width / 2; RotationPoint.Y = this.height / 2; } //---------------------------------------------------------------------------- // Method: animate // Description: Animates a sprite sheet and loops. //---------------------------------------------------------------------------- public void animate() { if (animCount++ > animDelay) { animCount = 0; curFrame++; if (curFrame >= totalFrames) curFrame = firstFrame; } } //---------------------------------------------------------------------------- // Method: animateOnce // Description: animates a sprite sheet once then stops //---------------------------------------------------------------------------- public void animateOnce() { if (animCount++ > animDelay) { animCount = 4; curFrame++; if (curFrame >= totalFrames) curFrame = totalFrames; } } //---------------------------------------------------------------------------- // Method: drawFrame // Description: Draws a single frame from a sprite sheet. //---------------------------------------------------------------------------- public void drawFrame(Sprite spriteHandler) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(); rect.X = (curFrame % columns) * width; rect.Y = (curFrame / columns) * height; rect.Width = width; rect.Height = height; //FIXME: Animation attempts to increase after reaching the final frame //HACK: Stops hp bar from increasing to a frame that doesn't exist if (curFrame >= totalFrames) curFrame = totalFrames; System.Drawing.Rectangle dest = new System.Drawing.Rectangle((int)x, (int)y, width, height); draw(spriteHandler, rect, dest); } //---------------------------------------------------------------------------- // Method: draw (overloaded) // Description: Renders objects to the screen. (overloaded) //---------------------------------------------------------------------------- private void draw(Sprite spriteHandler, System.Drawing.Rectangle srcrect, System.Drawing.Rectangle destrect) { //spriteHandler.Draw(spriteBitmap, srcrect, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(x, y, 0.0f), color); spriteHandler.Draw2D(spriteBitmap, srcrect, new System.Drawing.SizeF(width, height), RotationPoint, rotationAngle, new System.Drawing.PointF(x, y), color); //spriteHandler.Draw2D(spriteBitmap, new System.Drawing.PointF(x + width / 2, y + height / 2), 0.0f, new System.Drawing.PointF(x, y), color); //spriteHandler.Draw2D(spriteBitmap, srcrect, destSize, spritePoint, color); } //---------------------------------------------------------------------------- // Method: draw (overloaded) // Description: Renders objects to the screen. (overloaded) //---------------------------------------------------------------------------- public void draw(Sprite spriteHandler) { //System.Drawing.SizeF destSize = new System.Drawing.SizeF((float)width, (float)height); spriteHandler.Draw2D(spriteBitmap, RotationPoint, rotationAngle, new System.Drawing.PointF(x, y), color); //spriteHandler.Draw(spriteBitmap, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(x, y, 0.0f), color.ToArgb()); //System.Drawing.SizeF destSize = new System.Drawing.SizeF(destrect.Width, destrect.Height); //System.Drawing.PointF position = new System.Drawing.PointF(destrect.X, destrect.Y); //spriteHandler.Draw2D(spriteBitmap, srcrect, destSize, spritePoint, color); } Texture spriteBitmap; private System.Drawing.PointF spritePoint; private System.Drawing.Rectangle sourceRectangle; public System.Drawing.PointF RotationPoint; public float rotationAngle; public float length; //used somewhat like a speed variable (vector length) public System.Drawing.Color color; public float x; public float prevX; public float velX; public float y; public float prevY; public float velY; public int width; public void setWidth(int value) { sourceRectangle.Width = value; width = value; } public int height; public void setHeight(int value) { sourceRectangle.Height = value; height = value; } public bool alive; public int firstFrame; public int curFrame; public int totalFrames; public int animDelay; public int animCount; public int columns; } }