Created
May 11, 2017 12:47
-
-
Save Maybeking/03d85a07a0905d3449e25c59adb94f6b to your computer and use it in GitHub Desktop.
Animating sprites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Frame | |
{ | |
public Texture2D Texture; | |
public Rectangle? TextureClip; | |
public Vector2 LocalTextureTranslate; | |
public Transform Hitbox; | |
} | |
public class FramesManager | |
{ | |
public string Id { get; private set; } | |
public List<Frame> Frames; | |
public float DeltaTimeFrame; | |
public int CurrentFrameIndex; | |
private float currentDeltaTime; | |
public Frame CurrentFrame | |
{ | |
get { return Frames[CurrentFrameIndex]; } | |
} | |
public FramesManager(string id, float deltaTimeFrame) | |
{ | |
Id = id; | |
DeltaTimeFrame = deltaTimeFrame; | |
currentDeltaTime = 0; | |
CurrentFrameIndex = 0; | |
} | |
public void Update() | |
{ | |
currentDeltaTime += GameCore.DeltaTime; | |
if (currentDeltaTime >= DeltaTimeFrame) | |
{ | |
NextFrame(); | |
currentDeltaTime = 0; | |
} | |
} | |
public void NextFrame() | |
{ | |
if (CurrentFrameIndex + 1 >= Frames.Count) | |
{ | |
CurrentFrameIndex = 0; | |
} | |
else | |
{ | |
CurrentFrameIndex++; | |
} | |
} | |
} | |
public class Animation : Sprite | |
{ | |
private Dictionary<string, FramesManager> _animations; | |
private FramesManager _currentAnimation; | |
public FramesManager CurrentAnimation | |
{ | |
get { return _currentAnimation; } | |
} | |
public string CurrentAnimationId | |
{ | |
get | |
{ | |
if (_currentAnimation.IsNull()) | |
{ | |
return string.Empty; | |
} | |
else | |
{ | |
return _currentAnimation.Id; | |
} | |
} | |
} | |
private string _nextAnimation; | |
public Animation(int zIndex = 0) : base(default(Texture2D), true, zIndex) | |
{ | |
Active = true; | |
_animations = new Dictionary<string, FramesManager>(); | |
} | |
public override void Update() | |
{ | |
base.Update(); | |
if (_nextAnimation != CurrentAnimationId) | |
{ | |
_currentAnimation = _animations[_nextAnimation]; | |
} | |
if (_currentAnimation.IsNotNull()) | |
{ | |
_currentAnimation.Update(); | |
} | |
} | |
public void AddAnimation(string id, FramesManager frames) | |
{ | |
if (!_animations.ContainsKey(id)) | |
{ | |
_animations.Add(id, frames); | |
} | |
} | |
public void AddAnimation(string id, float delta, params Frame[] frames) | |
{ | |
if (!_animations.ContainsKey(id)) | |
{ | |
_animations.Add(id, new FramesManager(id, delta) { Frames = new List<Frame>(frames) }); | |
} | |
} | |
public void RemoveAnimation(string id) | |
{ | |
if (_animations.ContainsKey(id) && _currentAnimation.Id != id) | |
{ | |
_animations.Remove(id); | |
} | |
} | |
public void ChangeAnimation(string id) | |
{ | |
if (_animations.ContainsKey(id)) | |
{ | |
_nextAnimation = id; | |
} | |
} | |
public void Draw() | |
{ | |
spriteBatch.Draw( | |
_currentAnimation.CurrentFrame.Texture, | |
TexturePosition + drawable.TextureLocalTranslate, | |
_currentAnimation.CurrentFrame.TextureClip, | |
Color.White); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment