main.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. io.stdout:setvbuf("no")
  2. local love = _G.love
  3. local sti = require "lib/Simple-Tiled-Implementation/sti"
  4. local map, world, tx, ty, points
  5. function love.load()
  6. -- Load map
  7. map = sti("tests/ortho.lua", { "box2d" })
  8. --map = sti("tests/ortho-inf.lua", { "box2d" })
  9. --map = sti("tests/iso.lua", { "box2d" })
  10. --map = sti("tests/stag.lua", { "box2d" })
  11. --map = sti("tests/hex.lua", { "box2d" })
  12. --map = sti("tests/objects.lua", { "box2d" })
  13. -- Print versions
  14. print("STI: " .. sti._VERSION)
  15. print("Map: " .. map.tiledversion)
  16. -- Prepare translations
  17. tx, ty = 0, 0
  18. -- Prepare physics world
  19. love.physics.setMeter(32)
  20. world = love.physics.newWorld(0, 0)
  21. map:box2d_init(world)
  22. -- Drop points on clicked areas
  23. points = {
  24. mouse = {},
  25. pixel = {}
  26. }
  27. love.graphics.setPointSize(5)
  28. end
  29. function love.update(dt)
  30. world:update(dt)
  31. map:update(dt)
  32. -- Move map
  33. local kd = love.keyboard.isDown
  34. tx = kd("a", "left") and tx - 128 * dt or tx
  35. tx = kd("d", "right") and tx + 128 * dt or tx
  36. ty = kd("w", "up") and ty - 128 * dt or ty
  37. ty = kd("s", "down") and ty + 128 * dt or ty
  38. end
  39. function love.draw()
  40. -- Draw map
  41. love.graphics.setColor(1, 1, 1)
  42. map:draw(-tx, -ty)
  43. -- Draw physics objects
  44. love.graphics.setColor(1, 0, 1)
  45. map:box2d_draw(-tx, -ty)
  46. -- Draw points
  47. love.graphics.translate(-tx, -ty)
  48. love.graphics.setColor(0, 1, 1)
  49. for _, point in ipairs(points.mouse) do
  50. love.graphics.points(point.x, point.y)
  51. end
  52. love.graphics.setColor(1, 1, 0)
  53. for _, point in ipairs(points.pixel) do
  54. love.graphics.points(point.x, point.y)
  55. end
  56. end
  57. function love.mousepressed(x, y, button)
  58. if button == 1 then
  59. x = x + tx
  60. y = y + ty
  61. local tilex, tiley = map:convertPixelToTile(x, y)
  62. local pixelx, pixely = map:convertTileToPixel(tilex, tiley)
  63. table.insert(points.pixel, { x=pixelx, y=pixely })
  64. table.insert(points.mouse, { x=x, y=y })
  65. print(x, tilex, pixelx)
  66. print(y, tiley, pixely)
  67. end
  68. end
  69. function love.resize(w, h)
  70. map:resize(w, h)
  71. end