grid.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. local Grid = class()
  2. Grid.name = 'Grid'
  3. local g = love.graphics
  4. function Grid:init()
  5. self.color = {0, 0, 0, 50}
  6. self.hoverColor = {0, 0, 0, 20}
  7. self.size = 32
  8. self.depth = -10000
  9. end
  10. function Grid:draw()
  11. g.setLineWidth(1 / ctx.view.scale)
  12. g.setColor(self.hoverColor)
  13. local x = math.floor(ctx.view:worldMouseX() / self.size) * self.size
  14. local y = math.floor(ctx.view:worldMouseY() / self.size) * self.size
  15. if x >= 0 and y >= 0 and x < ctx.map.width and y < ctx.map.height then
  16. g.rectangle('fill', x, y, self.size, self.size)
  17. end
  18. g.setColor(self.color)
  19. for i = .5, ctx.map.width + .5, self.size do
  20. g.line(i, 0, i, ctx.map.height)
  21. end
  22. for i = .5, ctx.map.height + .5, self.size do
  23. g.line(0, i, ctx.map.width, i)
  24. end
  25. g.setLineWidth(1)
  26. end
  27. function Grid:keypressed(key)
  28. if key == '[' then self.size = math.max(self.size / 2, 8)
  29. elseif key == ']' then self.size = math.min(self.size * 2, 256) end
  30. end
  31. function Grid:snap(x, y)
  32. if love.keyboard.isDown('lalt') then return x, y end
  33. return math.round(x / self.size) * self.size, math.round(y / self.size) * self.size
  34. end
  35. return Grid