123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace Gemstone_Hunter
- {
- public class AnimationStrip
- {
- #region Declarations
- private Texture2D texture;
- private int frameWidth;
- private int frameHeight;
- private float frameTimer = 0f;
- private float frameDelay = 0.05f;
- private int currentFrame;
- private bool loopAnimation = true;
- private bool finishedPlaying = false;
- private string name;
- private string nextAnimation;
- #endregion
- #region Properties
- public int FrameWidth
- {
- get { return frameWidth; }
- set { frameWidth = value; }
- }
- public int FrameHeight
- {
- get { return frameHeight; }
- set { frameHeight = value; }
- }
- public Texture2D Texture
- {
- get { return texture; }
- set { texture = value; }
- }
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public string NextAnimation
- {
- get { return nextAnimation; }
- set { nextAnimation = value; }
- }
- public bool LoopAnimation
- {
- get { return loopAnimation; }
- set { loopAnimation = value; }
- }
- public bool FinishedPlaying
- {
- get { return finishedPlaying; }
- }
- public int FrameCount
- {
- get { return texture.Width / frameWidth; }
- }
- public float FrameLength
- {
- get { return frameDelay; }
- set { frameDelay = value; }
- }
- public Rectangle FrameRectangle
- {
- get
- {
- return new Rectangle(
- currentFrame * frameWidth,
- 0,
- frameWidth,
- frameHeight);
- }
- }
- #endregion
- #region Constructor
- public AnimationStrip(Texture2D texture, int frameWidth, string name)
- {
- this.texture = texture;
- this.frameWidth = frameWidth;
- this.frameHeight = texture.Height;
- this.name = name;
- }
- #endregion
- #region Public Methods
- public void Play()
- {
- currentFrame = 0;
- finishedPlaying = false;
- }
- public void Update(GameTime gameTime)
- {
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- frameTimer += elapsed;
- if (frameTimer >= frameDelay)
- {
- currentFrame++;
- if (currentFrame >= FrameCount)
- {
- if (loopAnimation)
- {
- currentFrame = 0;
- }
- else
- {
- currentFrame = FrameCount - 1;
- finishedPlaying = true;
- }
- }
- frameTimer = 0f;
- }
- }
- #endregion
- }
- }
|