31 m_animationPhases = 0;
35 m_currentDuration = 0;
45 m_animationPhases = animationPhases;
46 m_async = fin->
getU8() == 0;
47 m_loopCount = fin->
get32();
48 m_startPhase = fin->
get8();
50 for(
int i = 0; i < m_animationPhases; ++i) {
51 int minimum = fin->
getU32();
52 int maximum = fin->
getU32();
53 m_phaseDurations.emplace_back(minimum, maximum);
58 assert(m_animationPhases == (
int)m_phaseDurations.size());
59 assert(m_startPhase >= -1 && m_startPhase < m_animationPhases);
64 fin->
addU8(m_async ? 0 : 1);
65 fin->
add32(m_loopCount);
66 fin->
add8(m_startPhase);
68 for(std::tuple<int, int> phase : m_phaseDurations) {
69 fin->
addU32(std::get<0>(phase));
70 fin->
addU32(std::get<1>(phase));
76 if(m_phase == phase)
return;
83 else if(phase >= 0 && phase < m_animationPhases)
90 m_currentDuration = getPhaseDuration(phase);
93 calculateSynchronous();
99 if(ticks != m_lastPhaseTicks && !m_isComplete) {
100 int elapsedTicks = (int)(ticks - m_lastPhaseTicks);
101 if(elapsedTicks >= m_currentDuration) {
104 phase = getPingPongPhase();
106 phase = getLoopPhase();
108 if(m_phase != phase) {
109 int duration = getPhaseDuration(phase) - (elapsedTicks - m_currentDuration);
110 if(duration < 0 && !m_async) {
111 calculateSynchronous();
114 m_currentDuration = std::max<int>(0, duration);
119 m_currentDuration -= elapsedTicks;
121 m_lastPhaseTicks = ticks;
131 for(
const auto &pair: m_phaseDurations) {
132 total += std::get<1>(pair);
141 return std::min<int>(index, m_animationPhases - 1);
146 if(m_startPhase > -1)
153 m_isComplete =
false;
159 int Animator::getPingPongPhase()
162 int nextPhase = m_phase + count;
163 if(nextPhase < 0 || nextPhase >= m_animationPhases) {
167 return m_phase + count;
170 int Animator::getLoopPhase()
172 int nextPhase = m_phase + 1;
173 if(nextPhase < m_animationPhases)
179 if(m_currentLoop < (m_loopCount - 1)) {
187 int Animator::getPhaseDuration(
int phase)
189 assert(phase < (
int)m_phaseDurations.size());
191 std::tuple<int, int> data = m_phaseDurations.at(phase);
192 int min = std::get<0>(data);
193 int max = std::get<1>(data);
194 if(min == max)
return min;
198 void Animator::calculateSynchronous()
200 int totalDuration = 0;
201 for(
int i = 0; i < m_animationPhases; i++)
202 totalDuration += getPhaseDuration(i);
205 int elapsedTicks = (int)(ticks % totalDuration);
207 for(
int i = 0; i < m_animationPhases; i++) {
208 int duration = getPhaseDuration(i);
209 if(elapsedTicks >= totalTime && elapsedTicks < totalTime + duration) {
211 m_currentDuration = duration - (elapsedTicks - totalTime);
214 totalTime += duration;
216 m_lastPhaseTicks = ticks;
222 for (
const auto &pair: m_phaseDurations) {
223 time += std::get<1>(pair);