Advertisement
maxp0wer789

Untitled

Oct 9th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7.  
  8. namespace AnimatedSprite
  9. {
  10.     public class AnimatedTexture
  11.     {
  12.         private int framecount;
  13.         private Texture2D myTexture;
  14.         private float TimePerFrame;
  15.         private int Frame;
  16.         private float TotalElapsed;
  17.         private bool Paused;
  18.  
  19.         public float Rotation, Scale, Depth;
  20.         public Vector2 Origin;
  21.         public AnimatedTexture(Vector2 origin, float rotation,
  22.             float scale, float depth)
  23.         {
  24.             this.Origin = origin;
  25.             this.Rotation = rotation;
  26.             this.Scale = scale;
  27.             this.Depth = depth;
  28.         }
  29.         public void Load(ContentManager content, string asset,
  30.             int frameCount, int framesPerSec)
  31.         {
  32.             framecount = frameCount;
  33.             myTexture = content.Load<Texture2D>(asset);
  34.             TimePerFrame = (float)1 / framesPerSec;
  35.             Frame = 0;
  36.             TotalElapsed = 0;
  37.             Paused = false;
  38.         }
  39.  
  40.         // class AnimatedTexture
  41.         public void UpdateFrame(float elapsed)
  42.         {
  43.             if (Paused)
  44.                 return;
  45.             TotalElapsed += elapsed;
  46.             if (TotalElapsed > TimePerFrame)
  47.             {
  48.                 Frame++;
  49.                 // Keep the Frame between 0 and the total frames, minus one.
  50.                 Frame = Frame % framecount;
  51.                 TotalElapsed -= TimePerFrame;
  52.             }
  53.         }
  54.  
  55.         // class AnimatedTexture
  56.         public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
  57.         {
  58.             DrawFrame(batch, Frame, screenPos);
  59.         }
  60.         public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
  61.         {
  62.             int FrameWidth = myTexture.Width / framecount;
  63.             Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0,
  64.                 FrameWidth, myTexture.Height);
  65.             batch.Draw(myTexture, screenPos, sourcerect, Color.White,
  66.                 Rotation, Origin, Scale, SpriteEffects.None, Depth);
  67.         }
  68.  
  69.         public bool IsPaused
  70.         {
  71.             get { return Paused; }
  72.         }
  73.         public void Reset()
  74.         {
  75.             Frame = 0;
  76.             TotalElapsed = 0f;
  77.         }
  78.         public void Stop()
  79.         {
  80.             Pause();
  81.             Reset();
  82.         }
  83.         public void Play()
  84.         {
  85.             Paused = false;
  86.         }
  87.         public void Pause()
  88.         {
  89.             Paused = true;
  90.         }
  91.  
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement