diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-02 14:52:30 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-08 21:03:28 +0200 |
commit | 7de0b165f196cb0c1f983b6d2ab26ef9791a2d36 (patch) | |
tree | 86f55c00b3fe17dae950351f3c33d35e6abd5a4d /src/being.cpp | |
parent | 59a7d5c58f8b3af21b3e19d4e78f5653bf011bfb (diff) | |
download | mana-7de0b165f196cb0c1f983b6d2ab26ef9791a2d36.tar.gz mana-7de0b165f196cb0c1f983b6d2ab26ef9791a2d36.tar.bz2 mana-7de0b165f196cb0c1f983b6d2ab26ef9791a2d36.tar.xz mana-7de0b165f196cb0c1f983b6d2ab26ef9791a2d36.zip |
Do a single logic update each frame
The logic update now uses Time::deltaTimeMs() where needed to make it
framerate-independent. This means there will no longer be multiple logic
calls per frame (as was usually the case with logic ticking at 100 fps
whereas the game would generally run at 60 fps).
At the same time, the game can be more precise at higher framerates and
should now run smoother at 144 Hz, for example. Previously the game would
sometimes skip logic ticks at that rate.
This change affects:
* Updating of animations
* Being movement speed
* More moving of manual time variables to Timer
Notoriously, the particle system still does 100 ticks/second.
Diffstat (limited to 'src/being.cpp')
-rw-r--r-- | src/being.cpp | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/src/being.cpp b/src/being.cpp index 88104d74..b622143a 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -158,8 +158,8 @@ void Being::setMoveSpeed(const Vector &speed) mMoveSpeed = speed; // If we already can, recalculate the system speed right away. if (mMap) - mSpeedPixelsPerTick = - Net::getPlayerHandler()->getPixelsPerTickMoveSpeed(speed); + mSpeedPixelsPerSecond = + Net::getPlayerHandler()->getPixelsPerSecondMoveSpeed(speed); } int Being::getSpeechTextYPosition() const @@ -284,7 +284,7 @@ void Being::setSpeech(const std::string &text, int time) } if (!mSpeech.empty()) - mSpeechTime = time <= SPEECH_MAX_TIME ? time : SPEECH_MAX_TIME; + mSpeechTimer.set(std::min(time, SPEECH_MAX_TIME)); const int speech = config.getIntValue("speech"); if (speech == TEXT_OVERHEAD) @@ -755,12 +755,8 @@ int Being::getCollisionRadius() const void Being::logic() { - // Reduce the time that speech is still displayed - if (mSpeechTime > 0) - mSpeechTime--; - // Remove text and speechbubbles if speech boxes aren't being used - if (mSpeechTime == 0 && mText) + if (mText && mSpeechTimer.passed()) { delete mText; mText = nullptr; @@ -772,7 +768,7 @@ void Being::logic() restoreAllSpriteParticles(); } - if ((mAction != DEAD) && !mSpeedPixelsPerTick.isNull()) + if ((mAction != DEAD) && !mSpeedPixelsPerSecond.isNull()) { const Vector dest = (mPath.empty()) ? mDest : Vector(mPath.front().x, @@ -803,13 +799,14 @@ void Being::logic() // â = a / ||a|| (||a|| is the a length.) // Then, diff = (dir/||dir||) * speed. const Vector normalizedDir = dir.normalized(); - Vector diff(normalizedDir.x * mSpeedPixelsPerTick.x, - normalizedDir.y * mSpeedPixelsPerTick.y); + const int ms = Time::deltaTimeMs(); + Vector diff(normalizedDir.x * mSpeedPixelsPerSecond.x * ms / 1000.0f, + normalizedDir.y * mSpeedPixelsPerSecond.y * ms / 1000.0f); // Test if we don't miss the destination by a move too far: if (diff.length() > distance) { - setPosition(mPos + dir); + setPosition(dest); // Also, if the destination is reached, try to get the next // path point, if existing. @@ -876,13 +873,12 @@ void Being::drawSpeech(int offsetX, int offsetY) const int speech = config.getIntValue("speech"); // Draw speech above this being - if (mSpeechTime == 0) + if (mSpeechTimer.passed()) { if (mSpeechBubble->isVisible()) mSpeechBubble->setVisible(false); } - else if (mSpeechTime > 0 && (speech == NAME_IN_BUBBLE || - speech == NO_NAME_IN_BUBBLE)) + else if (speech == NAME_IN_BUBBLE || speech == NO_NAME_IN_BUBBLE) { const bool showName = (speech == NAME_IN_BUBBLE); @@ -897,11 +893,11 @@ void Being::drawSpeech(int offsetX, int offsetY) - mSpeechBubble->getHeight() - offsetY); mSpeechBubble->setVisible(true); } - else if (mSpeechTime > 0 && speech == TEXT_OVERHEAD) + else if (speech == TEXT_OVERHEAD) { mSpeechBubble->setVisible(false); - if (! mText) + if (!mText) { mText = new Text(mSpeech, getPixelX(), getPixelY() - getHeight(), @@ -1216,7 +1212,7 @@ void Being::setMap(Map *map) // Recalculate pixel/tick speed if (map && !mMoveSpeed.isNull()) { - mSpeedPixelsPerTick = - Net::getPlayerHandler()->getPixelsPerTickMoveSpeed(mMoveSpeed, map); + mSpeedPixelsPerSecond = + Net::getPlayerHandler()->getPixelsPerSecondMoveSpeed(mMoveSpeed, map); } } |