roof.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local Roof = {}
  2. Roof.name = 'Roof'
  3. Roof.code = 'roof'
  4. Roof.z = 64
  5. local function perim(x, y, l, t, w, h)
  6. local r, b = l + w, t + h
  7. x, y = math.clamp(x, l, r), math.clamp(y, t, b)
  8. local dl, dr, dt, db = math.abs(x - l), math.abs(x - r), math.abs(y - t), math.abs(y - b)
  9. local m = math.min(dl, dr, dt, db)
  10. if m == dt then return x, t end
  11. if m == db then return x, b end
  12. if m == dl then return l, y end
  13. return r, y
  14. end
  15. function Roof:activate(map)
  16. if ctx.view then
  17. ctx.view:register(self)
  18. if not Roof.texture then
  19. local x, y, w, h = map.textures.wall:getViewport()
  20. Roof.texture = love.graphics.newCanvas(w, h)
  21. Roof.texture:renderTo(function()
  22. love.graphics.draw(map.atlas, map.textures.wall)
  23. end)
  24. Roof.texture:setWrap('repeat', 'repeat')
  25. end
  26. self.alpha = 1
  27. self.top = love.graphics.newMesh({
  28. {0, 0, 0, 0},
  29. {0, 0, 1, 0},
  30. {0, 0, 1, 1},
  31. {0, 0, 0, 1}
  32. }, self.texture)
  33. end
  34. end
  35. function Roof:update()
  36. if ctx.view and ctx.id then
  37. local p = ctx.players:get(ctx.id)
  38. if math.inside(p.x, p.y, self.x, self.y, self.width, self.height) then
  39. self.alpha = math.lerp(self.alpha, 0.2, math.min(6 * tickRate, 1))
  40. else
  41. self.alpha = math.lerp(self.alpha, 1, math.min(6 * tickRate, 1))
  42. end
  43. if not self:inView() then return end
  44. local x1, y1 = ctx.view.x + ctx.view.width / 2, ctx.view.y + ctx.view.height / 2
  45. local x2, y2 = perim(x1, y1, self.x, self.y, self.width, self.height)
  46. self.depth = math.clamp(math.distance(x1, y1, x2, y2) * ctx.view.scale - 1000 - self.z, -4096, -16)
  47. end
  48. end
  49. function Roof:draw()
  50. if not self:inView() then return end
  51. local g, v = love.graphics, ctx.view
  52. local ulx, uly = v:three(self.x, self.y, self.z)
  53. local urx, ury = v:three(self.x + self.width, self.y, self.z)
  54. local llx, lly = v:three(self.x, self.y + self.height, self.z)
  55. local lrx, lry = v:three(self.x + self.width, self.y + self.height, self.z)
  56. local w, h = self.width / self.texture:getWidth(), self.height / self.texture:getHeight()
  57. self.top:setVertex(1, ulx, uly, 0, 0)
  58. self.top:setVertex(2, urx, ury, w, 0)
  59. self.top:setVertex(3, lrx, lry, w, h)
  60. self.top:setVertex(4, llx, lly, 0, h)
  61. g.setColor(128, 128, 128, 255 * self.alpha)
  62. g.draw(self.top, 0, 0)
  63. end
  64. function Roof:inView()
  65. local x, y, w, h = self.x, self.y, self.width, self.height
  66. local v = ctx.view
  67. return x + w >= v.x and x <= v.x + v.width and y + h >= v.y and y <= v.y + v.height
  68. end
  69. return Roof