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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright (c) 2015-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
namespace vke
{
public delegate void AnimationEventHandler(Animation a);
public delegate float GetterDelegate();
public delegate void SetterDelegate(float value);
public class Animation
{
public event AnimationEventHandler AnimationFinished;
public static Random random = new Random ();
public static int DelayMs = 0;
protected GetterDelegate getValue;
protected SetterDelegate setValue;
public string propertyName;
protected Stopwatch timer = new Stopwatch();
protected int delayStartMs = 0;
/// <summary>
/// Delay before firing ZnimationFinished event.
/// </summary>
protected int delayFinishMs = 0;
public static List<Animation> AnimationList = new List<Animation>();
public static bool HasAnimations => AnimationList.Count > 0;
//public FieldInfo member;
public Object AnimatedInstance;
#region CTOR
public Animation (){}
public Animation(Object instance, string _propertyName)
{
propertyName = _propertyName;
AnimatedInstance = instance;
PropertyInfo pi = instance.GetType().GetProperty(propertyName);
try {
getValue = (GetterDelegate)Delegate.CreateDelegate(typeof(GetterDelegate), instance, pi.GetGetMethod());
setValue = (SetterDelegate)Delegate.CreateDelegate(typeof(SetterDelegate), instance, pi.GetSetMethod());
} catch (Exception ex) {
Debug.WriteLine (ex.ToString ());
}
}
#endregion
public static void StartAnimation(Animation a, int delayMs = 0, AnimationEventHandler OnEnd = null)
{
lock (AnimationList) {
Animation aa = null;
if (Animation.GetAnimation (a.AnimatedInstance, a.propertyName, ref aa)) {
aa.CancelAnimation ();
}
//a.AnimationFinished += onAnimationFinished;
a.AnimationFinished += OnEnd;
a.delayStartMs = delayMs + DelayMs;
if (a.delayStartMs > 0)
a.timer.Start ();
AnimationList.Add (a);
}
}
static Stack<Animation> anims = new Stack<Animation>();
static int frame = 0;
public static void ProcessAnimations()
{
frame++;
// #region FLYING anim
// if (frame % 20 == 0){
// foreach (Player p in MagicEngine.CurrentEngine.Players) {
// foreach (CardInstance c in p.InPlay.Cards.Where(ci => ci.HasAbility(AbilityEnum.Flying) && ci.z < 0.4f)) {
//
// }
// }
// }
// #endregion
//Stopwatch animationTime = new Stopwatch();
//animationTime.Start();
const int maxAnim = 200000;
int count = 0;
lock (AnimationList) {
if (anims.Count == 0)
anims = new Stack<Animation> (AnimationList);
}
while (anims.Count > 0 && count < maxAnim) {
Animation a = anims.Pop ();
if (a == null)
continue;
if (a.timer.IsRunning) {
if (a.timer.ElapsedMilliseconds > a.delayStartMs)
a.timer.Stop ();
else
continue;
}
a.Process ();
count++;
}
//animationTime.Stop();
//Debug.WriteLine("animation: {0} ticks \t {1} ms ", animationTime.ElapsedTicks,animationTime.ElapsedMilliseconds);
}
public static bool GetAnimation(object instance, string PropertyName, ref Animation a)
{
for (int i = 0; i < AnimationList.Count; i++) {
Animation anim = AnimationList [i];
if (anim == null) {
continue;
}
if (anim.AnimatedInstance == instance && anim.propertyName == PropertyName) {
a = anim;
return true;
}
}
return false;
}
public virtual void Process () {}
public void CancelAnimation()
{
//Debug.WriteLine("Cancel anim: " + this.ToString());
AnimationList.Remove(this);
}
public void RaiseAnimationFinishedEvent()
{
if (AnimationFinished != null)
AnimationFinished (this);
}
public static void onAnimationFinished(Animation a)
{
Debug.WriteLine ("\t\tAnimation finished: " + a.ToString ());
}
}
}
|