Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 3449bd1

Browse files
committed
Refactor MOSprite animation
1 parent 2587e78 commit 3449bd1

File tree

2 files changed

+38
-54
lines changed

2 files changed

+38
-54
lines changed

Entities/MOSprite.cpp

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -458,59 +458,43 @@ Vector MOSprite::UnRotateOffset(const Vector &offset) const
458458
//////////////////////////////////////////////////////////////////////////////////////////
459459
// Description: Updates this MOSprite. Supposed to be done every frame.
460460

461-
void MOSprite::Update()
462-
{
463-
MovableObject::Update();
464-
465-
////////////////////////////////////////
466-
// Animate the sprite, if applicable
467-
468-
if (m_FrameCount > 1)
469-
{
470-
if (m_SpriteAnimMode == ALWAYSLOOP)
471-
{
472-
float cycleTime = ((long)m_SpriteAnimTimer.GetElapsedSimTimeMS()) % m_SpriteAnimDuration;
473-
m_Frame = floorf((cycleTime / (float)m_SpriteAnimDuration) * (float)m_FrameCount);
474-
}
475-
else if (m_SpriteAnimMode == ALWAYSRANDOM)
476-
{
477-
if (m_SpriteAnimTimer.GetElapsedSimTimeMS() > (m_SpriteAnimDuration / m_FrameCount))
478-
{
479-
// Quick switch to other frame if only two
480-
if (m_FrameCount == 2) { m_SpriteAnimMode = ALWAYSLOOP; }
481-
{
482-
int prevFrame = m_Frame;
483-
// Keep trying ot find a new frame
484-
do
485-
{
486-
m_Frame = floorf((float)m_FrameCount * PosRand());
487-
} while (m_Frame == prevFrame);
488-
}
489-
490-
m_SpriteAnimTimer.Reset();
491-
}
492-
}
493-
else if (m_SpriteAnimMode == ALWAYSPINGPONG)
494-
{
495-
if (m_FrameCount == 2) { m_SpriteAnimMode = ALWAYSLOOP; }
496-
int realFrameCount = m_FrameCount - 1;
497-
float perFrameTime = ((m_SpriteAnimDuration / m_FrameCount) / 2);
498-
if (m_SpriteAnimTimer.IsPastSimMS(perFrameTime))
499-
{
500-
m_SpriteAnimTimer.Reset();
501-
(m_SpriteAnimIsReversingFrames) ? (m_Frame--) : (m_Frame++);
502-
}
503-
504-
if (m_Frame == realFrameCount)
505-
{
506-
m_SpriteAnimIsReversingFrames = true;
507-
}
508-
else if (m_Frame == 0)
509-
{
510-
m_SpriteAnimIsReversingFrames = false;
511-
}
512-
}
513-
}
461+
void MOSprite::Update() {
462+
MovableObject::Update();
463+
464+
// First, check that the sprite has enough frames to even have an animation and override the setting if not
465+
if (m_FrameCount > 1) {
466+
// If animation mode is set to something other than ALWAYSLOOP but only has 2 frames, override it because it's pointless
467+
if ((m_SpriteAnimMode == ALWAYSRANDOM || m_SpriteAnimMode == ALWAYSPINGPONG) && m_FrameCount == 2) {
468+
m_SpriteAnimMode = ALWAYSLOOP;
469+
}
470+
} else {
471+
m_SpriteAnimMode = NOANIM;
472+
}
473+
474+
// Animate the sprite, if applicable
475+
int frameTime = m_SpriteAnimDuration / m_FrameCount;
476+
477+
if (m_SpriteAnimMode == ALWAYSLOOP) {
478+
float cycleTime = (static_cast<long>(m_SpriteAnimTimer.GetElapsedSimTimeMS())) % m_SpriteAnimDuration;
479+
m_Frame = floorf((cycleTime / static_cast<float>(m_SpriteAnimDuration)) * static_cast<float>(m_FrameCount));
480+
481+
} else if (m_SpriteAnimMode == ALWAYSRANDOM && m_SpriteAnimTimer.GetElapsedSimTimeMS() > frameTime) {
482+
int prevFrame = m_Frame;
483+
// Keep trying to find a new frame
484+
do {
485+
m_Frame = floorf(static_cast<float>(m_FrameCount) * PosRand());
486+
} while (m_Frame == prevFrame);
487+
m_SpriteAnimTimer.Reset();
488+
489+
} else if (m_SpriteAnimMode == ALWAYSPINGPONG && m_SpriteAnimTimer.GetElapsedSimTimeMS() > frameTime) {
490+
if (m_Frame == m_FrameCount - 1) {
491+
m_SpriteAnimIsReversingFrames = true;
492+
} else if (m_Frame == 0) {
493+
m_SpriteAnimIsReversingFrames = false;
494+
}
495+
m_SpriteAnimIsReversingFrames ? m_Frame-- : m_Frame++;
496+
m_SpriteAnimTimer.Reset();
497+
}
514498
}
515499

516500

Entities/MOSprite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ ENTITYALLOCATION(MOSprite)
651651
int m_SpriteAnimDuration;
652652
// The timer to keep track of the body animation
653653
Timer m_SpriteAnimTimer;
654-
// Keep track of animation direction (mainly for ALWAYSPINGPONG), true is increasing frame, false is decreasing frame
654+
// Keep track of animation direction (mainly for ALWAYSPINGPONG), true is decreasing frame, false is increasing frame
655655
bool m_SpriteAnimIsReversingFrames;
656656
// Whether flipped horizontally or not.
657657
bool m_HFlipped;

0 commit comments

Comments
 (0)