using_tilemaps.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. _doc_using_tilemaps:
  2. Using tilemaps
  3. ~~~~~~~~~~~~~~
  4. Introduction
  5. ~~~~~~~~~~~~
  6. Tilemaps are a simple and quick way to make 2D game levels. Basically,
  7. you start with bunch of reference tiles (or pieces) that can be put in a
  8. grid, as many times each as desired:
  9. .. image:: /img/tilemap.png
  10. Collision can also be added to the tiles, allowing for both 2D side
  11. scroller or top down games.
  12. Making a tileset
  13. ----------------
  14. To begin with, a tileset needs to be made. Here are some tiles for it.
  15. They are all in the same image because artists will often prefer this.
  16. Having them as separate images also works too.
  17. .. image:: /img/tileset.png
  18. Create a new project and throw the above png image inside.
  19. We will be creating a :ref:`TileSet <class_TileSet>`
  20. resource. While this resource exports properties, it's pretty difficult
  21. to get complex data into it and maintain it:
  22. .. image:: /img/tileset_edit_resource.png
  23. There's enough properties to get by, and with some effort editing this
  24. way can work, but the easiest way to edit and maintain a tileset is with
  25. the export tool!
  26. TileSet scene
  27. -------------
  28. Create a new scene with a regular node or node2d as root. For each new a
  29. sprite will be added. Since tiles here are 50x50, enabling snap might be
  30. a good idea.
  31. If more than one tile is present in the source image, make sure to use
  32. the region property of the sprite to adjust which of the sprites is the
  33. source texture is being edited.
  34. Finally, make sure to name your sprite node correctly, this will ensure
  35. that, in subsequent edits to the tileset (for example, if added
  36. collision, changed the region, etc), the tile will still be **identified
  37. correctly and updated**. This name should be unique.
  38. Sounds like a lot of requirements, so here's a screenshot that shows
  39. where everything of relevance is:
  40. .. image:: /img/tile_example.png
  41. Continue adding all the tiles, adjust the offsets if needed (if you use
  42. multiple tiles in a single image) unless there is a sprite per each
  43. tile. Again, as always, remember that their names must be unique.
  44. .. image:: /img/tile_example2.png
  45. Collision
  46. ---------
  47. To add collision to a tile, create a StaticBody2D child for each sprite.
  48. This is a static collision node. Then, as a child of the StaticBody2D,
  49. create a CollisionShape2D or CollisionPolygon. The later is recommended
  50. because it's easier to edit:
  51. .. image:: /img/tile_example3.png
  52. Finally, edit the polygon, this will give the tile a collision.
  53. **Remember to use snap!**. Using snap will make sure collision polygons
  54. are aligned properly, allowing a character to walk seamlessly from tile
  55. to tile. Also **do not scale or move** the collision and/or collision
  56. polygon nodes. leave them at offset 0,0, with scale 1,1 and rotation 0
  57. respect to the parent sprite.
  58. .. image:: /img/tile_example4.png
  59. Keep adding collisions to tiles untile we are done. Note that BG is just
  60. a BG so we don't care about it.
  61. .. image:: /img/tile_example5.png
  62. OK! We're done! Remember to save this scene for future edit, call it
  63. "tileset_edit.scn" or something like that.
  64. Exporting a TileSet
  65. -------------------
  66. With the scene created and opened in the editor, next step will be to
  67. create a tileset. Use Scene > Convert To > Tile Set from the Scene Menu:
  68. .. image:: /img/tileset_export.png
  69. Then choose a filename, like "mytiles.res". Make sure the "Merge With
  70. Existing" option is toggled on. This way, every time the tileset
  71. resource file is overwritten, existing tiles are merged and updated
  72. (they are referenced by their unique name, so again, **name your tiles
  73. properly**).
  74. .. image:: /img/tileset_merge.png
  75. Using the TileSet in a TileMap
  76. ------------------------------
  77. Create a new scene, use any node or node2d as root, then create a
  78. :ref:`TileMap <class_TileMap>` as
  79. a child.
  80. .. image:: /img/tilemap_scene.png
  81. Go to the tileset property of this node and assign the one created in
  82. previous steps:
  83. .. image:: /img/tileset_property.png
  84. Also set the cell size to '50', since that is the size used by the
  85. tiles. Quadrant size is a tuning value, which means that the engine will
  86. draw and cull the tilemap in blocks of 16x16 tiles. This value is
  87. usually fine and does not need to be changed, but can be used to tune
  88. performance in specific cases (if you know what you are doing).
  89. Painting your world
  90. -------------------
  91. With all set, make sure the TileMap node is selected. A red grid will
  92. appear on screen, allowing to paint on it with the selected tile on the
  93. left palette.
  94. .. image:: /img/tile_example6.png
  95. To avoid moving and selecting the tilemap node accidentally (something
  96. common given it's a huge node), it is recommended that you lock it,
  97. using the lock button:
  98. .. image:: /img/tile_lock.png
  99. Offset and scaling artifacts
  100. ----------------------------
  101. When using a single texture for all the tiles, scaling the tileset (or
  102. even moving to a non pixel-aligned location) will most likely result in
  103. filtering artifacts like this:
  104. .. image:: /img/tileset_filter.png
  105. This can't be avoided, as it is the way the hardware bilinear filter
  106. works. So, to avoid this situation, there are a few workarounds, try the
  107. ones that look better for you:
  108. - Use a single image for each tile, this will remove all artifacts but
  109. can be more cumbersome to implement, so first try the options below
  110. first.
  111. - Disable filtering for either the tileset texture or the entire image
  112. loader (see the :ref:`doc_managing_image_files` asset pipeline tutorial).
  113. - Enable pixel snap (set: "Scene > Project Settings >
  114. rasterizer/uxe_pixel_snap" to true).
  115. - Viewport Scaling can often help shrinking the map (see the
  116. :ref:`doc_viewports` tutorial).