Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MonoGame.Extended.TextureAtlases;
- using Microsoft.Xna.Framework;
- namespace TheLegendOfZelda.Game.Animation
- {
- class TAAnimationController
- {
- public TextureAtlas atlas;
- bool paused = false;
- Dictionary<string, FrameAnimation> animations = new Dictionary<string, FrameAnimation>();
- FrameAnimation loopAnimation = FrameAnimation.DEFAULT;
- FrameAnimation overrideAnimation = null;
- int currentFrame = 0;
- public TAAnimationController(TextureAtlas atlas)
- {
- this.atlas = atlas;
- }
- public void AddAnimation(string name, FrameAnimation animation) { animations.Add(name, animation); }
- public void AddAnimation(string name, params Frame[] frames) { AddAnimation(name, new FrameAnimation(frames)); }
- public void Update(GameTime delta) { Update(delta.ElapsedGameTime.TotalMilliseconds); }
- public void Update(double delta)
- {
- if (paused)
- return;
- //Update
- if (overrideAnimation != null)
- overrideAnimation.Update(delta);
- if (loopAnimation != null)
- loopAnimation.Update(delta);
- else
- loopAnimation = FrameAnimation.DEFAULT;
- //Check If Animation Is Over
- if (overrideAnimation != null && overrideAnimation.GetFrame() == -1)
- overrideAnimation = null;
- if (loopAnimation.GetFrame() == -1)
- loopAnimation = FrameAnimation.DEFAULT;
- //set frame
- if (overrideAnimation != null)
- currentFrame = overrideAnimation.GetFrame();
- else
- currentFrame = loopAnimation.GetFrame();
- }
- public void Pause() { this.paused = true; }
- public void Resume() { this.paused = false; }
- public void Play(String animation, bool force_override = false)
- {
- FrameAnimation a = animations[animation];
- a.Reset();
- if (!force_override && a.loop)
- this.loopAnimation = a;
- else
- this.overrideAnimation = a;
- this.Update(0);
- }
- public void Stop() { this.overrideAnimation = null; }
- public void StopAll() { Stop(); this.loopAnimation = FrameAnimation.DEFAULT; }
- public int GetFrame() { return currentFrame; }
- public TextureRegion2D GetTextureRegion()
- {
- return atlas.GetRegion(GetFrame());
- }
- }
- class TAAnimationControllerFactory {
- public TextureAtlas atlas;
- Dictionary<string, FrameAnimationFactory> animations = new Dictionary<string, FrameAnimationFactory>();
- public TAAnimationControllerFactory(TextureAtlas atlas)
- {
- this.atlas = atlas;
- }
- public void AddAnimation(string name, FrameAnimationFactory animation) { animations.Add(name, animation); }
- public void AddAnimation(string name, FrameAnimation animation) { animations.Add(name, new FrameAnimationFactory(animation.loop,animation.frames)); }
- public void AddAnimation(string name, params Frame[] frames) { AddAnimation(name, new FrameAnimationFactory(frames)); }
- public TAAnimationController Create() {
- var ret = new TAAnimationController(atlas);
- foreach (string name in animations.Keys) {
- ret.AddAnimation(name,animations[name].Create());
- }
- return ret;
- }
- }
- class FrameAnimation
- {
- public static readonly FrameAnimation DEFAULT = new FrameAnimation(0);
- public double totalLength = 0d;
- public bool loop;
- public Frame[] frames;
- double currentTime = 0;
- int currentFrame = 0;
- public FrameAnimation(params Frame[] frames) : this(true, frames) { }
- public FrameAnimation(bool loop, params Frame[] frames)
- {
- this.loop = loop;
- this.frames = frames;
- foreach (Frame f in frames)
- {
- totalLength += f.duration;
- }
- if (totalLength <= 0)
- throw new Exception("Zero Length Animation Exception");
- if (frames.Length <= 0)
- throw new Exception("Frameless Animation Exception");
- this.currentFrame = frames[0].index;
- }
- public void Update(double delta)
- {
- currentTime += delta;
- if (loop)
- currentTime %= totalLength;
- double seek_head = 0d;
- foreach (Frame f in frames)
- {
- seek_head += f.duration;
- if (seek_head >= currentTime)
- {
- currentFrame = f.index;
- return;
- }
- }
- currentFrame = -1;
- }
- public int GetFrame()
- {
- return currentFrame;
- }
- public void Reset()
- {
- currentTime = 0d;
- this.currentFrame = frames[0].index;
- }
- }
- class FrameAnimationFactory
- {
- public bool loop;
- public Frame[] frames;
- public FrameAnimationFactory(params Frame[] frames) : this(true, frames) { }
- public FrameAnimationFactory(bool loop, params Frame[] frames)
- {
- this.loop = loop;
- this.frames = frames;
- }
- public FrameAnimation Create()
- {
- return new FrameAnimation(loop, frames);
- }
- }
- struct Frame
- {
- const double DEFAULT_FRAME_TIME = 100.0d;
- public double duration;
- public int index;
- public Frame(int index, double duration = DEFAULT_FRAME_TIME)
- {
- this.index = index;
- this.duration = duration;
- }
- public static implicit operator Frame(int i)
- {
- return new Frame(i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement