let rows = [ [ 0, 0, 0, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 ], [ 6, 0, 5, 4, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4 ], [ 4, 4, 4, 4, 4, 4, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 4, 4 ], [ 4, 4, 4, 4, 4, 4, 4, 4, 6, 0, 5, 6, 0, 0, 5, 4, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4 ], [ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 0, 5, 4, 6, 0, 0, 0, 5, 4, 4, 4, 4 ] ]; const rowsBackup = JSON.parse(JSON.stringify(rows)); const cloudW = 110; const density = 0.0333; const maxDroplets = 60; let raining = false; const times = []; const rainSpeed = 7; const rainIncrease = 0.025; const spreadIncrement = 0.01; // how fast the water spreads const spreadCutoff = 0.01; // determines when the water should consider itself "equalized" let reset = false; let fps; $(".reset").on("click", function (e) { reset = true; }); $(".debug").on("click", function () { console.log(rows); }); $(document).on("mousemove", function (e) { $(".cloud").css("left", e.pageX); }); $(document).on("mousedown", function () { raining = true; }); $(document).on("mouseup", function () { raining = false; }); function mainLoop() { if (reset) { console.log("resetting flood..."); rows = JSON.parse(JSON.stringify(rowsBackup)); $(".raindrop").remove(); reset = false; } else { if (raining && $(".raindrop").length < maxDroplets) { addDrop(); } moveDrops(); calcDrops(); spreadWater(); } calcFPS(); drawRows(); requestAnimationFrame(mainLoop); } requestAnimationFrame(mainLoop); function moveDrops() { const screenH = $(window).outerHeight(); const screenW = $(window).outerWidth(); $(".raindrop").each(function () { const currY = $(this).offset().top; const newY = currY + rainSpeed; const dropCol = parseInt($(this).data("col")); const colHeight = getColHeight(dropCol); if (newY > screenH - colHeight * (screenW * density)) { let emptyRow = -1; $.each(rows.slice().reverse(), function (index, row) { if (row[dropCol] < 1) { emptyRow = rows.length - 1 - index; return false; } }); if (emptyRow > -1) { rows[emptyRow][dropCol] = Math.min( 1, rows[emptyRow][dropCol] + rainIncrease ); } $(this).remove(); } else { $(this).css("transform", "translateY(" + newY + "px)"); } }); } function addDrop() { const xRand = Math.random() * cloudW; const cloudLeft = $(".cloud").offset().left; const rainPos = cloudLeft + xRand; const winW = $(window).outerWidth(); const colSector = Math.floor((rainPos / winW / 3.33) * 100); // console.log("adding raindrop to column " + colSector); $("body").append('
'); $(".raindrop--new") .removeClass("raindrop--new") .css("left", rainPos) .data("col", colSector); } function calcFPS() { const now = performance.now(); while (times.length > 0 && times[0] <= now - 1000) { times.shift(); } times.push(now); fps = times.length; $(".fps").text("FPS: " + fps); } function calcDrops() { $(".rain-count").text("Droplets: " + $(".raindrop").length); } function drawRows() { const grid = $(".ground-grid"); grid.empty(); $.each(rows, function (index, row) { $.each(row, function (index2, col) { let removeSpillage = false; let newBlock = $( '