tilemap.rst 5.6 KB

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