Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region File Description
- //-----------------------------------------------------------------------------
- // AnimatedTexture.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace GameTest
- {
- public class AnimatedTexture
- {
- private int framecount;
- public Texture2D myTexture;
- private float TimePerFrame;
- private int Frame;
- private float TotalElapsed;
- private bool Paused;
- public float Rotation, Scale, Depth;
- public Vector2 Origin, Position;
- public AnimatedTexture(Vector2 origin, float rotation, float scale, float depth)
- {
- this.Origin = origin;
- this.Position = origin;
- this.Rotation = rotation;
- this.Scale = scale;
- this.Depth = depth;
- }
- public void Load(ContentManager content, string asset, int frameCount, int framesPerSec)
- {
- framecount = frameCount;
- myTexture = content.Load<Texture2D>(asset);
- TimePerFrame = (float)1 / framesPerSec;
- Frame = 0;
- TotalElapsed = 0;
- Paused = false;
- }
- public void UpdateFrame(float elapsed)
- {
- if (Paused)
- return;
- TotalElapsed += elapsed;
- if (TotalElapsed > TimePerFrame)
- {
- Frame++;
- // Keep the Frame between 0 and the total frames, minus one.
- Frame = Frame % framecount;
- TotalElapsed -= TimePerFrame;
- }
- }
- public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
- {
- DrawFrame(batch, Frame, screenPos);
- }
- public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
- {
- Position = screenPos;
- int FrameWidth = myTexture.Width / framecount;
- Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0, FrameWidth, myTexture.Height);
- //batch.Draw(myTexture, Position, sourcerect, Color.White, Rotation, Position, Scale, SpriteEffects.None, Depth);
- batch.Draw(myTexture, Position, sourcerect, Color.White);
- }
- public bool IsPaused
- {
- get { return Paused; }
- }
- public void Reset()
- {
- Frame = 0;
- TotalElapsed = 0f;
- }
- public void Stop()
- {
- Pause();
- Reset();
- }
- public void Play()
- {
- Paused = false;
- }
- public void Pause()
- {
- Paused = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement