using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; class AnimationPlayer { protected Animation animation; protected int frameIndex; protected float time; protected bool mirror; public AnimationPlayer() { this.mirror = false; } public bool Mirror { get { return mirror; } set { mirror = value; } } public void playAnimation(Animation anim) { if (this.animation == anim) return; this.animation = anim; this.frameIndex = 0; this.time = 0.0f; } public Animation Animation { get { return animation; } } public void Update(GameTime gameTime) { time += (float)gameTime.ElapsedGameTime.TotalSeconds; while (time > animation.FrameTime) { time -= animation.FrameTime; if (animation.IsLooping) frameIndex = (frameIndex + 1) % animation.CountFrames; else frameIndex = Math.Min(frameIndex + 1, animation.CountFrames - 1); } } public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position) { SpriteEffects spriteEffects = SpriteEffects.None; if (mirror) spriteEffects = SpriteEffects.FlipHorizontally; spriteBatch.Draw(animation.Sprite, position, animation.calculateFrame(frameIndex), Color.White, 0.0f, animation.Origin, 1.0f, spriteEffects, 0.0f); } }