Skip to content

Commit

Permalink
add workaround for animation speed changing / stopping
Browse files Browse the repository at this point in the history
closes #90
  • Loading branch information
Sesu8642 committed Dec 1, 2024
1 parent 2e578eb commit 9bff111
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
*/
public class MapRenderer {

/**
* If the stateTime reaches this value, it will be reduced by this value.
* The reason is that the stateTime must stay relatively small because of float limitations.
* See https://github.com/libgdx/libgdx/issues/7536.
* The subtracted amount must be a multiple of the animation duration.
* Equal to one day in seconds.
*/
private static final float STATE_TIME_THRESHOLD = 24 * 60 * 60f;
public static final float SPRITE_SIZE_MULTIPLIER = 1.05F;
public static final float LINE_EXTENSION = 0.14F;
public static final float WATER_TILE_SIZE = 12;
Expand Down Expand Up @@ -379,7 +387,7 @@ public synchronized void render() {
HashMap<Vector2, TextureRegion> frames = new HashMap<>(); // current frame for each map object
HashMap<Vector2, TextureRegion> darkenedFrames = new HashMap<>(); // current frame for each map object
spriteBatch.setProjectionMatrix(camera.combined);
stateTime += Gdx.graphics.getDeltaTime();
updateStateTime(Gdx.graphics.getDeltaTime());
// get the correct frames
for (Entry<Vector2, Animation<TextureRegion>> content : animatedContents.entrySet()) {
frames.put(content.getKey(), (content.getValue()).getKeyFrame(stateTime, true));
Expand Down Expand Up @@ -631,14 +639,23 @@ public void placeCameraForFullMapView(GameState gameState, long marginLeftPx, lo
camera.update();
}

/**
* See {@link MapRenderer#STATE_TIME_THRESHOLD}
*/
private void updateStateTime(float delta) {
stateTime += delta;
if (stateTime >= STATE_TIME_THRESHOLD) {
stateTime -= STATE_TIME_THRESHOLD;
}
}

/**
* Disposes all the disposables used.
*/
public void dispose() {
spriteBatch.dispose();
}

public void resize() {
spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, HEXTILE_WIDTH, HEXTILE_HEIGHT);
}


private class DrawTile {
Vector2 mapCoords;
Color color;
Expand Down

0 comments on commit 9bff111

Please sign in to comment.