| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Tutorial030.Models;
- namespace Tutorial030.Managers
- {
- public class AnimationManager
- {
- private Animation _animation;
- private float _timer;
- private bool _updated;
- public int FrameWidth
- {
- get
- {
- return _animation.FrameWidth;
- }
- }
- public int FrameHeight
- {
- get
- {
- return _animation.FrameHeight;
- }
- }
- public Vector2 Position { get; set; }
- public float Layer { get; set; }
- public AnimationManager(Animation animation)
- {
- _animation = animation;
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- if (!_updated)
- throw new Exception("Need to call 'Update' first");
- _updated = false;
- spriteBatch.Draw(_animation.Texture,
- Position,
- new Rectangle(_animation.CurrentFrame * _animation.FrameWidth,
- 0,
- _animation.FrameWidth,
- _animation.FrameHeight),
- Color.White,
- 0f,
- new Vector2(0, 0),
- 1f,
- SpriteEffects.None,
- Layer);
- }
- public void Play(Animation animation)
- {
- if (_animation == animation)
- return;
- _animation = animation;
- _animation.CurrentFrame = 0;
- _timer = 0;
- }
- public void Stop()
- {
- _timer = 0f;
- _animation.CurrentFrame = 0;
- }
- public void Update(GameTime gameTime)
- {
- _updated = true;
- _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
- if (_timer > _animation.FrameSpeed)
- {
- _timer = 0f;
- _animation.CurrentFrame++;
- if (_animation.CurrentFrame >= _animation.FrameCount)
- _animation.CurrentFrame = 0;
- }
- }
- }
- }
|