Refactor createMarioDay function to enhance jump logic and coin spawning conditions [skip ci]
This commit is contained in:
@@ -54,15 +54,33 @@ function createMarioDay(container) {
|
|||||||
wrapper.appendChild(mario);
|
wrapper.appendChild(mario);
|
||||||
container.appendChild(wrapper);
|
container.appendChild(wrapper);
|
||||||
|
|
||||||
|
let jumpCount = 0;
|
||||||
|
let maxJumpsForThisRun = Math.floor(Math.random() * 3); // 0, 1, or 2
|
||||||
|
|
||||||
|
const resetJumpInterval = setInterval(() => {
|
||||||
|
if (!document.body.contains(container)) { clearInterval(resetJumpInterval); return; }
|
||||||
|
jumpCount = 0;
|
||||||
|
maxJumpsForThisRun = Math.floor(Math.random() * 3); // Randomize jumps for the next pass
|
||||||
|
}, (marioSpeedSeconds / 2) * 1000);
|
||||||
|
|
||||||
// Periodically throw out an 8-bit coin
|
// Periodically throw out an 8-bit coin
|
||||||
const intervalId = setInterval(() => {
|
const intervalId = setInterval(() => {
|
||||||
if (!document.body.contains(container)) { clearInterval(intervalId); return; }
|
if (!document.body.contains(container)) { clearInterval(intervalId); return; }
|
||||||
if (container.style.display === 'none') return;
|
if (container.style.display === 'none') return;
|
||||||
|
|
||||||
|
const marioRect = wrapper.getBoundingClientRect();
|
||||||
|
if (marioRect.left < 0 || marioRect.right > window.innerWidth) return;
|
||||||
|
|
||||||
|
if (letMarioJump && !mario.classList.contains('mario-jump') && jumpCount < maxJumpsForThisRun) {
|
||||||
|
mario.classList.add('mario-jump');
|
||||||
|
jumpCount++;
|
||||||
|
setTimeout(() => mario.classList.remove('mario-jump'), 800);
|
||||||
|
}
|
||||||
|
|
||||||
const coin = document.createElement('div');
|
const coin = document.createElement('div');
|
||||||
coin.className = 'mario-coin';
|
coin.className = 'mario-coin';
|
||||||
|
|
||||||
// Grab Mario's current screen position to lock the coin's X coordinate
|
// Grab Mario's current screen position to lock the coin's X coordinate
|
||||||
const marioRect = wrapper.getBoundingClientRect();
|
|
||||||
coin.style.left = `${marioRect.left + 16}px`;
|
coin.style.left = `${marioRect.left + 16}px`;
|
||||||
coin.style.bottom = '35px'; // bottom offset
|
coin.style.bottom = '35px'; // bottom offset
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user