Advertisement
et1337

Animation.cs

Dec 30th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace Lemma.Components
  8. {
  9.     public class Animation : Component, IUpdateableComponent
  10.     {
  11.         public abstract class Base
  12.         {
  13.             public bool Done { get; protected set; }
  14.             public abstract void Reset();
  15.             public abstract void Update(float dt);
  16.         }
  17.  
  18.         public abstract class Interval : Base
  19.         {
  20.             public float Duration { get; private set; }
  21.             private float time;
  22.  
  23.             public Interval(float duration)
  24.             {
  25.                 this.Duration = duration;
  26.             }
  27.  
  28.             public override void Reset()
  29.             {
  30.                 this.time = 0.0f;
  31.                 this.Done = false;
  32.             }
  33.  
  34.             public sealed override void Update(float dt)
  35.             {
  36.                 this.time += dt;
  37.                 this.UpdateInterval(Math.Min(this.time / this.Duration, 1.0f));
  38.                 this.Done = this.time > this.Duration;
  39.             }
  40.  
  41.             public abstract void UpdateInterval(float x);
  42.         }
  43.  
  44.         public abstract class Move<T> : Interval
  45.         {
  46.             protected Property<T> property;
  47.             protected T start;
  48.             protected T parameter;
  49.  
  50.             public Move(Property<T> p, T t, float duration)
  51.                 : base(duration)
  52.             {
  53.                 this.property = p;
  54.                 this.start = p;
  55.                 this.parameter = t;
  56.             }
  57.         }
  58.  
  59.         public class Vector2MoveTo : Move<Vector2>
  60.         {
  61.             public Vector2MoveTo(Property<Vector2> p, Vector2 t, float duration)
  62.                 : base(p, t, duration)
  63.             {
  64.  
  65.             }
  66.  
  67.             public override void UpdateInterval(float x)
  68.             {
  69.                 this.property.Value = this.start + (this.parameter - this.start) * x;
  70.             }
  71.         }
  72.  
  73.         public class Vector2MoveBy : Move<Vector2>
  74.         {
  75.             public Vector2MoveBy(Property<Vector2> p, Vector2 t, float duration)
  76.                 : base(p, t, duration)
  77.             {
  78.  
  79.             }
  80.  
  81.             public override void UpdateInterval(float x)
  82.             {
  83.                 this.property.Value = this.start + this.parameter * x;
  84.             }
  85.         }
  86.  
  87.         public class Vector3MoveTo : Move<Vector3>
  88.         {
  89.             public Vector3MoveTo(Property<Vector3> p, Vector3 t, float duration)
  90.                 : base(p, t, duration)
  91.             {
  92.  
  93.             }
  94.  
  95.             public override void UpdateInterval(float x)
  96.             {
  97.                 this.property.Value = this.start + (this.parameter - this.start) * x;
  98.             }
  99.         }
  100.  
  101.         public class Vector3MoveBy : Move<Vector3>
  102.         {
  103.             public Vector3MoveBy(Property<Vector3> p, Vector3 t, float duration)
  104.                 : base(p, t, duration)
  105.             {
  106.  
  107.             }
  108.  
  109.             public override void UpdateInterval(float x)
  110.             {
  111.                 this.property.Value = this.start + this.parameter * x;
  112.             }
  113.         }
  114.  
  115.         public class Vector4MoveTo : Move<Vector4>
  116.         {
  117.             public Vector4MoveTo(Property<Vector4> p, Vector4 t, float duration)
  118.                 : base(p, t, duration)
  119.             {
  120.  
  121.             }
  122.  
  123.             public override void UpdateInterval(float x)
  124.             {
  125.                 this.property.Value = this.start + (this.parameter - this.start) * x;
  126.             }
  127.         }
  128.  
  129.         public class ColorMoveTo : Move<Color>
  130.         {
  131.             public ColorMoveTo(Property<Color> p, Color t, float duration)
  132.                 : base(p, t, duration)
  133.             {
  134.  
  135.             }
  136.  
  137.             public override void UpdateInterval(float x)
  138.             {
  139.                 this.property.Value = new Color(this.start.ToVector4() + (this.parameter.ToVector4() - this.start.ToVector4()) * x);
  140.             }
  141.         }
  142.  
  143.         public class Vector4MoveBy : Move<Vector4>
  144.         {
  145.             public Vector4MoveBy(Property<Vector4> p, Vector4 t, float duration)
  146.                 : base(p, t, duration)
  147.             {
  148.  
  149.             }
  150.  
  151.             public override void UpdateInterval(float x)
  152.             {
  153.                 this.property.Value = this.start + this.parameter * x;
  154.             }
  155.         }
  156.  
  157.         public class FloatMoveTo : Move<float>
  158.         {
  159.             public FloatMoveTo(Property<float> p, float t, float duration)
  160.                 : base(p, t, duration)
  161.             {
  162.  
  163.             }
  164.  
  165.             public override void UpdateInterval(float x)
  166.             {
  167.                 this.property.Value = this.start + (this.parameter - this.start) * x;
  168.             }
  169.         }
  170.  
  171.         public class FloatMoveBy : Move<float>
  172.         {
  173.             public FloatMoveBy(Property<float> p, float t, float duration)
  174.                 : base(p, t, duration)
  175.             {
  176.  
  177.             }
  178.  
  179.             public override void UpdateInterval(float x)
  180.             {
  181.                 this.property.Value = this.start + this.parameter * x;
  182.             }
  183.         }
  184.  
  185.         public class Delay : Interval
  186.         {
  187.             public Delay(float duration)
  188.                 : base(duration)
  189.             {
  190.  
  191.             }
  192.  
  193.             public override void UpdateInterval(float x)
  194.             {
  195.                 // Do nothing!
  196.             }
  197.         }
  198.  
  199.         public class Execute : Interval
  200.         {
  201.             private Action action;
  202.             private bool executed;
  203.  
  204.             public Execute(Action action)
  205.                 : base(0)
  206.             {
  207.                 this.action = action;
  208.             }
  209.  
  210.             public override void Reset()
  211.             {
  212.                 base.Reset();
  213.                 this.executed = false;
  214.             }
  215.  
  216.             public override void UpdateInterval(float x)
  217.             {
  218.                 if (!this.executed)
  219.                     this.action();
  220.                 this.executed = true;
  221.             }
  222.         }
  223.  
  224.         public class Parallel : Interval
  225.         {
  226.             private bool[] finished;
  227.             private Interval[] intervals;
  228.  
  229.             public Parallel(params Interval[] intervals)
  230.                 : base(intervals.Max(x => x.Duration))
  231.             {
  232.                 this.intervals = intervals;
  233.                 this.finished = new bool[this.intervals.Length];
  234.             }
  235.  
  236.             public override void Reset()
  237.             {
  238.                 base.Reset();
  239.                 foreach (Interval i in this.intervals)
  240.                     i.Reset();
  241.                 for (int i = 0; i < this.finished.Length; i++)
  242.                     this.finished[i] = false;
  243.             }
  244.  
  245.             public override void UpdateInterval(float x)
  246.             {
  247.                 float time = x * this.Duration;
  248.                 for (int i = 0; i < this.intervals.Length; i++)
  249.                 {
  250.                     Interval interval = this.intervals[i];
  251.                     float intervalTime = time / interval.Duration;
  252.                     if (intervalTime >= 1.0f)
  253.                     {
  254.                         if (!this.finished[i])
  255.                         {
  256.                             interval.UpdateInterval(1.0f);
  257.                             this.finished[i] = true;
  258.                         }
  259.                     }
  260.                     else
  261.                         interval.UpdateInterval(intervalTime);
  262.                 }
  263.             }
  264.         }
  265.  
  266.         public class Sequence : Interval
  267.         {
  268.             private float[] criticalPoints;
  269.             private Interval[] intervals;
  270.             private int currentIndex;
  271.  
  272.             public Sequence(params Interval[] intervals)
  273.                 : base(intervals.Sum(x => x.Duration))
  274.             {
  275.                 this.intervals = intervals;
  276.                 this.criticalPoints = new float[this.intervals.Length];
  277.                 float total = 0.0f;
  278.                 int i = 0;
  279.                 foreach (Interval interval in this.intervals)
  280.                 {
  281.                     this.criticalPoints[i] = total;
  282.                     total += interval.Duration / this.Duration;
  283.                     i++;
  284.                 }
  285.             }
  286.  
  287.             public override void Reset()
  288.             {
  289.                 base.Reset();
  290.                 this.currentIndex = 0;
  291.                 foreach (Interval i in this.intervals)
  292.                     i.Reset();
  293.             }
  294.  
  295.             public override void UpdateInterval(float x)
  296.             {
  297.                 // Advance the index if necessary
  298.                 int maxIndex = this.intervals.Length - 1;
  299.                 if (this.currentIndex < maxIndex)
  300.                 {
  301.                     while (x >= this.criticalPoints[this.currentIndex + 1])
  302.                     {
  303.                         this.intervals[this.currentIndex].UpdateInterval(1.0f);
  304.                         this.currentIndex++;
  305.                         if (this.currentIndex == maxIndex)
  306.                             break;
  307.                     }
  308.                 }
  309.  
  310.                 // Retreat the index if necessary
  311.                 if (this.currentIndex > 0)
  312.                 {
  313.                     while (x < this.criticalPoints[this.currentIndex])
  314.                     {
  315.                         this.intervals[this.currentIndex].UpdateInterval(0.0f);
  316.                         this.currentIndex--;
  317.                     }
  318.                 }
  319.  
  320.                 // Update the current interval
  321.                 float lastCriticalPoint = this.criticalPoints[this.currentIndex];
  322.                 float nextCriticalPoint = this.currentIndex >= maxIndex ? 1.0f : this.criticalPoints[this.currentIndex + 1];
  323.                 this.intervals[this.currentIndex].UpdateInterval((x - lastCriticalPoint) / (nextCriticalPoint - lastCriticalPoint));
  324.             }
  325.         }
  326.  
  327.         public class Repeat : Interval
  328.         {
  329.             private Interval interval;
  330.             private int count;
  331.             private int index;
  332.  
  333.             public Repeat(Interval interval, int count)
  334.                 : base(interval.Duration * count)
  335.             {
  336.                 this.interval = interval;
  337.                 this.count = count;
  338.             }
  339.  
  340.             public override void Reset()
  341.             {
  342.                 base.Reset();
  343.                 this.interval.Reset();
  344.                 this.index = 0;
  345.             }
  346.  
  347.             public override void UpdateInterval(float x)
  348.             {
  349.                 float d = x / (1.0f / this.count);
  350.                 int newIndex = (int)Math.Floor(d);
  351.                 if (newIndex != this.index)
  352.                     this.interval.Reset();
  353.                 this.index = newIndex;
  354.                 this.interval.UpdateInterval(d - newIndex);
  355.             }
  356.         }
  357.  
  358.         public class Reverse : Interval
  359.         {
  360.             private Interval interval;
  361.  
  362.             public Reverse(Interval interval)
  363.                 : base(interval.Duration)
  364.             {
  365.                 this.interval = interval;
  366.             }
  367.  
  368.             public override void Reset()
  369.             {
  370.                 base.Reset();
  371.                 this.interval.Reset();
  372.             }
  373.  
  374.             public override void UpdateInterval(float x)
  375.             {
  376.                 this.interval.UpdateInterval(1.0f - x);
  377.             }
  378.         }
  379.  
  380.         public class Speed : Interval
  381.         {
  382.             private Interval interval;
  383.             private float speed;
  384.  
  385.             public Speed(Interval interval, float speed)
  386.                 : base(interval.Duration / speed)
  387.             {
  388.                 this.interval = interval;
  389.                 this.speed = speed;
  390.             }
  391.  
  392.             public override void Reset()
  393.             {
  394.                 base.Reset();
  395.                 this.interval.Reset();
  396.             }
  397.  
  398.             public override void UpdateInterval(float x)
  399.             {
  400.                 this.interval.UpdateInterval(x);
  401.             }
  402.         }
  403.  
  404.         public class RepeatForever : Base
  405.         {
  406.             private Interval interval;
  407.             private float time;
  408.  
  409.             public RepeatForever(Interval interval)
  410.             {
  411.                 this.interval = interval;
  412.             }
  413.  
  414.             public override void Reset()
  415.             {
  416.                 this.interval.Reset();
  417.                 this.time = 0.0f;
  418.             }
  419.  
  420.             public sealed override void Update(float dt)
  421.             {
  422.                 if (this.time > this.interval.Duration)
  423.                 {
  424.                     this.interval.Reset();
  425.                     this.time = 0.0f;
  426.                 }
  427.                 this.time += dt;
  428.                 this.interval.UpdateInterval(this.time / this.interval.Duration);
  429.             }
  430.         }
  431.  
  432.         private Base action;
  433.         public float RunTime { get; private set; }
  434.  
  435.         public Animation(Base action)
  436.         {
  437.             this.action = action;
  438.         }
  439.  
  440.         public Animation(params Interval[] intervals)
  441.         {
  442.             if (intervals.Length == 1)
  443.                 this.action = intervals[0];
  444.             else
  445.                 this.action = new Sequence(intervals);
  446.         }
  447.  
  448.         void IUpdateableComponent.Update(float dt)
  449.         {
  450.             if (this.action.Done)
  451.                 this.Delete();
  452.             else
  453.             {
  454.                 this.RunTime += dt;
  455.                 this.action.Update(dt);
  456.             }
  457.         }
  458.     }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement