aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/Animation/Animation.cs
blob: 21b141a2fbfe091d773cdafb93d56af2901be9ba (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
//  Animation.cs
//
//  Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
//  Copyright (c) 2015 jp
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

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 ());
		}


    }
}