diff --git a/src/animation.ts b/src/animation.ts index a17e76d..48fe35a 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -312,6 +312,24 @@ export function getScorePopupsForFrame( }); } +/** + * Match Snake's commit-eating beat by extending the frame just before a green + * contribution is eaten. Alternate one and two extra frame durations so the + * pause stays brief but noticeable, without introducing nondeterminism. + */ +export function getCommitPauseFramesForFrame( + scoreEvents: readonly ScoreEvent[], + frameIndex: number, +): 0 | 1 | 2 { + const nextMoveFrame = frameIndex + 1; + const nextScoreEvent = scoreEvents.find( + (event) => event.eatenAtFrame === nextMoveFrame, + ); + + if (!nextScoreEvent) return 0; + return nextScoreEvent.score % 2 === 0 ? 2 : 1; +} + /** * Render an animated GIF of Commit-Man travelling through the contribution * garden. The character follows a connected, varied-width zigzag path and @@ -442,9 +460,11 @@ export function renderCommitManGif( } const indexed = applyPalette(pixels, PALETTE); + const commitPauseFrames = getCommitPauseFramesForFrame(scoreEvents, frameIndex); + encoder.writeFrame(indexed, width, height, { palette: PALETTE, - delay: frameDelayMs, + delay: frameDelayMs * (1 + commitPauseFrames), ...(frameIndex === 0 ? { repeat: 0 } : {}), }); } diff --git a/tests/animation.test.ts b/tests/animation.test.ts index 2847bfd..354e526 100644 --- a/tests/animation.test.ts +++ b/tests/animation.test.ts @@ -5,6 +5,7 @@ import { buildScoreEvents, buildZigzagPath, getCommitManEyePosition, + getCommitPauseFramesForFrame, getContributionLevelForFrame, getScorePopupsForFrame, renderCommitManGif, @@ -135,6 +136,22 @@ test("awards increasing scores only for green contribution cells", () => { ); }); +test("adds a deterministic one- or two-frame pause before eating green cells only", () => { + const scoreEvents = [ + { position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 }, + { position: { column: 2, row: 0 }, score: 2, eatenAtFrame: 3 }, + { position: { column: 4, row: 0 }, score: 3, eatenAtFrame: 6 }, + ]; + + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 0), 1); + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 1), 0); + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 2), 2); + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 3), 0); + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 5), 1); + assert.equal(getCommitPauseFramesForFrame(scoreEvents, 6), 0); + assert.equal(getCommitPauseFramesForFrame([], 0), 0); +}); + test("shows floating score popups briefly with an increasing score", () => { const events = [ { position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 1 },