1+ #include " ActorAnimationInstance.hpp"
2+ #include " ../Actor.hpp"
3+ #include < cmath>
4+
5+ using namespace nima ;
6+
7+ ActorAnimationInstance::ActorAnimationInstance (Actor* actor, ActorAnimation* animation) :
8+ m_Actor(actor),
9+ m_Animation(animation),
10+ m_Time(0 .0f ),
11+ m_Min(0 .0f ),
12+ m_Max(animation->duration ()),
13+ m_Range(animation->duration ()),
14+ m_Loop(animation->isLooping ()),
15+ m_EventCallbackUserData(nullptr ),
16+ m_EventCallback(nullptr )
17+ {
18+
19+ }
20+
21+ ActorAnimationInstance::~ActorAnimationInstance ()
22+ {
23+
24+ }
25+
26+ float ActorAnimationInstance::duration () const
27+ {
28+ return m_Range;
29+ }
30+
31+ float ActorAnimationInstance::min () const
32+ {
33+ return m_Min;
34+ }
35+
36+ float ActorAnimationInstance::max () const
37+ {
38+ return m_Max;
39+ }
40+
41+ float ActorAnimationInstance::time () const
42+ {
43+ return m_Time;
44+ }
45+
46+ void ActorAnimationInstance::time (float value)
47+ {
48+ float delta = value - m_Time;
49+ float time = m_Time + std::fmod (delta, m_Range);
50+
51+ if (time < m_Min)
52+ {
53+ if (m_Loop)
54+ {
55+ time = m_Max - (m_Min - time);
56+ }
57+ else
58+ {
59+ time = m_Min;
60+ }
61+ }
62+ else if (time > m_Max)
63+ {
64+ if (m_Loop)
65+ {
66+ time = m_Min + (time - m_Max);
67+ }
68+ else
69+ {
70+ time = m_Max;
71+ }
72+ }
73+ m_Time = time;
74+ }
75+
76+ bool ActorAnimationInstance::isLooping () const
77+ {
78+ return m_Loop;
79+ }
80+
81+ void ActorAnimationInstance::isLooping (bool isIt)
82+ {
83+ m_Loop = isIt;
84+ }
85+
86+ void ActorAnimationInstance::eventCallback (ActorAnimationEvent::Callback callback, void * userdata)
87+ {
88+ m_EventCallbackUserData = userdata;
89+ m_EventCallback = callback;
90+ }
91+
92+ void ActorAnimationInstance::advance (float seconds)
93+ {
94+ float time = m_Time;
95+ time += std::fmod (seconds, m_Range);
96+ if (time < m_Min)
97+ {
98+ if (m_Loop)
99+ {
100+ m_Animation->triggerEvents (m_Actor, time, m_Time, m_Events);
101+ time = m_Max - (m_Min - time);
102+ m_Animation->triggerEvents (m_Actor, time, m_Max, m_Events);
103+ }
104+ else
105+ {
106+ time = m_Min;
107+ if (m_Time != time)
108+ {
109+ m_Animation->triggerEvents (m_Actor, m_Min, m_Time, m_Events);
110+ }
111+ }
112+ }
113+ else if (time > m_Max)
114+ {
115+ if (m_Loop)
116+ {
117+ m_Animation->triggerEvents (m_Actor, time, m_Time, m_Events);
118+ time = m_Min + (time - m_Max);
119+ m_Animation->triggerEvents (m_Actor, m_Min-0 .001f , time, m_Events);
120+ }
121+ else
122+ {
123+ time = m_Max;
124+ if (m_Time != time)
125+ {
126+ m_Animation->triggerEvents (m_Actor, m_Time, m_Max, m_Events);
127+ }
128+ }
129+ }
130+ else if (time > m_Time)
131+ {
132+ m_Animation->triggerEvents (m_Actor, m_Time, time, m_Events);
133+ }
134+ else
135+ {
136+ m_Animation->triggerEvents (m_Actor, time, m_Time, m_Events);
137+ }
138+
139+ for (const ActorAnimationEvent& ev : m_Events)
140+ {
141+ if (m_EventCallback != nullptr )
142+ {
143+ m_EventCallback (ev, m_EventCallbackUserData);
144+ }
145+ if (m_Actor->m_EventCallback != nullptr )
146+ {
147+ m_Actor->m_EventCallback (ev, m_Actor->m_EventCallbackUserData );
148+ }
149+ /* if (AnimationEvent != null)
150+ {
151+ AnimationEvent(this, ev);
152+ }
153+ m_Actor.OnAnimationEvent(ev);*/
154+ }
155+ m_Events.clear ();
156+ /* for(var i = 0; i < triggeredEvents.length; i++)
157+ {
158+ var event = triggeredEvents[i];
159+ this.dispatch("animationEvent", event);
160+ m_Actor.dispatch("animationEvent", event);
161+ }*/
162+ m_Time = time;
163+ }
164+
165+ void ActorAnimationInstance::apply (float mix)
166+ {
167+ m_Animation->apply (m_Time, m_Actor, mix);
168+ }
0 commit comments