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

Commit a916160

Browse files
authored
Merge pull request #82 from cortex-command-community/CF77-pingpoing-anim-mode
Cf77 pingpoing anim mode
2 parents df8ac79 + d69fe7e commit a916160

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

Entities/MOSRotating.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,43 +1614,6 @@ void MOSRotating::Update()
16141614

16151615
MOSprite::Update();
16161616

1617-
////////////////////////////////////////
1618-
// Animate the sprite, if applicable
1619-
1620-
if (m_FrameCount > 1)
1621-
{
1622-
if (m_SpriteAnimMode == ALWAYSLOOP)
1623-
{
1624-
float cycleTime = ((long)m_SpriteAnimTimer.GetElapsedSimTimeMS()) % m_SpriteAnimDuration;
1625-
m_Frame = floorf((cycleTime / (float)m_SpriteAnimDuration) * (float)m_FrameCount);
1626-
}
1627-
else if (m_SpriteAnimMode == ALWAYSRANDOM)
1628-
{
1629-
if (m_SpriteAnimTimer.GetElapsedSimTimeMS() > (m_SpriteAnimDuration / m_FrameCount))
1630-
{
1631-
// Quick switch to other frame if only two
1632-
if (m_FrameCount == 2)
1633-
m_Frame = m_Frame == 0 ? 1 : 0;
1634-
else
1635-
{
1636-
int prevFrame = m_Frame;
1637-
// Keep trying ot find a new frame
1638-
do
1639-
{
1640-
m_Frame = floorf((float)m_FrameCount * PosRand());
1641-
}
1642-
while (m_Frame == prevFrame);
1643-
}
1644-
1645-
m_SpriteAnimTimer.Reset();
1646-
}
1647-
}
1648-
else if (m_SpriteAnimMode == ALWAYSPINGPONG)
1649-
{
1650-
1651-
}
1652-
}
1653-
16541617
if (m_InheritEffectRotAngle)
16551618
m_EffectRotAngle = m_Rotation.GetRadAngle();
16561619

Entities/MOSprite.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void MOSprite::Clear()
3737
m_SpriteAnimMode = NOANIM;
3838
m_SpriteAnimDuration = 500;
3939
m_SpriteAnimTimer.Reset();
40+
m_SpriteAnimIsReversingFrames = false;
4041
m_HFlipped = false;
4142
m_MaxRadius = 1;
4243
m_MaxDiameter = 2;
@@ -451,17 +452,54 @@ Vector MOSprite::UnRotateOffset(const Vector &offset) const
451452
return rotOff;
452453
}
453454

454-
/*
455+
455456
//////////////////////////////////////////////////////////////////////////////////////////
456457
// Pure v. method: Update
457458
//////////////////////////////////////////////////////////////////////////////////////////
458459
// Description: Updates this MOSprite. Supposed to be done every frame.
459460

460-
void MOSprite::Update()
461-
{
462-
MovableObject::Update();
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+
unsigned int frameTime = m_SpriteAnimDuration / m_FrameCount;
476+
unsigned int prevFrame = m_Frame;
477+
478+
if (m_SpriteAnimTimer.GetElapsedSimTimeMS() > frameTime) {
479+
switch (m_SpriteAnimMode) {
480+
case ALWAYSLOOP:
481+
m_Frame = ((m_Frame + 1) % m_FrameCount);
482+
break;
483+
case ALWAYSRANDOM:
484+
while (m_Frame == prevFrame) {
485+
m_Frame = floorf(static_cast<float>(m_FrameCount) * PosRand());
486+
}
487+
break;
488+
case ALWAYSPINGPONG:
489+
if (m_Frame == m_FrameCount - 1) {
490+
m_SpriteAnimIsReversingFrames = true;
491+
} else if (m_Frame == 0) {
492+
m_SpriteAnimIsReversingFrames = false;
493+
}
494+
m_SpriteAnimIsReversingFrames ? m_Frame-- : m_Frame++;
495+
break;
496+
default:
497+
break;
498+
}
499+
m_SpriteAnimTimer.Reset();
500+
}
463501
}
464-
*/
502+
465503

466504
//////////////////////////////////////////////////////////////////////////////////////////
467505
// Virtual method: Draw

Entities/MOSprite.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,16 @@ ENTITYALLOCATION(MOSprite)
586586

587587
virtual void SetSpriteAnimDuration(int newDuration) { m_SpriteAnimDuration = newDuration; }
588588

589-
/*
589+
590590
//////////////////////////////////////////////////////////////////////////////////////////
591591
// Virtual method: Update
592592
//////////////////////////////////////////////////////////////////////////////////////////
593593
// Description: Updates this MovableObject. Supposed to be done every frame.
594594
// Arguments: None.
595595
// Return value: None.
596596

597-
virtual void Update() = 0;
598-
*/
597+
virtual void Update();
598+
599599

600600
//////////////////////////////////////////////////////////////////////////////////////////
601601
// Virtual method: Draw
@@ -651,6 +651,8 @@ 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 decreasing frame, false is increasing frame
655+
bool m_SpriteAnimIsReversingFrames;
654656
// Whether flipped horizontally or not.
655657
bool m_HFlipped;
656658
// The precalculated maximum possible radius and diameter of this, in pixels

0 commit comments

Comments
 (0)