Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- namespace TileEngine
- {
- public class FrameAnimation : ICloneable
- {
- Rectangle[] frames;
- int currentFrame = 0;
- float frameLenght = 0.5f;
- float timer = 0;
- public float FramesPerSecond
- {
- get
- {
- return 1f / frameLenght;
- }
- set
- {
- frameLenght = (float)Math.Max(1f / (float)value, .001f);
- }
- }
- public Rectangle CurrentRect
- {
- get { return frames[currentFrame]; }
- }
- public int CurrentFrame
- {
- get { return currentFrame; }
- set
- {
- currentFrame = (int)MathHelper.Clamp(value, 0, frames.Length - 1);
- }
- }
- public FrameAnimation(
- int numberOfFrames,
- int frameWidth,
- int frameHeight,
- int xOffset,
- int yOffset)
- {
- frames = new Rectangle[numberOfFrames];
- for (int i = 0; i < numberOfFrames; i++)
- {
- Rectangle rect = new Rectangle();
- rect.Width = frameWidth;
- rect.Height = frameHeight;
- rect.X = xOffset + (i * frameWidth);
- rect.Y = yOffset;
- frames[i] = rect;
- }
- }
- private FrameAnimation()
- {
- }
- public void Update(GameTime gameTime)
- {
- timer = +(float)gameTime.ElapsedGameTime.TotalSeconds;
- if (timer >= frameLenght)
- {
- timer = 0f;
- currentFrame++;
- if (currentFrame >= frames.Length)
- currentFrame = 0;
- }
- }
- public object Clone()
- {
- FrameAnimation anim = new FrameAnimation();
- anim.frameLenght = frameLenght;
- anim.frames = frames;
- return anim;
- }
- }
- }
Add Comment
Please, Sign In to add comment