Advertisement
bhalash

Flyatron player animation.

Jul 22nd, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.45 KB | None | 0 0
  1. /*
  2.  * Date: 21/07/2012
  3.  * Author: Mark Grealish
  4.  *
  5.  * A very simple sprite animation guide. I chose to preclude the use of a timer and instead demonstrate
  6.  * how different frames are loaded based on given criteria. This illustration has six frames that relate
  7.  * to the mouse's position within the game window.
  8.  *  
  9.  * Questions? Feedback? Email mark@bhalash.com
  10.  *
  11.  * Enjoy and share!
  12.  */
  13.  
  14. using System;
  15. using Microsoft.Xna.Framework;
  16. using Microsoft.Xna.Framework.Content;
  17. using Microsoft.Xna.Framework.Graphics;
  18. using Microsoft.Xna.Framework.Input;
  19. using Microsoft.Xna.Framework.Media;
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22.  
  23. namespace Animmmmmation
  24. {
  25.     public class Game1 : Microsoft.Xna.Framework.Game
  26.     {
  27.         bool debug = true;
  28.  
  29.         GraphicsDeviceManager graphics;
  30.         SpriteBatch spriteBatch;
  31.  
  32.         int gameWidth = 1024;
  33.         int gameHeight = 600;
  34.         bool fullScreen = false;
  35.         bool showMouse = true;
  36.  
  37.         SpriteFont font;
  38.  
  39.         Rectangle bodyRectangle, headRectangle, gunRectangle, flamesRectangle;
  40.         Texture2D headTexture, bodyTexture, gunTexture, flameTexture;
  41.         Vector2 head, headOffset, body, gun, gunOffset, flames;
  42.         float headRotation, bodyRotation, gunRotation;
  43.         int gunWidth, gunHeight, headWidth, headHeight;
  44.         int bodyWidth, bodyHeight, flamesWidth, flamesHeight;
  45.         Stopwatch flamesTimer;
  46.  
  47.         MouseState currentMouseState, previousMouseState;
  48.         KeyboardState currentKeyboardState, previousKeyboardState;
  49.  
  50.         float velocity = 15f;
  51.  
  52.         public Game1()
  53.         {
  54.             graphics = new GraphicsDeviceManager(this);
  55.             graphics.PreferredBackBufferWidth = gameWidth;
  56.             graphics.PreferredBackBufferHeight = gameHeight;
  57.             this.graphics.IsFullScreen = fullScreen;
  58.             this.IsMouseVisible = showMouse;
  59.  
  60.             Content.RootDirectory = "Content";
  61.         }
  62.  
  63.         protected override void Initialize()
  64.         {
  65.             flamesWidth  = 23;
  66.             flamesHeight = 46;
  67.  
  68.             gunWidth  = 35;
  69.             gunHeight = 18;
  70.  
  71.             bodyWidth  = 35;
  72.             bodyHeight = 72;
  73.  
  74.             headWidth  = 35;
  75.             headHeight = 35;
  76.  
  77.             headRectangle   = new Rectangle(0, 0, headWidth, headHeight);
  78.             bodyRectangle   = new Rectangle(0, 0, bodyWidth, bodyHeight);
  79.             gunRectangle    = new Rectangle(0, 0, gunWidth, gunHeight);
  80.             flamesRectangle = new Rectangle(0, 0, flamesWidth, flamesHeight);
  81.  
  82.             body = new Vector2(100,100);
  83.             head = new Vector2(117.5F,117.5F);
  84.             gun = new Vector2(117.5F, 145);
  85.             flames = new Vector2(106, 154);
  86.  
  87.             // These are the centre of the respective texture frames, used to correctly rotate them.
  88.             headOffset = new Vector2(17.5F, 17.5F);
  89.             gunOffset = new Vector2(17.5F, 9F);
  90.  
  91.             bodyRotation = 0;
  92.             headRotation = 0;
  93.             gunRotation  = 0;
  94.  
  95.             flamesTimer = new Stopwatch();
  96.             flamesTimer.Start();
  97.  
  98.             base.Initialize();
  99.         }
  100.  
  101.         protected override void LoadContent()
  102.         {
  103.             spriteBatch = new SpriteBatch(GraphicsDevice);
  104.             font = Content.Load<SpriteFont>("font1");
  105.  
  106.             headTexture = Content.Load<Texture2D>("head");
  107.             bodyTexture = Content.Load<Texture2D>("body");
  108.             gunTexture = Content.Load<Texture2D>("gun");
  109.             flameTexture = Content.Load<Texture2D>("flames");
  110.         }
  111.  
  112.         protected override void UnloadContent()
  113.         {
  114.         }
  115.  
  116.         protected override void Update(GameTime gameTime)
  117.         {
  118.             currentKeyboardState = Keyboard.GetState();
  119.             currentMouseState = Mouse.GetState();
  120.  
  121.             UpdateHead(currentMouseState);
  122.             UpdateBody(currentMouseState);
  123.             UpdateGun(currentMouseState);
  124.             UpdateFlames();
  125.  
  126.             if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  127.                 this.Exit();
  128.             if (Keyboard.GetState().IsKeyDown(Keys.Space))
  129.                 this.Exit();
  130.  
  131.             Move(currentKeyboardState);
  132.  
  133.             previousMouseState = currentMouseState;
  134.             previousKeyboardState = currentKeyboardState;
  135.  
  136.             base.Update(gameTime);
  137.         }
  138.  
  139.         protected override void Draw(GameTime gameTime)
  140.         {
  141.             GraphicsDevice.Clear(Color.CornflowerBlue);
  142.  
  143.             spriteBatch.Begin();
  144.  
  145.             if (debug)
  146.                 Debug(font, spriteBatch);
  147.  
  148.             // Player Flames.
  149.             spriteBatch.Draw(
  150.                 flameTexture,
  151.                 flames,
  152.                 flamesRectangle,
  153.                 Color.White,
  154.                 0,
  155.                 new Vector2(0, 0),
  156.                 1,
  157.                 SpriteEffects.None,
  158.                 0
  159.             );
  160.  
  161.             // Player body.
  162.             spriteBatch.Draw(
  163.                 bodyTexture,
  164.                 body,
  165.                 bodyRectangle,
  166.                 Color.White,
  167.                 bodyRotation,
  168.                 new Vector2(0, 0),
  169.                 1,
  170.                 SpriteEffects.None,
  171.                 0
  172.             );
  173.  
  174.             // Player head.
  175.             spriteBatch.Draw(
  176.                 headTexture,
  177.                 head,
  178.                 headRectangle,
  179.                 Color.White,
  180.                 headRotation,
  181.                 headOffset,
  182.                 1,     
  183.                 SpriteEffects.None,
  184.                 0  
  185.             );
  186.  
  187.             // Player weapon.
  188.             spriteBatch.Draw(
  189.                 gunTexture,
  190.                 gun,
  191.                 gunRectangle,
  192.                 Color.White,
  193.                 gunRotation,
  194.                 gunOffset,
  195.                 1,
  196.                 SpriteEffects.None,
  197.                 0
  198.             );
  199.  
  200.             spriteBatch.End();
  201.  
  202.             base.Draw(gameTime);
  203.         }
  204.  
  205.         private void Debug(SpriteFont font, SpriteBatch spriteBatch)
  206.         {
  207.             int x = 30;
  208.             int y = 30;
  209.  
  210.             List<string> debugMsg = new List<string>()
  211.             {
  212.                 "Head:",
  213.                 "X: " + head.X,
  214.                 "Y: " + head.Y,
  215.                 "Rotation: " + headRotation,
  216.                 "",
  217.                 "Gun:",
  218.                 "X: " + gun.X,
  219.                 "Y: " + gun.Y,
  220.                 "Rotation: " + gunRotation,
  221.                 "",
  222.                 "Body:",
  223.                 "X: " + body.X,
  224.                 "Y: " + body.Y,
  225.                 "",
  226.                 "Flames:",
  227.                 "X: " + flames.X,
  228.                 "Y: " + flames.Y,
  229.                 "Timer: " + flamesTimer.ElapsedMilliseconds
  230.             };
  231.  
  232.             for (int i = 0; i < debugMsg.Count; i++)
  233.             {
  234.                 spriteBatch.DrawString(font, debugMsg[i], new Vector2(x, y), Color.Black);
  235.                 y += 20;
  236.             }
  237.  
  238.         }
  239.  
  240.         private void UpdateHead(MouseState mouse)
  241.         {
  242.             Vector2 mouseLoc = new Vector2(mouse.X, mouse.Y);
  243.  
  244.             Vector2 leftFacing = new Vector2(head.X - mouse.X, head.Y - mouse.Y);
  245.             Vector2 rightFacing = new Vector2(mouse.X - head.X, mouse.Y - head.Y);
  246.  
  247.             float leftAngle = (float)(Math.Atan2(leftFacing.Y, leftFacing.X));
  248.             float rightAngle = (float)(Math.Atan2(rightFacing.Y, rightFacing.X));
  249.  
  250.             if (mouse.X < head.X)
  251.             {
  252.                 headRotation = leftAngle;
  253.                 headRectangle = new Rectangle(44, 0, headWidth, headHeight);
  254.             }
  255.             if (mouse.X > head.X)
  256.             {
  257.                 headRotation = rightAngle;
  258.                 headRectangle = new Rectangle(0, 0, headWidth, headHeight);
  259.             }
  260.         }
  261.  
  262.         private void UpdateGun(MouseState mouse)
  263.         {
  264.             Vector2 mouseLoc = new Vector2(mouse.X, mouse.Y);
  265.  
  266.             Vector2 leftFacing = new Vector2(gun.X - mouse.X, gun.Y - mouse.Y);
  267.             Vector2 rightFacing = new Vector2(mouse.X - gun.X, mouse.Y - gun.Y);
  268.  
  269.             float leftAngle = (float)(Math.Atan2(leftFacing.Y, leftFacing.X));
  270.             float rightAngle = (float)(Math.Atan2(rightFacing.Y, rightFacing.X));
  271.  
  272.             if (mouse.X < gun.X)
  273.             {
  274.                 gunRotation = leftAngle;
  275.                 gunRectangle = new Rectangle(39, 0, gunWidth, gunHeight);
  276.             }
  277.             if (mouse.X > gun.X)
  278.             {
  279.                 gunRotation = rightAngle;
  280.                 gunRectangle = new Rectangle(0, 0, gunWidth, gunHeight);
  281.             }
  282.         }
  283.  
  284.         private void UpdateBody(MouseState mouse)
  285.         {
  286.  
  287.             if (mouse.X < body.X)
  288.             {
  289.                 bodyRectangle = new Rectangle(40, 0, bodyWidth, bodyHeight);
  290.             }
  291.             if (mouse.X > body.X)
  292.             {
  293.                 bodyRectangle = new Rectangle(0, 0, bodyWidth, bodyHeight);
  294.             }
  295.         }
  296.  
  297.         private void UpdateFlames()
  298.         {
  299.             if ((flamesTimer.ElapsedMilliseconds >= 0) && (flamesTimer.ElapsedMilliseconds < 300))
  300.                 flamesRectangle = new Rectangle(52, 0, flamesWidth, flamesHeight); 
  301.             if ((flamesTimer.ElapsedMilliseconds >= 300) && (flamesTimer.ElapsedMilliseconds < 600))
  302.                 flamesRectangle = new Rectangle(25, 0, flamesWidth, flamesHeight);
  303.             if ((flamesTimer.ElapsedMilliseconds >= 600) && (flamesTimer.ElapsedMilliseconds < 900))
  304.                 flamesRectangle = new Rectangle(0, 0, flamesWidth, flamesHeight);
  305.  
  306.             if (flamesTimer.ElapsedMilliseconds > 900)
  307.                 flamesTimer.Restart();
  308.         }
  309.  
  310.         private void Move(KeyboardState keyboard)
  311.         {
  312.             if (Keyboard.GetState().IsKeyDown(Keys.W))
  313.             {
  314.                 if (body.Y > 0)
  315.                 {
  316.                     gun.Y  -= velocity;
  317.                     body.Y -= velocity;
  318.                     head.Y -= velocity;
  319.                     flames.Y -= velocity;
  320.                 }
  321.             }
  322.             if (Keyboard.GetState().IsKeyDown(Keys.S))
  323.             {
  324.                 if (body.Y + bodyRectangle.Height < gameHeight)
  325.                 {
  326.                     gun.Y  += velocity;
  327.                     body.Y += velocity;
  328.                     head.Y += velocity;
  329.                     flames.Y += velocity;
  330.                 }
  331.             }
  332.             if (Keyboard.GetState().IsKeyDown(Keys.A))
  333.             {
  334.                 if (body.X > 0)
  335.                 {
  336.                     gun.X  -= velocity;
  337.                     body.X -= velocity;
  338.                     head.X -= velocity;
  339.                     flames.X -= velocity;
  340.                 }
  341.             }
  342.             if (Keyboard.GetState().IsKeyDown(Keys.D))
  343.             {
  344.                 if (body.X + bodyRectangle.Width < gameWidth)
  345.                 {
  346.                     gun.X  += velocity;
  347.                     body.X += velocity;
  348.                     head.X += velocity;
  349.                     flames.X += velocity;
  350.                 }
  351.             }
  352.  
  353.         }
  354.     }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement