SHARE
TWEET

Untitled

a guest Apr 26th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3.  
  4. namespace ShootEmUp.Animations
  5. {
  6.     class Animation
  7.     {
  8.         public Texture2D GetSpriteSheet { get; }
  9.         public Rectangle GetSourceRectangle { get; set; }
  10.         public Point GetSpriteCount { get; private set; }
  11.  
  12.         private Point mySpriteSize;
  13.         private float myElapsedTime;
  14.         private float myTimeBetweenFrames;
  15.  
  16.         public Animation(Texture2D aSpriteSheet, Point someSpriteSize, Point someSpriteCount, float someTimeBetweenFrames)
  17.         {
  18.             GetSpriteSheet = aSpriteSheet;
  19.             mySpriteSize = someSpriteSize;
  20.             GetSpriteCount = someSpriteCount;
  21.             myTimeBetweenFrames = someTimeBetweenFrames;
  22.         }
  23.  
  24.         public void Update(GameTime someGameTime)
  25.         {
  26.             if (GetSpriteCount.X * GetSpriteCount.Y == 1)
  27.             {
  28.                 GetSourceRectangle = new Rectangle(0, 0, mySpriteSize.X, mySpriteSize.Y);
  29.                 return;
  30.             }
  31.  
  32.             int tempTargetSpriteIndex = GetTargetSpriteIndex(someGameTime);
  33.  
  34.             Point tempTargetImage = GetTargetImage(tempTargetSpriteIndex);
  35.             Point tempTargetImagePosition = new Point(tempTargetImage.X * mySpriteSize.X, tempTargetImage.Y * mySpriteSize.Y);
  36.  
  37.             Rectangle tempTargetRectangle = new Rectangle(tempTargetImagePosition.X, tempTargetImagePosition.Y, mySpriteSize.X, mySpriteSize.Y);
  38.  
  39.             GetSourceRectangle = tempTargetRectangle;
  40.         }
  41.  
  42.         private int GetTargetSpriteIndex(GameTime someGameTime)
  43.         {
  44.             int tempTargetSpriteIndex = 0;
  45.  
  46.             do
  47.             {
  48.                 tempTargetSpriteIndex = (int)(myElapsedTime / (myTimeBetweenFrames <= 0 ? 1 : myTimeBetweenFrames));
  49.                 myElapsedTime += (float)someGameTime.ElapsedGameTime.TotalSeconds;
  50.  
  51.                 if (tempTargetSpriteIndex >= GetSpriteCount.X * GetSpriteCount.Y)
  52.                 {
  53.                     myElapsedTime = 0;
  54.                 }
  55.  
  56.             } while (tempTargetSpriteIndex >= GetSpriteCount.X * GetSpriteCount.Y);
  57.  
  58.             return tempTargetSpriteIndex;
  59.         }
  60.  
  61.         private Point GetTargetImage(int someTargetSpriteIndex)
  62.         {
  63.             return new Point(someTargetSpriteIndex % GetSpriteCount.X, (int)(someTargetSpriteIndex / GetSpriteCount.X));
  64.         }
  65.     }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top