Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Date: 21/07/2012
- * Author: Mark Grealish
- *
- * A very simple sprite animation guide. I chose to preclude the use of a timer and instead demonstrate
- * how different frames are loaded based on given criteria. This illustration has six frames that relate
- * to the mouse's position within the game window.
- *
- * Questions? Feedback? Email mark@bhalash.com
- *
- * Enjoy and share!
- */
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace Animmmmmation
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- bool debug = true;
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- int gameWidth = 1024;
- int gameHeight = 600;
- bool fullScreen = false;
- bool showMouse = true;
- SpriteFont font;
- Rectangle bodyRectangle, headRectangle, gunRectangle, flamesRectangle;
- Texture2D headTexture, bodyTexture, gunTexture, flameTexture;
- Vector2 head, headOffset, body, gun, gunOffset, flames;
- float headRotation, bodyRotation, gunRotation;
- int gunWidth, gunHeight, headWidth, headHeight;
- int bodyWidth, bodyHeight, flamesWidth, flamesHeight;
- Stopwatch flamesTimer;
- MouseState currentMouseState, previousMouseState;
- KeyboardState currentKeyboardState, previousKeyboardState;
- float velocity = 15f;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = gameWidth;
- graphics.PreferredBackBufferHeight = gameHeight;
- this.graphics.IsFullScreen = fullScreen;
- this.IsMouseVisible = showMouse;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- flamesWidth = 23;
- flamesHeight = 46;
- gunWidth = 35;
- gunHeight = 18;
- bodyWidth = 35;
- bodyHeight = 72;
- headWidth = 35;
- headHeight = 35;
- headRectangle = new Rectangle(0, 0, headWidth, headHeight);
- bodyRectangle = new Rectangle(0, 0, bodyWidth, bodyHeight);
- gunRectangle = new Rectangle(0, 0, gunWidth, gunHeight);
- flamesRectangle = new Rectangle(0, 0, flamesWidth, flamesHeight);
- body = new Vector2(100,100);
- head = new Vector2(117.5F,117.5F);
- gun = new Vector2(117.5F, 145);
- flames = new Vector2(106, 154);
- // These are the centre of the respective texture frames, used to correctly rotate them.
- headOffset = new Vector2(17.5F, 17.5F);
- gunOffset = new Vector2(17.5F, 9F);
- bodyRotation = 0;
- headRotation = 0;
- gunRotation = 0;
- flamesTimer = new Stopwatch();
- flamesTimer.Start();
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- font = Content.Load<SpriteFont>("font1");
- headTexture = Content.Load<Texture2D>("head");
- bodyTexture = Content.Load<Texture2D>("body");
- gunTexture = Content.Load<Texture2D>("gun");
- flameTexture = Content.Load<Texture2D>("flames");
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- currentKeyboardState = Keyboard.GetState();
- currentMouseState = Mouse.GetState();
- UpdateHead(currentMouseState);
- UpdateBody(currentMouseState);
- UpdateGun(currentMouseState);
- UpdateFlames();
- if (Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- this.Exit();
- Move(currentKeyboardState);
- previousMouseState = currentMouseState;
- previousKeyboardState = currentKeyboardState;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- if (debug)
- Debug(font, spriteBatch);
- // Player Flames.
- spriteBatch.Draw(
- flameTexture,
- flames,
- flamesRectangle,
- Color.White,
- 0,
- new Vector2(0, 0),
- 1,
- SpriteEffects.None,
- 0
- );
- // Player body.
- spriteBatch.Draw(
- bodyTexture,
- body,
- bodyRectangle,
- Color.White,
- bodyRotation,
- new Vector2(0, 0),
- 1,
- SpriteEffects.None,
- 0
- );
- // Player head.
- spriteBatch.Draw(
- headTexture,
- head,
- headRectangle,
- Color.White,
- headRotation,
- headOffset,
- 1,
- SpriteEffects.None,
- 0
- );
- // Player weapon.
- spriteBatch.Draw(
- gunTexture,
- gun,
- gunRectangle,
- Color.White,
- gunRotation,
- gunOffset,
- 1,
- SpriteEffects.None,
- 0
- );
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void Debug(SpriteFont font, SpriteBatch spriteBatch)
- {
- int x = 30;
- int y = 30;
- List<string> debugMsg = new List<string>()
- {
- "Head:",
- "X: " + head.X,
- "Y: " + head.Y,
- "Rotation: " + headRotation,
- "",
- "Gun:",
- "X: " + gun.X,
- "Y: " + gun.Y,
- "Rotation: " + gunRotation,
- "",
- "Body:",
- "X: " + body.X,
- "Y: " + body.Y,
- "",
- "Flames:",
- "X: " + flames.X,
- "Y: " + flames.Y,
- "Timer: " + flamesTimer.ElapsedMilliseconds
- };
- for (int i = 0; i < debugMsg.Count; i++)
- {
- spriteBatch.DrawString(font, debugMsg[i], new Vector2(x, y), Color.Black);
- y += 20;
- }
- }
- private void UpdateHead(MouseState mouse)
- {
- Vector2 mouseLoc = new Vector2(mouse.X, mouse.Y);
- Vector2 leftFacing = new Vector2(head.X - mouse.X, head.Y - mouse.Y);
- Vector2 rightFacing = new Vector2(mouse.X - head.X, mouse.Y - head.Y);
- float leftAngle = (float)(Math.Atan2(leftFacing.Y, leftFacing.X));
- float rightAngle = (float)(Math.Atan2(rightFacing.Y, rightFacing.X));
- if (mouse.X < head.X)
- {
- headRotation = leftAngle;
- headRectangle = new Rectangle(44, 0, headWidth, headHeight);
- }
- if (mouse.X > head.X)
- {
- headRotation = rightAngle;
- headRectangle = new Rectangle(0, 0, headWidth, headHeight);
- }
- }
- private void UpdateGun(MouseState mouse)
- {
- Vector2 mouseLoc = new Vector2(mouse.X, mouse.Y);
- Vector2 leftFacing = new Vector2(gun.X - mouse.X, gun.Y - mouse.Y);
- Vector2 rightFacing = new Vector2(mouse.X - gun.X, mouse.Y - gun.Y);
- float leftAngle = (float)(Math.Atan2(leftFacing.Y, leftFacing.X));
- float rightAngle = (float)(Math.Atan2(rightFacing.Y, rightFacing.X));
- if (mouse.X < gun.X)
- {
- gunRotation = leftAngle;
- gunRectangle = new Rectangle(39, 0, gunWidth, gunHeight);
- }
- if (mouse.X > gun.X)
- {
- gunRotation = rightAngle;
- gunRectangle = new Rectangle(0, 0, gunWidth, gunHeight);
- }
- }
- private void UpdateBody(MouseState mouse)
- {
- if (mouse.X < body.X)
- {
- bodyRectangle = new Rectangle(40, 0, bodyWidth, bodyHeight);
- }
- if (mouse.X > body.X)
- {
- bodyRectangle = new Rectangle(0, 0, bodyWidth, bodyHeight);
- }
- }
- private void UpdateFlames()
- {
- if ((flamesTimer.ElapsedMilliseconds >= 0) && (flamesTimer.ElapsedMilliseconds < 300))
- flamesRectangle = new Rectangle(52, 0, flamesWidth, flamesHeight);
- if ((flamesTimer.ElapsedMilliseconds >= 300) && (flamesTimer.ElapsedMilliseconds < 600))
- flamesRectangle = new Rectangle(25, 0, flamesWidth, flamesHeight);
- if ((flamesTimer.ElapsedMilliseconds >= 600) && (flamesTimer.ElapsedMilliseconds < 900))
- flamesRectangle = new Rectangle(0, 0, flamesWidth, flamesHeight);
- if (flamesTimer.ElapsedMilliseconds > 900)
- flamesTimer.Restart();
- }
- private void Move(KeyboardState keyboard)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.W))
- {
- if (body.Y > 0)
- {
- gun.Y -= velocity;
- body.Y -= velocity;
- head.Y -= velocity;
- flames.Y -= velocity;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.S))
- {
- if (body.Y + bodyRectangle.Height < gameHeight)
- {
- gun.Y += velocity;
- body.Y += velocity;
- head.Y += velocity;
- flames.Y += velocity;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.A))
- {
- if (body.X > 0)
- {
- gun.X -= velocity;
- body.X -= velocity;
- head.X -= velocity;
- flames.X -= velocity;
- }
- }
- if (Keyboard.GetState().IsKeyDown(Keys.D))
- {
- if (body.X + bodyRectangle.Width < gameWidth)
- {
- gun.X += velocity;
- body.X += velocity;
- head.X += velocity;
- flames.X += velocity;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement