Guest User

Untitled

a guest
May 14th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5.  
  6. namespace EverMoreDeeper.MenuDesignPlaystyle.HeroClassesBtns
  7. {
  8.    abstract class AnimatedBtn
  9.     {
  10.  
  11.         // Variables
  12.         public enum buttonState { Idle, Hovered, MouseDowned, Toggled };
  13.         protected buttonState currentState = buttonState.Idle;
  14.         protected Texture2D sTexture;
  15.         protected Vector2 sPostion;
  16.         private int frameIndex;
  17.         private double timeElapsed;
  18.         private double timeToUpdate;
  19.         protected string currentAnimation;
  20.  
  21.         // Properties
  22.         public int FramesPerSecond
  23.         {
  24.             set { timeToUpdate = (1f / value); }
  25.         }
  26.  
  27.         // Collections
  28.         private Dictionary<string, Rectangle[]> sAnimations = new Dictionary<string, Rectangle[]>();
  29.         private Dictionary<string, Vector2> sOffsets = new Dictionary<string, Vector2>();
  30.  
  31.         public AnimatedBtn(Vector2 position)
  32.         {
  33.             sPostion = position;
  34.         }
  35.  
  36.  
  37.         public void AddAnimation(int frames, int yPos, int xStartFrame, string name, int width, int height, Vector2 offset)
  38.         {
  39.  
  40.             Rectangle[] Rectangles = new Rectangle[frames];
  41.  
  42.             for (int i = 0; i < frames; i++)
  43.             {
  44.                 Rectangles[i] = new Rectangle((i + xStartFrame) * width, yPos, width, height);
  45.             }
  46.             sAnimations.Add(name, Rectangles);
  47.             sOffsets.Add(name, offset);
  48.         }
  49.  
  50.  
  51.         public virtual void Update(GameTime gameTime, MouseState mouse)
  52.         {
  53.  
  54.             timeElapsed += gameTime.ElapsedGameTime.TotalSeconds;
  55.  
  56.  
  57.             if (timeElapsed > timeToUpdate)
  58.             {
  59.  
  60.                 timeElapsed -= timeToUpdate;
  61.  
  62.  
  63.                 if (frameIndex < sAnimations[currentAnimation].Length - 1)
  64.                 {
  65.                     frameIndex++;
  66.                 }
  67.                 else //Restarts the animation
  68.                 {
  69.                     AnimationDone(currentAnimation);
  70.                     frameIndex = 0;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         public void Draw(SpriteBatch spriteBatch)
  76.         {
  77.             spriteBatch.Draw(sTexture, sPostion + sOffsets[currentAnimation], sAnimations[currentAnimation][frameIndex], Color.White);
  78.         }
  79.  
  80.  
  81.         public void PlayAnimation(string name)
  82.         {
  83.             //Makes sure we won't start a new annimation unless it differs from our current animation
  84.             if (currentAnimation != name && currentState == buttonState.Idle)
  85.             {
  86.                 currentAnimation = name;
  87.                 frameIndex = 0;
  88.             }
  89.         }
  90.  
  91.  
  92.         /// Method that is called every time an animation finishes
  93.         /// <param name="animationName">Ended animation</param>
  94.         public abstract void AnimationDone(string animation);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment