diff --git a/playdate/source/images/matrix-table-20-20.png b/playdate/matrix_lua/images/matrix-table-20-20.png similarity index 100% rename from playdate/source/images/matrix-table-20-20.png rename to playdate/matrix_lua/images/matrix-table-20-20.png diff --git a/playdate/source/main.lua b/playdate/matrix_lua/main.lua similarity index 63% rename from playdate/source/main.lua rename to playdate/matrix_lua/main.lua index 374fdb7..752d869 100644 --- a/playdate/source/main.lua +++ b/playdate/matrix_lua/main.lua @@ -1,14 +1,16 @@ local gfx = playdate.graphics +local floor = math.floor +local random = math.random local screenWidth = playdate.display.getWidth() local screenHeight = playdate.display.getHeight() local glyphWidth = 20 -local numColumns = math.floor(screenWidth / glyphWidth) -local numRows = math.floor(screenHeight / glyphWidth) +local numColumns = floor(screenWidth / glyphWidth) +local numRows = floor(screenHeight / glyphWidth) local numCells = numColumns * numRows local numGlyphs = 133 -local numFades = 15 +local numFades = 16 local blackImage = gfx.image.new(glyphWidth, glyphWidth, gfx.kColorBlack) local glyphTable = gfx.imagetable.new('images/matrix') local ditherType = gfx.image.kDitherTypeAtkinson @@ -26,31 +28,45 @@ local maxSpeed = 1 local time = 0 local speed = maxSpeed -local sqrt2 = math.sqrt(2) -local sqrt5 = math.sqrt(5) -function wobble(x) - return x + 0.3 * math.sin(sqrt2 * x) + 0.2 * math.sin(sqrt5 * x) +local sineTable = {} +for i = 1,360 do + sineTable[i] = math.sin(math.pi / 180 * i) end +-- function fastSin(x) +-- x = x / 360 % 1 +-- local sign +-- if x < 0.5 then +-- sign = -1 +-- else +-- sign = 1 +-- end +-- x = (x % 0.5) * 2 - 0.5 +-- return sign * x * x * 4 - 1 +-- end + +local wobbleA = math.sqrt(2) / 50 +local wobbleB = math.sqrt(5) / 50 + local cells = {} for x = 1, numColumns do - local columnTimeOffset = math.random() * 1000 - local columnSpeedOffset = math.random() * 0.5 + 0.5 + local columnTimeOffset = random() * 1000 + local columnSpeedOffset = random() * 0.5 + 0.5 for y = 1, numRows do local cell = {} cell.x = x cell.y = y - cell.glyphCycle = math.random() + cell.glyphCycle = random() cell.columnTimeOffset = columnTimeOffset cell.columnSpeedOffset = columnSpeedOffset - cell.glyphIndex = math.floor(math.random() * numGlyphs) + 1 + cell.glyphIndex = floor(random() * numGlyphs) + 1 cell.fadeIndex = -1 cells[#cells + 1] = cell end end -playdate.display.setRefreshRate(0) +playdate.display.setRefreshRate(24) playdate.resetElapsedTime() function playdate.update() @@ -68,10 +84,18 @@ function playdate.update() for i = 1, numCells do local mustDraw = false local cell = cells[i] - local cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset - local brightness = math.log((1 - wobble(cellTime) % 1) * 1.25) * 6 - local fadeIndex = math.max(1, math.min(numFades, math.floor(numFades * (1 - brightness)))) + local cellTime = cell.y * -0.03 + cell.columnTimeOffset + time * cell.columnSpeedOffset + local brightness = 4 * ( + ( + cellTime + + 0.3 * sineTable[floor((wobbleA * cellTime) % 360) + 1] + + 0.2 * sineTable[floor((wobbleB * cellTime) % 360) + 1] + ) % 1 + ) + local fadeIndex = floor(brightness * numFades) + if fadeIndex < 1 then fadeIndex = 1 end + if fadeIndex > numFades then fadeIndex = numFades end if cell.fadeIndex ~= fadeIndex then cell.fadeIndex = fadeIndex mustDraw = true @@ -80,7 +104,7 @@ function playdate.update() cell.glyphCycle = cell.glyphCycle + delta * 2 if cell.glyphCycle > 1 then cell.glyphCycle = cell.glyphCycle % 1 - local glyphIndex = (cell.glyphIndex + math.random(20)) % numGlyphs + 1 + local glyphIndex = (cell.glyphIndex + random(20)) % numGlyphs + 1 if cell.glyphIndex ~= glyphIndex then cell.glyphIndex = glyphIndex if fadeIndex < numFades then @@ -93,5 +117,4 @@ function playdate.update() glyphs[cell.glyphIndex][cell.fadeIndex]:draw((cell.x - 1) * glyphWidth, (cell.y - 1) * glyphWidth) end end - end diff --git a/playdate/source/pdxinfo b/playdate/matrix_lua/pdxinfo similarity index 100% rename from playdate/source/pdxinfo rename to playdate/matrix_lua/pdxinfo