Add ability to control generation rate via crank
authorJC <dev@jacobcasper.com>
Mon, 31 Jul 2023 03:21:41 +0000 (22:21 -0500)
committerJC <dev@jacobcasper.com>
Mon, 31 Jul 2023 03:21:41 +0000 (22:21 -0500)
This seems like a good idea

source/main.lua

index 889fa60..7394e3a 100644 (file)
@@ -5,6 +5,21 @@ local WIDTH <const> = 24
 local HEIGHT <const> = 14
 local CELL_SIZE <const> = 16
 
+--- refresh / generation rate during runtime
+local refreshRate = 2
+
+local INPUT_HANDLERS <const> = {
+    cranked = function(change, acceleratedChange)
+        if change > 0 then
+            refreshRate = math.min(20, refreshRate + 1)
+        elseif change < 0 then
+            refreshRate = math.max(1, refreshRate - 1)
+        end
+    end
+}
+
+pd.inputHandlers.push(INPUT_HANDLERS)
+
 -- Convenience function for iterating over 8 neighbors
 function neighbors(g1, x, y)
     -- center tiles
@@ -115,6 +130,7 @@ local x = 1
 local y = 1
 
 local running = false
+
 function playdate.update()
     if playdate.buttonJustPressed("b") then
         running = not running
@@ -138,7 +154,7 @@ function playdate.update()
     else
         -- running
         -- Make running less epileptic while letting interaction remain smooth
-        pd.display.setRefreshRate(2)
+        pd.display.setRefreshRate(refreshRate)
         draw(cells)
         cells = generation(cells)
     end