-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweenEngine.cs
82 lines (71 loc) · 1.97 KB
/
TweenEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Microsoft.Xna.Framework;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace FlappyXna
{
interface ITween
{
bool IsComplete { get; }
void Update(GameTime gameTime);
ITween To(Func<float> getter, Action<float> setter, float targetValue, float duration);
}
class Tween : ITween
{
float duration;
float elapsed;
float targetValue;
float originalValue;
Action<float> tweenFunc;
public bool IsComplete { get; private set; }
public Tween()
{
}
public ITween To(Func<float> getter, Action<float> setter, float targetValue, float duration)
{
this.originalValue = getter();
this.targetValue = targetValue;
this.tweenFunc = setter;
this.duration = duration;
this.elapsed = 0;
IsComplete = false;
return this;
}
public void Update(GameTime gameTime)
{
elapsed += gameTime.ElapsedGameTime.Milliseconds;
float currentValue = targetValue;
if(elapsed <= duration)
{
currentValue = MathHelper.Lerp(originalValue, targetValue, elapsed / duration);
} else
{
IsComplete = true;
}
tweenFunc(currentValue);
}
}
class TweenEngine
{
private List<ITween> objects;
public TweenEngine()
{
objects = new List<ITween>();
}
public ITween Add()
{
objects.Add(new Tween());
return objects[objects.Count-1];
}
public void Update(GameTime gameTime)
{
// apply gravity
for (int i = 0; i < objects.Count; i++)
{
objects[i].Update(gameTime);
}
objects.RemoveAll(tween => tween.IsComplete);
}
}
}