Skip to content

Commit

Permalink
Add two new SpriteAnimMode modes
Browse files Browse the repository at this point in the history
Rework MOSParticle animation handling - now works with SpriteAnimMode
Remove Framerate property from MOSParticle
  • Loading branch information
MaximDude committed May 18, 2020
1 parent e589af5 commit e1fdbec
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Exposed `AHuman.ThrowPrepTime` to lua and ini: ([Issue #101](https://github.com/cortex-command-community/Cortex-Command-Community-Project-Source/issues/101))
`ThrowPrepTime = valueInMS` will set how long it takes the `AHuman` to fully charge a throw. Default value is 1000.
`AHuman.ThrowPrepTime` to get/set values via lua.

- Added new `SpriteAnimMode` modes:
```
SpriteAnimMode = 7 // OVERLIFETIME
```
This mode handles exactly like (now removed) `MOSParticle.Framerate = 0` and will complete the sprite's animation cycle over the course of it's existence. `SpriteAnimDuration` is inapplicable when using this mode and will do nothing.
For example, an object that has a sprite with 10 frames and a lifetime of 10 seconds will animate at a rate of 1 frame per second, finishing it's animation cycle just before being deleted from the scene.
If this mode is used on an object that has `LifeTime = 0` (infinite) it will be overriden to `SpriteAnimMode = 1` (ALWAYSLOOP) otherwise it will never animate.
```
SpriteAnimMode = 8 // ONCOLLIDE
```
This mode will drive the animation forward based on collisions this object has with other MOs or the terrain. `SpriteAnimDuration` is inapplicable when using this mode and will do nothing.
This mode is `MOSParticle` specific and used mainly for animating casings. Using this mode on anything other than `MOSParticle` will do nothing.

### Changed

Expand Down Expand Up @@ -79,6 +92,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Removed hardcoded 10 second `LifeTime` restriction for `MOPixel` and `MOSParticle`.

- `MOSParticle` animation can now be set with `SpriteAnimMode` and `SpriteAnimDuration`. If the property isn't defined it will default to `SpriteAnimMode = 7` (OVERLIFETIME).

### Fixed

- Control schemes will no longer get deleted when being configured. Resetting the control scheme will load a preset instead of leaving it blank. ([Issue #121](https://github.com/cortex-command-community/Cortex-Command-Community-Project-Source/issues/121))
Expand All @@ -87,6 +102,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Removed `Settings.ini` property `PixelsPerMeter`. Now hardcoded and cannot be changed by the user.

- Removed `MOSParticle` property `Framerate` and lua bindings. `MOSParticle` animation is now handled with `SpriteAnimMode` like everything else.

***

## [0.1.0 pre-release 2][0.1.0-pre2] - 2020/05/08
Expand Down
32 changes: 11 additions & 21 deletions Entities/MOSParticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace RTE {

void MOSParticle::Clear() {
m_Atom = 0;
m_Framerate = 0;
m_TimeRest = 0;
m_SpriteAnimMode = OVERLIFETIME;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -32,7 +31,6 @@ namespace RTE {

m_Atom = new Atom(*(reference.m_Atom));
m_Atom->SetOwner(this);
m_Framerate = reference.m_Framerate;

return 0;
}
Expand All @@ -44,8 +42,6 @@ namespace RTE {
if (!m_Atom) { m_Atom = new Atom; }
reader >> *m_Atom;
m_Atom->SetOwner(this);
} else if (propName == "Framerate") {
reader >> m_Framerate;
} else {
// See if the base class(es) can find a match instead
return MOSprite::ReadProperty(propName, reader);
Expand All @@ -62,8 +58,6 @@ namespace RTE {
/*
writer.NewProperty("Atom");
writer << m_Atom;
writer.NewProperty("Framerate");
writer << m_Framerate;
*/

return 0;
Expand Down Expand Up @@ -131,27 +125,23 @@ namespace RTE {
// Now clear out the ignore override for next frame
m_Atom->ClearMOIDIgnoreList();

// Change angular velocity after collision.
if (hitCount >= 1) {
m_AngularVel *= 0.5F * velMag * NormalRand();
m_AngularVel = -m_AngularVel;
}
if (m_SpriteAnimMode == ONCOLLIDE) {
// Change angular velocity after collision.
if (hitCount >= 1) {
m_AngularVel *= 0.5F * velMag * NormalRand();
m_AngularVel = -m_AngularVel;
}

// Animate based on rotation.. temporary.
if (m_Framerate) {
// TODO: Rework this is it's less incomprehensible black magic math and not driven by AngularVel.
double newFrame = m_Rotation.GetRadAngle();
newFrame -= std::floorf(m_Rotation.GetRadAngle() / (2 * c_PI)) * (2 * c_PI);
newFrame /= (2 * c_PI);
newFrame *= m_FrameCount;
m_Frame = std::floorf(newFrame);
m_Rotation += static_cast<long double>(m_AngularVel) * static_cast<long double>(deltaTime);
} else {
// Animate over lifetime
double newFrame = static_cast<double>(m_FrameCount) * (static_cast<double>(m_AgeTimer.GetElapsedSimTimeMS()) / static_cast<double>(m_Lifetime));
m_Frame = std::floorf(newFrame);
}
m_Rotation += m_AngularVel * deltaTime;

if (m_Frame >= m_FrameCount) { m_Frame = m_FrameCount - 1; }
if (m_Frame >= m_FrameCount) { m_Frame = m_FrameCount - 1; }
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 0 additions & 14 deletions Entities/MOSParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@ namespace RTE {
/// </summary>
/// <param name="newAtom">A reference to the new Atom.</param>
void SetAtom(Atom *newAtom);

/// <summary>
/// Gets the current frame rate of the animation playback.
/// </summary>
/// <returns>The current frame rate in frames per second.</returns>
float GetFramerate() const { return m_Framerate; }

/// <summary>
/// Sets the current frame rate of the animation playback.
/// </summary>
/// <param name="newFramerate">The new frame rate in frames per second.</param>
void SetFramerate(float newFramerate) { m_Framerate = newFramerate; }
#pragma endregion

#pragma region Virtual Override Methods
Expand Down Expand Up @@ -168,8 +156,6 @@ namespace RTE {
static Entity::ClassInfo m_sClass; //!< ClassInfo for this class.

Atom *m_Atom; //!< The Atom that will be the physical representation of this MOSParticle.

float m_Framerate; //!< Frame rate of the animation playback in fps. Negative means backwards.
float m_TimeRest; //!< Accumulated time in seconds that did not cause a frame change.

private:
Expand Down
10 changes: 9 additions & 1 deletion Entities/MOSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ void MOSprite::Update() {
// If animation mode is set to something other than ALWAYSLOOP but only has 2 frames, override it because it's pointless
if ((m_SpriteAnimMode == ALWAYSRANDOM || m_SpriteAnimMode == ALWAYSPINGPONG) && m_FrameCount == 2) {
m_SpriteAnimMode = ALWAYSLOOP;
// If animation mode is set to over lifetime but lifetime is unlimited, override to always loop otherwise it will never animate.
} else if (m_SpriteAnimMode == OVERLIFETIME && m_Lifetime == 0) {
m_SpriteAnimMode = ALWAYSLOOP;
}
} else {
m_SpriteAnimMode = NOANIM;
Expand All @@ -474,6 +477,7 @@ void MOSprite::Update() {
// Animate the sprite, if applicable
unsigned int frameTime = m_SpriteAnimDuration / m_FrameCount;
unsigned int prevFrame = m_Frame;
double lifeTimeFrame = 0;

if (m_SpriteAnimTimer.GetElapsedSimTimeMS() > frameTime) {
switch (m_SpriteAnimMode) {
Expand All @@ -483,7 +487,7 @@ void MOSprite::Update() {
break;
case ALWAYSRANDOM:
while (m_Frame == prevFrame) {
m_Frame = floorf(static_cast<float>(m_FrameCount) * PosRand());
m_Frame = std::floorf(static_cast<float>(m_FrameCount) * PosRand());
}
m_SpriteAnimTimer.Reset();
break;
Expand All @@ -496,6 +500,10 @@ void MOSprite::Update() {
m_SpriteAnimIsReversingFrames ? m_Frame-- : m_Frame++;
m_SpriteAnimTimer.Reset();
break;
case OVERLIFETIME:
lifeTimeFrame = static_cast<double>(m_FrameCount) * m_AgeTimer.GetElapsedSimTimeMS() / static_cast<double>(m_Lifetime));
m_Frame = std::floorf(lifeTimeFrame);
break;
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions Entities/MOSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class MOSprite:
LOOPWHENMOVING,
LOOPWHENOPENCLOSE,
PINGPONGOPENCLOSE,
OVERLIFETIME,
ONCOLLIDE,
SpriteAnimModeCount
};

Expand Down
5 changes: 3 additions & 2 deletions Managers/LuaMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,9 @@ int LuaMan::Create()
value("ALWAYSPINGPONG", 3),
value("LOOPWHENMOVING", 4),
value("LOOPWHENOPENCLOSE", 5),
value("PINGPONGOPENCLOSE", 6)
value("PINGPONGOPENCLOSE", 6),
value("OVERLIFETIME", 7),
value("ONCOLLIDE", 8)
]
/*.property("Material", &MOSprite::GetMaterial)*/
.property("Diameter", &MOSprite::GetDiameter)
Expand Down Expand Up @@ -735,7 +737,6 @@ int LuaMan::Create()

CONCRETELUABINDING(MOSParticle, MOSprite)
/*.property("Material", &MOSParticle::GetMaterial)*/
.property("Framerate", &MOSParticle::GetFramerate, &MOSParticle::SetFramerate)
// .property("Atom", &MOSParticle::GetAtom, &MOSParticle:SetAtom)
.property("IsGold", &MOSParticle::IsGold),

Expand Down

0 comments on commit e1fdbec

Please sign in to comment.