Guest User

Untitled

a guest
Apr 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 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 Star_Defense
  9. {
  10.     class AnimatedSprite
  11.     {
  12.         Texture2D t2dTexture;
  13.  
  14.         float fFrameRate = 0.02f;
  15.         float fElapsed = 0.0f;
  16.  
  17.         int iFrameOffsetX = 0;
  18.         int iFrameOffsetY = 0;
  19.         int iFrameWidth = 32;
  20.         int iFrameHeight = 32;
  21.  
  22.         int iFrameCount = 1;
  23.         int iCurrentFrame = 0;
  24.         int iScreenX = 0;
  25.         int iScreenY = 0;
  26.  
  27.         Color cTinting = Color.White;
  28.  
  29.         bool bAnimating = true;
  30.  
  31.         public int X
  32.         {
  33.             get { return iScreenX; }
  34.             set { iScreenX = value; }
  35.         }
  36.  
  37.         public int Y
  38.         {
  39.             get { return iScreenY; }
  40.             set { iScreenY = value; }
  41.         }
  42.  
  43.         public int Frame
  44.         {
  45.             get { return iCurrentFrame; }
  46.             set { iCurrentFrame = (int)MathHelper.Clamp(value, 0, iFrameCount); }
  47.         }
  48.  
  49.         public float FrameLength
  50.         {
  51.             get { return fFrameRate; }
  52.             set { fFrameRate = (float)Math.Max(value, 0f); }
  53.         }
  54.  
  55.         public bool IsAnimating
  56.         {
  57.             get { return bAnimating; }
  58.             set { bAnimating = value; }
  59.         }
  60.  
  61.         public Color Tint
  62.         {
  63.             get { return cTinting; }
  64.             set { cTinting = value; }
  65.         }
  66.  
  67.         public AnimatedSprite(
  68.             Texture2D texture,
  69.             int FrameOffsetX,
  70.             int FrameOffsetY,
  71.             int FrameWidth,
  72.             int FrameHeight,
  73.             int FrameCount)
  74.         {
  75.             t2dTexture = texture;
  76.             iFrameOffsetX = FrameOffsetX;
  77.             iFrameOffsetY = FrameOffsetY;
  78.             iFrameWidth = FrameWidth;
  79.             iFrameHeight = FrameHeight;
  80.             iFrameCount = FrameCount;
  81.         }
  82.  
  83.         public Rectangle GetSourceRect()
  84.         {
  85.             return new Rectangle(
  86.             iFrameOffsetX + (iFrameWidth * iCurrentFrame),
  87.             iFrameOffsetY,
  88.             iFrameWidth,
  89.             iFrameHeight);
  90.         }
  91.  
  92.         public void Update(GameTime gametime)
  93.         {
  94.             if (bAnimating)
  95.             {
  96.                 // Accumulate elapsed time...
  97.                 fElapsed += (float)gametime.ElapsedGameTime.TotalSeconds;
  98.  
  99.                 // Until it passes our frame length
  100.                 if (fElapsed > fFrameRate)
  101.                 {
  102.                     // Increment the current frame, wrapping back to 0 at iFrameCount
  103.                     iCurrentFrame = (iCurrentFrame + 1) % iFrameCount;
  104.  
  105.                     // Reset the elapsed frame time.
  106.                     fElapsed = 0.0f;
  107.                 }
  108.             }
  109.         }
  110.  
  111.         public void Draw(
  112.             SpriteBatch spriteBatch,
  113.             int XOffset,
  114.             int YOffset,
  115.             bool NeedBeginEnd)
  116.         {
  117.             if (NeedBeginEnd)
  118.                 spriteBatch.Begin();
  119.  
  120.             spriteBatch.Draw(
  121.                 t2dTexture,
  122.                 new Rectangle(
  123.                   iScreenX + XOffset,
  124.                   iScreenY + YOffset,
  125.                   iFrameWidth,
  126.                   iFrameHeight),
  127.                 GetSourceRect(),
  128.                 cTinting);
  129.  
  130.             if (NeedBeginEnd)
  131.                 spriteBatch.End();
  132.         }
  133.  
  134.         public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
  135.         {
  136.             Draw(spriteBatch, XOffset, YOffset, true);
  137.         }
  138.     }
  139. }
Add Comment
Please, Sign In to add comment