Guest User

Untitled

a guest
Jul 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8. namespace TileEngine
  9. {
  10.     public class AnimatedSprite
  11.     {
  12.         public Dictionary<string, FrameAnimation> Animations = new Dictionary<string, FrameAnimation>();
  13.  
  14.         string currentAnimation = null;
  15.         bool animating = true;
  16.         public Vector2 Position = Vector2.Zero;
  17.         Texture2D texture;
  18.  
  19.         public bool IsAnimating
  20.         {
  21.             get { return animating; }
  22.             set { animating = value; }
  23.         }
  24.  
  25.         public FrameAnimation CurrentAnimation
  26.         {
  27.             get
  28.             {
  29.                 if(!string.IsNullOrEmpty(currentAnimation))
  30.                     return Animations[currentAnimation];
  31.                 else
  32.                     return null;
  33.             }
  34.         }
  35.  
  36.         public string CurrentAnimationName
  37.         {
  38.             get { return currentAnimation; }
  39.             set
  40.             {
  41.                 if (Animations.ContainsKey(value))
  42.                     currentAnimation = value;
  43.             }
  44.         }
  45.  
  46.         public AnimatedSprite(Texture2D texture)
  47.         {
  48.             this.texture = texture;
  49.  
  50.         }
  51.  
  52.         public void Update(GameTime gameTime)
  53.         {
  54.             if (!IsAnimating)
  55.                 return;
  56.  
  57.             FrameAnimation animation = CurrentAnimation;
  58.             if (animation == null)
  59.             {
  60.                 if (Animations.Count > 0)
  61.                 {
  62.                     string[] keys = new string[Animations.Count];
  63.                     Animations.Keys.CopyTo(keys, 0);
  64.  
  65.                     currentAnimation = keys[0];
  66.  
  67.                     animation = CurrentAnimation;
  68.                 }
  69.                 else
  70.                     return;
  71.             }
  72.  
  73.             animation.Update(gameTime);
  74.         }
  75.  
  76.         public void Draw(SpriteBatch spriteBatch)
  77.         {
  78.             FrameAnimation animation = CurrentAnimation;
  79.  
  80.             if (animation != null)
  81.                 spriteBatch.Draw(
  82.                     texture,
  83.                     Position,
  84.                     animation.CurrentRect,
  85.                     Color.White);
  86.         }
  87.     }
  88. }
Add Comment
Please, Sign In to add comment