using_tilemaps.rst 6.1 KB

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