Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 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.  
  7. namespace TileEngine
  8. {
  9.     public class FrameAnimation : ICloneable
  10.     {
  11.         Rectangle[] frames;
  12.         int currentFrame = 0;
  13.  
  14.         float frameLenght = 0.5f;
  15.         float timer = 0;
  16.  
  17.         public float FramesPerSecond
  18.         {
  19.             get
  20.             {
  21.                 return 1f / frameLenght;
  22.             }
  23.             set
  24.             {
  25.                 frameLenght = (float)Math.Max(1f / (float)value, .001f);
  26.             }
  27.         }
  28.  
  29.         public Rectangle CurrentRect
  30.         {
  31.             get { return frames[currentFrame]; }
  32.         }
  33.         public int CurrentFrame
  34.         {
  35.             get { return currentFrame; }
  36.             set
  37.             {
  38.                 currentFrame = (int)MathHelper.Clamp(value, 0, frames.Length - 1);
  39.             }
  40.         }
  41.  
  42.         public FrameAnimation(
  43.             int numberOfFrames,
  44.             int frameWidth,
  45.             int frameHeight,
  46.             int xOffset,
  47.             int yOffset)
  48.         {
  49.             frames = new Rectangle[numberOfFrames];
  50.  
  51.             for (int i = 0; i < numberOfFrames; i++)
  52.             {
  53.                 Rectangle rect = new Rectangle();
  54.                 rect.Width = frameWidth;
  55.                 rect.Height = frameHeight;
  56.                 rect.X = xOffset + (i * frameWidth);
  57.                 rect.Y = yOffset;
  58.  
  59.                 frames[i] = rect;
  60.             }
  61.         }
  62.  
  63.         private FrameAnimation()
  64.         {
  65.  
  66.         }
  67.  
  68.         public void Update(GameTime gameTime)
  69.         {
  70.             timer = +(float)gameTime.ElapsedGameTime.TotalSeconds;
  71.  
  72.             if (timer >= frameLenght)
  73.             {
  74.                 timer = 0f;
  75.  
  76.                 currentFrame++;
  77.                 if (currentFrame >= frames.Length)
  78.                     currentFrame = 0;
  79.             }
  80.         }
  81.  
  82.         public object Clone()
  83.         {
  84.             FrameAnimation anim = new FrameAnimation();
  85.  
  86.             anim.frameLenght = frameLenght;
  87.             anim.frames = frames;
  88.  
  89.             return anim;
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment