using_tilemaps.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Collisions can also be added to the tiles, allowing for both 2D side
  11. scrolling and top down games.
  12. Making a tileset
  13. ----------------
  14. To begin, 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.
  17. .. image:: img/tileset.png
  18. Create a new project and move the above png image into the directory. Next
  19. go into the image settings and turn off ``Filter``, keeping it on will cause
  20. issues later.
  21. We will be creating a :ref:`TileSet <class_TileSet>`
  22. resource. While this resource exports properties, it's pretty difficult
  23. to get complex data into it and maintain it. Here is what it would look like to
  24. manually edit the resource:
  25. .. image:: img/tileset_edit_resource.png
  26. There's enough properties to get by. With some effort, editing this
  27. way can work. But the easiest way to edit and maintain a tileset is exporting
  28. it from a specially-crafted scene!
  29. TileSet scene
  30. -------------
  31. Create a new scene with a regular node or node2d as root. For each tile,
  32. add a sprite as a child. Since tiles here are 50x50, you should turn on the grid
  33. (``G`` key) and enable snap. Moving tiles with the mouse might still be a innacurate
  34. so use your arrow keys as well.
  35. If more than one tile is present in the source image, make sure to use
  36. the region property of the sprite to adjust which part of the texture is being
  37. used.
  38. Finally, make sure to name your sprite node correctly. This will ensure
  39. that, in subsequent edits to the tileset (for example, if added
  40. collision, changed the region, etc), the tile will still be **identified
  41. correctly and updated**. This name should be unique.
  42. Sounds like a lot of requirements, so here's a screenshot that shows
  43. where everything of relevance is:
  44. .. image:: img/tile_example.png
  45. Continue adding all the tiles, adjusting the offsets if needed (that is, if you have
  46. multiple tiles in a single source image). Again, *remember that their names must
  47. be unique*.
  48. .. image:: img/tile_example2.png
  49. Collision
  50. ---------
  51. To add collision to a tile, create a StaticBody2D child for each sprite.
  52. This is a static collision node. Then create a CollisionShape2D or
  53. CollisionPolygon as a child of the StaticBody2D. The CollisionPolygon is
  54. recommended because it is easier to edit.
  55. .. image:: img/tile_example3.png
  56. Finally, edit the polygon, this will give the tile a collision, and fix
  57. the warning icon next to the CollisionPolygon node. **Remember to use snap!**
  58. Using snap will make sure collision polygons are aligned properly, allowing
  59. a character to walk seamlessly from tile to tile. Also **do not scale or move**
  60. the collision and/or collision polygon nodes. Leave them at offset 0,0, with
  61. scale 1,1 and rotation 0 with respect to the parent sprite.
  62. .. image:: img/tile_example4.png
  63. Keep adding collisions to the tiles until we are done. Note that BG is just
  64. a background, so it should not have a collision.
  65. .. image:: img/tile_example5.png
  66. OK! We're done! Remember to save this scene for future edit. Name it
  67. "tileset_edit.scn" or something like that.
  68. Exporting a TileSet
  69. -------------------
  70. With the scene created and opened in the editor, the next step will be to
  71. create a tileset. Use Scene > Convert To > Tile Set from the Scene Menu:
  72. .. image:: img/tileset_export.png
  73. Then choose a filename, like "mytiles.tres". Make sure the "Merge With
  74. Existing" option is toggled on. This way, every time the tileset
  75. resource file is overwritten, existing tiles are merged and updated
  76. (they are referenced by their unique name, so again, **name your tiles
  77. properly**).
  78. .. image:: img/tileset_merge.png
  79. Using the TileSet in a TileMap
  80. ------------------------------
  81. Create a new scene, using any node or node2d as root, and then create a
  82. :ref:`TileMap <class_TileMap>` as
  83. a child.
  84. .. image:: img/tilemap_scene.png
  85. Go to the TileSet property of this node and assign the one created in
  86. previous steps:
  87. .. image:: img/tileset_property.png
  88. Also set the cell size to '50', since that is the size used by the
  89. tiles. Quadrant size is a tuning value, which means that the engine will
  90. draw and cull the tilemap in blocks of 16x16 tiles. This value is
  91. usually fine and does not need to be changed, but can be used to fine tune
  92. performance in specific cases (if you know what you are doing).
  93. Painting your world
  94. -------------------
  95. With all set, make sure the TileMap node is selected. A red grid will
  96. appear on screen, allowing to paint on it with the selected tile on the
  97. left palette.
  98. .. image:: img/tile_example6.png
  99. To avoid accidentally moving and selecting the tilemap node (something
  100. common, given it's a huge node), it is recommended that you lock it,
  101. using the lock button:
  102. .. image:: img/tile_lock.png
  103. If you accidentally place a tile somewhere you don't want it to be, you
  104. can delete it with ``RMB`` while in the tilemap editor.
  105. You can also flip and rotate sprites in the TileMap editor (note:
  106. flipping the sprite in the TileSet will have no effect). Icons at the
  107. top right of the editor allow flipping and rotating of the currently
  108. selected sprite - you can also use the A and S keys to flip the sprite
  109. horizontally and vertically. With a brick pattern like this tutorial uses,
  110. flipping the sprites would create unpleasant discontinuities unless you're
  111. flipping an entire region of bricks. But for some kinds of tiles, flipping
  112. can be a convenient and space-saving feature.
  113. Offset and scaling artifacts
  114. ----------------------------
  115. When using a single texture for all the tiles, scaling the tileset (or
  116. even moving to a non pixel-aligned location) will most likely result in
  117. filtering artifacts like so:
  118. .. image:: img/tileset_filter.png
  119. This is unavoidable, as it is the way the hardware bilinear filter
  120. works. So, to avoid this situation, there are a few workarounds. Try the
  121. one that looks better for you:
  122. - Disable filtering for either the tileset texture or the entire image
  123. loader (see the :ref:`doc_import_images` asset pipeline tutorial).
  124. - Enable pixel snap (set: "Scene > Project Settings >
  125. Display/use_2d_pixel_snap" to true).
  126. - Viewport Scaling can often help with shrinking the map (see the
  127. :ref:`doc_viewports` tutorial).
  128. - Use a single image for each tile. This will remove all artifacts, but
  129. can be more cumbersome to implement.