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, and 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 (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, as a child of the StaticBody2D,
  50. create a CollisionShape2D or CollisionPolygon. The latter is recommended
  51. 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. respect to the parent sprite.
  59. .. image:: /img/tile_example4.png
  60. Keep adding collisions to 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, call it
  64. "tileset_edit.scn" or something like that.
  65. Exporting a TileSet
  66. -------------------
  67. With the scene created and opened in the editor, 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.res". 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, use any node or node2d as root, 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 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 moving and selecting the tilemap node accidentally (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 A and S to flip horizontally and
  104. vertically. With a brick pattern like this tutorial uses, flipping the
  105. sprites would create unpleasant discontinuities unless you're flipping
  106. an entire region of bricks, but for some kinds of tiles flipping can
  107. 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 this:
  113. .. image:: /img/tileset_filter.png
  114. This can't be avoided, as it is the way the hardware bilinear filter
  115. works. So, to avoid this situation, there are a few workarounds, try the
  116. ones that look better for you:
  117. - Use a single image for each tile, this will remove all artifacts but
  118. can be more cumbersome to implement, so try the options below first.
  119. - Disable filtering for either the tileset texture or the entire image
  120. loader (see the :ref:`doc_managing_image_files` asset pipeline tutorial).
  121. - Enable pixel snap (set: "Scene > Project Settings >
  122. Display/use_2d_pixel_snap" to true).
  123. - Viewport Scaling can often help with shrinking the map (see the
  124. :ref:`doc_viewports` tutorial).