Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. local blocks = {}
  2.  
  3. local function tris(x, y, w, h)
  4. return {
  5. x, y,
  6. x+w, y,
  7. x+w, y+h,
  8. x, y,
  9. x, y+h,
  10. x+w, y+h
  11. }
  12. end
  13.  
  14. local function append(t, x, y, w, h)
  15. for k,v in ipairs(tris(x, y, w, h)) do
  16. t[#t + 1] = v
  17. end
  18. end
  19.  
  20. local blank = Graphics.loadImage(Misc.resolveFile("graphics/stock-0.png"))
  21. local swap = setmetatable({}, {__index = function() return blank end})
  22.  
  23. local function swapSprites()
  24. for _,i in ipairs(Block.SIZEABLE) do
  25. swap[i], Graphics.sprites.block[i].img = Graphics.sprites.block[i].img, swap[i]
  26. end
  27. end
  28.  
  29. function onDraw()
  30. for k,v in ipairs(Block.get(Block.SIZEABLE)) do
  31. if not v.isHidden then
  32. table.insert(blocks, v)
  33.  
  34. local id = v.id
  35. local img = Graphics.sprites.block[id].img
  36. local vtx = {add = append}
  37. local tex = {add = append}
  38. local x, y = v.x, v.y
  39. local w, h = v.width, v.height
  40.  
  41. -- size of the edges
  42. local tw = math.min(w/2, 32)
  43. local th = math.min(h/2, 32)
  44.  
  45. -- size of the edges for tex
  46. local ttw = tw / 96
  47. local tth = th / 96
  48.  
  49. local x2 = x + tw -- end of first corner
  50. local x3 = x + w - tw -- start of second corner
  51. local x4 = x + w --end of box
  52.  
  53. local y2 = y + th
  54. local y3 = y + h - th
  55. local y4 = y + h
  56. -- top left
  57. vtx:add(x, y, tw, th)
  58. tex:add(0, 0, ttw, tth)
  59. -- top right
  60. vtx:add(x3, y, tw, th)
  61. tex:add(1-ttw, 0, ttw, tth)
  62. -- bottom left
  63. vtx:add(x, y3, tw, th)
  64. tex:add(0, 1-tth, ttw, tth)
  65. -- bottom right
  66. vtx:add(x3, y3, tw, th)
  67. tex:add(1-ttw, 1-tth, ttw, tth)
  68. local maxI
  69. for i = x2, x3-32, 32 do
  70. -- top edge
  71. vtx:add(i, y, 32, th)
  72. tex:add(1/3, 0, 1/3, tth)
  73. -- bottom edge
  74. vtx:add(i, y3, 32, th)
  75. tex:add(1/3, 1-tth, 1/3, tth)
  76. maxI = i
  77. end
  78. local maxJ
  79. for j = y2, y3 - 32, 32 do
  80. -- left edge
  81. vtx:add(x, j, tw, 32)
  82. tex:add(0, 1/3, ttw, 1/3)
  83. -- right edge
  84. vtx:add(x3, j, tw, 32)
  85. tex:add(1-ttw, 1/3, ttw, 1/3)
  86. maxJ = j
  87. end
  88. local iw, jh = 0, 0 -- size of extra fill/edge portion
  89. if maxI then
  90. iw = x3 - (maxI + 32)
  91. end
  92. if maxJ then
  93. jh = y3 - (maxJ + 32)
  94. end
  95. if maxI and maxJ then
  96. for i = x2, maxI, 32 do
  97. for j = y2, maxJ, 32 do
  98. -- fill
  99. vtx:add(i, j, 32, 32)
  100. tex:add(1/3, 1/3, 1/3, 1/3)
  101. end
  102. end
  103. end
  104. if iw > 0 then
  105. -- end of top edge
  106. vtx:add(maxI + 32, y, iw, th)
  107. tex:add(1/3, 0, iw/96, tth)
  108. -- end of bottom edge
  109. vtx:add(maxI + 32, y3, iw, th)
  110. tex:add(1/3, 1-tth, iw/96, tth)
  111. if maxJ then
  112. for j = y2, maxJ, 32 do
  113. -- right edge of fill
  114. vtx:add(maxI + 32, j, iw, 32)
  115. tex:add(1/3, 1/3, iw/96, 1/3)
  116. end
  117. end
  118. end
  119. if jh > 0 then
  120. -- end of left edge
  121. vtx:add(x, maxJ + 32, tw, jh)
  122. tex:add(0, 1/3, ttw, jh/96)
  123. -- end of right edge
  124. vtx:add(x3, maxJ + 32, tw, jh)
  125. tex:add(1-ttw, 1/3, ttw, jh/96)
  126. if maxI then
  127. for i = x2, maxI, 32 do
  128. -- bottom edge of fill
  129. vtx:add(i, maxJ + 32, 32, jh)
  130. tex:add(1/3, 1/3, 1/3, jh/96)
  131. end
  132. end
  133. end
  134. if iw > 0 and jh > 0 then
  135. -- bottom right of fill
  136. vtx:add(maxI + 32, maxJ + 32, iw, jh)
  137. tex:add(1/3, 1/3, iw/96, jh/96)
  138. end
  139. Graphics.glDraw{
  140. texture = img,
  141. sceneCoords = true,
  142. vertexCoords = vtx,
  143. textureCoords = tex
  144. }
  145. end
  146. end
  147. swapSprites()
  148. end
  149.  
  150. function onDrawEnd()
  151. swapSprites()
  152. end
Add Comment
Please, Sign In to add comment