Advertisement
Guest User

ah

a guest
Apr 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Shadow
  11. {
  12.     class AnimetedSprite : Sprite
  13.     {
  14.         public AnimetedSprite(Vector2 position, Texture2D texture, Point FrameSize) : base(position, texture)
  15.         {
  16.             this.sourceRectangle.Width = FrameSize.X;
  17.             sourceRectangle.Height = FrameSize.Y;
  18.             dictAnimation = new Dictionary<string, int[]>();
  19.             framePerSecond = 1;
  20.             currentFrame = 0;
  21.             animationIsFinish = false;
  22.         }
  23.  
  24.         Dictionary<string, int[]> dictAnimation;
  25.         string currentAnimation;
  26.         float framePerSecond;
  27.         int currentFrame;
  28.         float chrono;
  29.         bool isLooping;
  30.         bool animationIsFinish;
  31.  
  32.         public void AddAnimation(string animationName, int[] tabAnimation)
  33.         {
  34.             if (!dictAnimation.ContainsKey(animationName))
  35.             {
  36.                 dictAnimation.Add(animationName, tabAnimation);
  37.             }
  38.             else
  39.             {
  40.                 throw new Exception("L'animation " + animationName + " existe deja.");
  41.             }
  42.         }
  43.         public bool AnimationExist(string animationName)
  44.         {
  45.             return dictAnimation.ContainsKey(animationName);
  46.         }
  47.         public void PlayAnimation(string animationName, float framePerSecond = 1, bool isLooping = false)
  48.         {
  49.             animationIsFinish = false;
  50.             this.isLooping = isLooping;
  51.             currentFrame = 0;
  52.             this.framePerSecond = framePerSecond;
  53.             currentAnimation = animationName;
  54.  
  55.             if (!dictAnimation.ContainsKey(animationName))
  56.             {
  57.                 throw new Exception("L'animation " + animationName + " n'existe pas.");
  58.             }
  59.             UpdateFrame();
  60.         }
  61.  
  62.         private void UpdateFrame()
  63.         {
  64.             int nbFrameInTextureWidth = texture.Width / sourceRectangle.Width;
  65.             int positionY = dictAnimation[currentAnimation][currentFrame] / nbFrameInTextureWidth;
  66.  
  67.             sourceRectangle.Y = positionY * sourceRectangle.Height;
  68.  
  69.             int positionX = dictAnimation[currentAnimation][currentFrame] - positionY * nbFrameInTextureWidth;
  70.             sourceRectangle.X = positionX * sourceRectangle.Width;
  71.         }
  72.  
  73.         public override void Update(GameTime gameTime)
  74.         {
  75.             base.Update(gameTime);
  76.  
  77.             #region gestionAnimation
  78.             chrono += (float)gameTime.ElapsedGameTime.TotalSeconds;
  79.  
  80.             if (!animationIsFinish && chrono >= framePerSecond)
  81.             {
  82.                 if (currentFrame + 1 < dictAnimation[currentAnimation].Length)
  83.                 {
  84.                     currentFrame += 1;
  85.                     UpdateFrame();
  86.                 }
  87.                 else
  88.                 {
  89.                     if (isLooping)
  90.                     {
  91.                         currentFrame = 0;
  92.                         UpdateFrame();
  93.                     }
  94.                     else
  95.                     {
  96.                         animationIsFinish = true;
  97.                     }
  98.                 }
  99.                 chrono = 0;
  100.             }
  101.             #endregion
  102.         }
  103.  
  104.         public override void Draw(SpriteBatch spriteBatch)
  105.         {
  106.             base.Draw(spriteBatch);
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement