get_set_tile.script 961 B

12345678910111213141516171819202122232425262728293031
  1. local TILE_SIZE = 64
  2. -- helper function check if a tile coordinate is within the bounds of the tilemap
  3. local function within_bounds(x, y)
  4. local bx, by, bw, bh = tilemap.get_bounds("#level")
  5. return x >= bx and y >= by and x < (bx + bw) and y < (by + bh)
  6. end
  7. function init(self)
  8. msg.post(".", "acquire_input_focus")
  9. end
  10. function on_input(self, action_id, action)
  11. local tile_x = math.ceil(action.x / TILE_SIZE)
  12. local tile_y = math.ceil(action.y / TILE_SIZE)
  13. if within_bounds(tile_x, tile_y) then
  14. -- click to place a flower
  15. if action_id == hash("touch") and action.pressed then
  16. tilemap.set_tile("#level", "layer1", tile_x, tile_y, 77)
  17. end
  18. -- show tile info
  19. local tile = tilemap.get_tile("#level", "layer1", tile_x, tile_y)
  20. local text = ("x: %d y: %d tile: %d"):format(tile_x, tile_y, tile)
  21. label.set_text("#label", text)
  22. else
  23. local text = ("x: %d y: %d out of bounds"):format(tile_x, tile_y)
  24. label.set_text("#label", text)
  25. end
  26. end