Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace EverMoreDeeper.MenuDesignPlaystyle.HeroClassesBtns
- {
- abstract class AnimatedBtn
- {
- // Variables
- public enum buttonState { Idle, Hovered, MouseDowned, Toggled };
- protected buttonState currentState = buttonState.Idle;
- protected Texture2D sTexture;
- protected Vector2 sPostion;
- private int frameIndex;
- private double timeElapsed;
- private double timeToUpdate;
- protected string currentAnimation;
- // Properties
- public int FramesPerSecond
- {
- set { timeToUpdate = (1f / value); }
- }
- // Collections
- private Dictionary<string, Rectangle[]> sAnimations = new Dictionary<string, Rectangle[]>();
- private Dictionary<string, Vector2> sOffsets = new Dictionary<string, Vector2>();
- public AnimatedBtn(Vector2 position)
- {
- sPostion = position;
- }
- public void AddAnimation(int frames, int yPos, int xStartFrame, string name, int width, int height, Vector2 offset)
- {
- Rectangle[] Rectangles = new Rectangle[frames];
- for (int i = 0; i < frames; i++)
- {
- Rectangles[i] = new Rectangle((i + xStartFrame) * width, yPos, width, height);
- }
- sAnimations.Add(name, Rectangles);
- sOffsets.Add(name, offset);
- }
- public virtual void Update(GameTime gameTime, MouseState mouse)
- {
- timeElapsed += gameTime.ElapsedGameTime.TotalSeconds;
- if (timeElapsed > timeToUpdate)
- {
- timeElapsed -= timeToUpdate;
- if (frameIndex < sAnimations[currentAnimation].Length - 1)
- {
- frameIndex++;
- }
- else //Restarts the animation
- {
- AnimationDone(currentAnimation);
- frameIndex = 0;
- }
- }
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(sTexture, sPostion + sOffsets[currentAnimation], sAnimations[currentAnimation][frameIndex], Color.White);
- }
- public void PlayAnimation(string name)
- {
- //Makes sure we won't start a new annimation unless it differs from our current animation
- if (currentAnimation != name && currentState == buttonState.Idle)
- {
- currentAnimation = name;
- frameIndex = 0;
- }
- }
- /// Method that is called every time an animation finishes
- /// <param name="animationName">Ended animation</param>
- public abstract void AnimationDone(string animation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment