tic80-loop.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function getFloorSpriteValueOfType(floor, row, col, spriteType)
  2. if (spriteType == "tile") then
  3. local tile = floorRef(floor, "tile", row, col)
  4. if (tile == nil) then
  5. return -1
  6. else
  7. return tile + 3
  8. end
  9. else
  10. local mob = floorRef(floor, "mob", row, col)
  11. if (mob == nil or mob == 0) then
  12. return -1
  13. else
  14. return mob + 258
  15. end
  16. end
  17. end
  18. function drawFloorSpritesOfType(floor, spriteType)
  19. local player = floor["player"]
  20. local centerX = player["col"]
  21. local centerY = player["row"]
  22. local xStart = centerX - 8
  23. local yStart = centerY - 5
  24. local xEnd = xStart + 15
  25. local yEnd = yStart + 9
  26. local screenX = 0
  27. local screenY = 0
  28. for j=yStart,yEnd do
  29. screenX = 0
  30. for i=xStart,xEnd do
  31. local spriteValue = getFloorSpriteValueOfType(floor, j, i, spriteType)
  32. if (spriteValue >= 0) then
  33. spr(getFloorSpriteValueOfType(floor, j, i, spriteType), screenX, screenY, 0, 2, 0, 0, 1, 1)
  34. end
  35. screenX = screenX + 16
  36. end
  37. screenY = screenY + 16
  38. end
  39. end
  40. function drawFloorTileSprites(floor)
  41. drawFloorSpritesOfType("tile")
  42. end
  43. function drawFloorMobSprites(floor)
  44. drawFloorSpritesOfType("mob")
  45. end
  46. function refreshOutput()
  47. cls(1)
  48. if (globalState["currentFloor"] ~= nil) then
  49. drawFloorSpritesOfType(globalState["currentFloor"], "tile")
  50. drawFloorSpritesOfType(globalState["currentFloor"], "mob")
  51. end
  52. end
  53. local timeHeld = {0,0,0,0,0,0,0,0}
  54. function processButtonHold(key)
  55. return 0
  56. end
  57. function processButtonPress(key)
  58. if (key == 0) then
  59. attemptToMove(globalState, "up")
  60. elseif (key == 1) then
  61. attemptToMove(globalState, "down")
  62. elseif (key == 2) then
  63. attemptToMove(globalState, "left")
  64. elseif (key == 3) then
  65. attemptToMove(globalState, "right")
  66. end
  67. end
  68. function TIC()
  69. refreshOutput()
  70. for i=0,7 do
  71. if (btn(i)) then
  72. if (timeHeld[i] > 30) then
  73. processButtonHold(i)
  74. elseif (timeHeld[i] == 0) then
  75. processButtonPress(i)
  76. end
  77. timeHeld[i] = timeHeld[i] + 1
  78. else
  79. timeHeld[i] = 0
  80. end
  81. end
  82. end