Advertisement
dallonz88

AnimatedTexture.cs

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