Advertisement
Necr0

MGE Animation System

Dec 11th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MonoGame.Extended.TextureAtlases;
  7. using Microsoft.Xna.Framework;
  8.  
  9. namespace TheLegendOfZelda.Game.Animation
  10. {
  11.  
  12.     class TAAnimationController
  13.     {
  14.         public TextureAtlas atlas;
  15.         bool paused = false;
  16.         Dictionary<string, FrameAnimation> animations = new Dictionary<string, FrameAnimation>();
  17.         FrameAnimation loopAnimation = FrameAnimation.DEFAULT;
  18.         FrameAnimation overrideAnimation = null;
  19.  
  20.         int currentFrame = 0;
  21.  
  22.         public TAAnimationController(TextureAtlas atlas)
  23.         {
  24.             this.atlas = atlas;
  25.         }
  26.  
  27.         public void AddAnimation(string name, FrameAnimation animation) { animations.Add(name, animation); }
  28.         public void AddAnimation(string name, params Frame[] frames) { AddAnimation(name, new FrameAnimation(frames)); }
  29.  
  30.         public void Update(GameTime delta) { Update(delta.ElapsedGameTime.TotalMilliseconds); }
  31.         public void Update(double delta)
  32.         {
  33.             if (paused)
  34.                 return;
  35.             //Update
  36.             if (overrideAnimation != null)
  37.                 overrideAnimation.Update(delta);
  38.             if (loopAnimation != null)
  39.                 loopAnimation.Update(delta);
  40.             else
  41.                 loopAnimation = FrameAnimation.DEFAULT;
  42.  
  43.             //Check If Animation Is Over
  44.             if (overrideAnimation != null && overrideAnimation.GetFrame() == -1)
  45.                 overrideAnimation = null;
  46.             if (loopAnimation.GetFrame() == -1)
  47.                 loopAnimation = FrameAnimation.DEFAULT;
  48.  
  49.             //set frame
  50.             if (overrideAnimation != null)
  51.                 currentFrame = overrideAnimation.GetFrame();
  52.             else
  53.                 currentFrame = loopAnimation.GetFrame();
  54.         }
  55.  
  56.         public void Pause() { this.paused = true; }
  57.         public void Resume() { this.paused = false; }
  58.         public void Play(String animation, bool force_override = false)
  59.         {
  60.             FrameAnimation a = animations[animation];
  61.             a.Reset();
  62.             if (!force_override && a.loop)
  63.                 this.loopAnimation = a;
  64.             else
  65.                 this.overrideAnimation = a;
  66.             this.Update(0);
  67.         }
  68.         public void Stop() { this.overrideAnimation = null; }
  69.         public void StopAll() { Stop(); this.loopAnimation = FrameAnimation.DEFAULT; }
  70.  
  71.         public int GetFrame() { return currentFrame; }
  72.         public TextureRegion2D GetTextureRegion()
  73.         {
  74.             return atlas.GetRegion(GetFrame());
  75.         }
  76.     }
  77.  
  78.     class TAAnimationControllerFactory {
  79.  
  80.         public TextureAtlas atlas;
  81.         Dictionary<string, FrameAnimationFactory> animations = new Dictionary<string, FrameAnimationFactory>();
  82.  
  83.         public TAAnimationControllerFactory(TextureAtlas atlas)
  84.         {
  85.             this.atlas = atlas;
  86.         }
  87.  
  88.         public void AddAnimation(string name, FrameAnimationFactory animation) { animations.Add(name, animation); }
  89.         public void AddAnimation(string name, FrameAnimation animation) { animations.Add(name, new FrameAnimationFactory(animation.loop,animation.frames)); }
  90.         public void AddAnimation(string name, params Frame[] frames) { AddAnimation(name, new FrameAnimationFactory(frames)); }
  91.  
  92.         public TAAnimationController Create() {
  93.             var ret = new TAAnimationController(atlas);
  94.             foreach (string name in animations.Keys) {
  95.                 ret.AddAnimation(name,animations[name].Create());
  96.             }
  97.             return ret;
  98.         }
  99.     }
  100.  
  101.     class FrameAnimation
  102.     {
  103.         public static readonly FrameAnimation DEFAULT = new FrameAnimation(0);
  104.  
  105.         public double totalLength = 0d;
  106.         public bool loop;
  107.         public Frame[] frames;
  108.  
  109.         double currentTime = 0;
  110.         int currentFrame = 0;
  111.  
  112.         public FrameAnimation(params Frame[] frames) : this(true, frames) { }
  113.         public FrameAnimation(bool loop, params Frame[] frames)
  114.         {
  115.             this.loop = loop;
  116.             this.frames = frames;
  117.             foreach (Frame f in frames)
  118.             {
  119.                 totalLength += f.duration;
  120.             }
  121.             if (totalLength <= 0)
  122.                 throw new Exception("Zero Length Animation Exception");
  123.             if (frames.Length <= 0)
  124.                 throw new Exception("Frameless Animation Exception");
  125.             this.currentFrame = frames[0].index;
  126.         }
  127.  
  128.         public void Update(double delta)
  129.         {
  130.             currentTime += delta;
  131.             if (loop)
  132.                 currentTime %= totalLength;
  133.  
  134.             double seek_head = 0d;
  135.  
  136.             foreach (Frame f in frames)
  137.             {
  138.                 seek_head += f.duration;
  139.                 if (seek_head >= currentTime)
  140.                 {
  141.                     currentFrame = f.index;
  142.                     return;
  143.                 }
  144.             }
  145.             currentFrame = -1;
  146.         }
  147.  
  148.         public int GetFrame()
  149.         {
  150.             return currentFrame;
  151.         }
  152.  
  153.         public void Reset()
  154.         {
  155.             currentTime = 0d;
  156.             this.currentFrame = frames[0].index;
  157.         }
  158.     }
  159.  
  160.     class FrameAnimationFactory
  161.     {
  162.         public bool loop;
  163.         public Frame[] frames;
  164.  
  165.         public FrameAnimationFactory(params Frame[] frames) : this(true, frames) { }
  166.         public FrameAnimationFactory(bool loop, params Frame[] frames)
  167.         {
  168.             this.loop = loop;
  169.             this.frames = frames;
  170.         }
  171.  
  172.         public FrameAnimation Create()
  173.         {
  174.             return new FrameAnimation(loop, frames);
  175.         }
  176.     }
  177.  
  178.     struct Frame
  179.     {
  180.         const double DEFAULT_FRAME_TIME = 100.0d;
  181.         public double duration;
  182.         public int index;
  183.  
  184.         public Frame(int index, double duration = DEFAULT_FRAME_TIME)
  185.         {
  186.             this.index = index;
  187.             this.duration = duration;
  188.         }
  189.  
  190.         public static implicit operator Frame(int i)
  191.         {
  192.             return new Frame(i);
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement