using_tilemaps.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. .. _doc_using_tilemaps:
  2. Using TileMaps
  3. ==============
  4. Introduction
  5. ------------
  6. A tilemap is a grid of tiles used to create a game's layout. There are several
  7. benefits to using :ref:`TileMap <class_TileMap>` nodes to design your levels.
  8. First, they make it possible to draw the layout by "painting" the tiles onto a
  9. grid, which is much faster than placing individual :ref:`Sprite <class_Sprite>`
  10. nodes one by one. Second, they allow for much larger levels because they are
  11. optimized for drawing large numbers of tiles. Finally, you can add collision,
  12. occlusion, and navigation shapes to tiles, adding additional functionality to
  13. the TileMap.
  14. .. image:: img/tileset_draw_atlas.png
  15. Project setup
  16. -------------
  17. This demo will use the following tiles taken from Kenney's "Abstract Platformer"
  18. art pack. You can find the complete set `here <https://kenney.nl/assets/abstract-platformer>`_
  19. but for this demo we'll stick to this small set.
  20. .. image:: img/tilesheet.png
  21. Create a new project and place the above image in the project folder.
  22. When using a tileset, it's important that adjacent tiles match up. Godot's default
  23. is to import 2D images using an interpolated "filter" mode, which will result in
  24. ugly borders between the tiles. Select the image and click the Import tab. Turn
  25. off ``Filter`` and click "Reimport". See :ref:`doc_import_images` for details.
  26. TileMap node
  27. ------------
  28. Add a new :ref:`TileMap <class_TileMap>` node to the scene. By default, a TileMap
  29. uses a square grid of tiles. You can also use a perspective-based "Isometric" mode
  30. or define your own custom tile shape.
  31. .. image:: img/tilemap_mode.png
  32. Under the "Cell" section in the Inspector are many properties you can adjust to
  33. customize your tilemap's behavior:
  34. .. image:: img/tilemap_size.png
  35. - ``Cell Size``
  36. This defines the size of the grid. This should match the pixel size
  37. of your tiles. The default value is ``(64, 64)``.
  38. - ``YSort``
  39. This causes tiles to be drawn in order of their ``Y`` position, so that
  40. "lower" tiles are drawn on top of "higher" ones.
  41. - ``Half Offset`` and ``Tile Origin``
  42. These properties affect the position of the tile relative to the grid position.
  43. - ``Quadrant``
  44. Defines the chunk size used for batched drawing. This can negatively
  45. affect performance. Don't change it unless you know what you're doing.
  46. - ``Custom Transform``
  47. Used to alter the tile's shape. Use this if you have non-square tiles.
  48. All of these options can be left at their defaults for this demo.
  49. Creating a TileSet
  50. ------------------
  51. Once you've configured your tilemap, it's time to add a
  52. :ref:`TileSet <class_TileSet>`. A TileSet is a
  53. :ref:`Resource <class_Resource>` that contains the data about your
  54. tiles - their textures, collision shapes, and other properties. When the game
  55. runs, the TileMap combines the individual tiles into a single object.
  56. To add a new TileSet, click on the "Tile Set" property and select "New
  57. TileSet".
  58. .. image:: img/tilemap_add_tileset.png
  59. Click on the TileSet property, and the "TileSet" panel will open at the bottom
  60. of the editor window:
  61. .. image:: img/tilemap_tool.png
  62. First, you need to add the texture(s) that you'll use for the tiles. Click the
  63. "Add Texture(s) to TileSet" button and select the ``tilesheet.png`` image.
  64. Next, click "New Single Tile" and drag in the image to select the tile you want.
  65. Click the "Enable Snap" button to make it easier to select the entire tile. A
  66. yellow rectangle appears around the selected tile.
  67. .. image:: img/tilemap_add_tile.png
  68. Click on the TileMap in the scene tree, and you'll see that the newly created
  69. tile now appears on the right side. Click in the viewport and you can place
  70. tiles. Right-click to remove them.
  71. .. image:: img/tilemap_draw.png
  72. It's easy to accidentally select and move the tilemap node. To avoid this, use
  73. the node's lock button:
  74. .. image:: img/tile_lock.png
  75. Collision shapes
  76. ----------------
  77. If you're making a map that needs collisions - walls, floor, or other obstacles,
  78. for example - then you'll need to add collision shapes to any tiles that you
  79. want to be considered "solid".
  80. Click "TileSet" at the bottom of the editor window to return to the tileset
  81. tool. Click the tile you previously defined (outlined in yellow). Select the
  82. "Collision" tab and click the "Create a new rectangle" button. Make sure you
  83. still have grid snap enabled, then click and drag in the tile. A square
  84. collision shape appears in light blue:
  85. .. image:: img/tileset_add_collision.png
  86. You can add occlusion and navigation shapes to the tile in the same way.
  87. Atlas tiles
  88. -----------
  89. Rather than adding individual tiles one at a time, you can define a group of
  90. tiles all at once using an atlas. This also allows you to randomly generate
  91. tiles from the group.
  92. Click "New Atlas" and drag to select the entire tile sheet.
  93. .. image:: img/tileset_atlas.png
  94. If you haven't already, make sure to change the "Step" in the snap settings to
  95. `(64, 64)`, or your tiles may be chopped into smaller pieces. You can find
  96. this in the Inspector:
  97. .. image:: img/tileset_snap.png
  98. Once you've defined the atlas you can add collision shapes to the individual
  99. tiles as before. You can also click "Icon" to select one of the tiles to represent
  100. the atlas.
  101. Back in the TileMap, you can select the atlas tile and you'll see all of the
  102. tiles it contains:
  103. .. image:: img/tileset_draw_atlas.png
  104. In addition to saving time when defining the tiles, this can help by grouping
  105. similar tiles together when you're working with a large number of tiles.
  106. Random tile priorities
  107. ~~~~~~~~~~~~~~~~~~~~~~
  108. When drawing with atlas tiles, enabling the "Use priority" option causes tiles
  109. to be selected at random. By default, each tile in the tileset has an equal
  110. likelihood of occurring. You can change the likelihood by setting different
  111. priorities for each tile. For example, a tile with priority 2 is twice as
  112. likely to be selected as a tile with priority 1, and a tile with priority 3 is
  113. 50% more likely to be selected than a tile with priority 2.
  114. Autotiles
  115. ---------
  116. Autotiles allow you to define a group of tiles, then add rules to control which
  117. tile gets used for drawing based on the content of adjacent cells.
  118. Click "New Autotile" and drag to select the tiles you wish to use. You can add
  119. collisions, occlusion, navigation shapes, tile priorties, and select an icon
  120. tile in the same manner as for atlas tiles.
  121. Tile selection is controlled by bitmasks. Bitmasks can be added by clicking
  122. "Bitmask", then clicking parts of the tiles to add or remove bits in the mask.
  123. Left-clicking an area of the tile adds a bit, right-click removes "off",
  124. and shift-left-click sets an "ignore" bit.
  125. Whenever Godot updates a cell using an autotile, it first creates a pattern
  126. based on which adjacent cells are already set. Then, it searches the autotile
  127. for a single tile with a bitmask matching the created pattern. If no matching
  128. bitmask is found, the "icon" tile will be used instead. If more than one
  129. matching bitmask is found, one of them will be selected randomly, using the
  130. tile priorities.
  131. The rules for matching a bitmask to a pattern depend on the tileset's autotile
  132. bitmask mode. This can be set in the "Inspector" tab, under the "Selected Tile"
  133. heading. Allowed values are "2x2", "3x3 (minimal)", and "3x3".
  134. All "on" and "off" bits must be satisfied for a bitmask to match, but "ignore"
  135. bits are ignored.
  136. 2x2
  137. ~~~
  138. In 2x2 mode, each bitmask contains four bits, one for each corner.
  139. Where a bit is "on", all cells connected to that corner must be filled using
  140. the same autotile, in order for the bitmask to match.
  141. For example, if the top-left bit is set, the cell directly above,
  142. directly left, and diagonally above-left must be filled.
  143. Where a bit is "off", at least one cell connected to that corner must not be
  144. set using the same autotile.
  145. At least one bit must be set for the tile to be used, so a total of 15 tiles
  146. would be needed to provide exactly one tile for each arrangement that this mode
  147. can test for.
  148. 2x2 mode can only match cells that are part of a 2-by-2 block - cells with no
  149. neighbors and lines only one cell wide are not supported.
  150. **Template - Generic:**
  151. This template can be used for sideways or fully top-down perspectives.
  152. It's designed for a TileMap cell size of 64x64.
  153. Key:
  154. - Red: "on"
  155. - White: "off"
  156. .. image:: img/autotile_template_2x2.png
  157. 3x3 (minimal)
  158. ~~~~~~~~~~~~~
  159. In 3x3 (minimal) mode, each bitmask contains 9 bits (4 corners, 4 edges,
  160. 1 center). The 4 corner bits work the same as in 2x2 mode.
  161. When an edge bit is "on", the cell which shares that edge must be filled.
  162. When an edge bit is "off", the cell which shares that edge must be empty.
  163. The center bit should be "on" for any tile you wish to use. Note that in this
  164. mode, it makes no sense for a corner bit to be "on" when either edge bit
  165. adjacent to it is not "on".
  166. A total of 47 tiles would be needed to provide exactly one bitmask for each
  167. arrangement that this mode can test for.
  168. .. note::
  169. Right-click an image and choose **Save image as…** to save it.
  170. **Template - Generic:**
  171. This template can be used for sideways or fully top-down perspectives.
  172. All templates below are designed for a TileMap cell size of 64x64, but you may
  173. have to use different subtile sizes for top-down templates as described below.
  174. Key:
  175. - Red: "on"
  176. - White: "off"
  177. .. image:: img/autotile_template_3x3_minimal.png
  178. **Template - Generic 16 tiles:**
  179. This template can be used for tilesets that only have 16 tiles - for simpler art
  180. styles the missing tiles will not be noticeable.
  181. Key:
  182. - Red: "on"
  183. - White: "off"
  184. - Blue-checkered: "ignore"
  185. .. image:: img/autotile_template_3x3_minimal_16.png
  186. **Template - Top-down floor in 3/4 perspective:**
  187. Key (applies to the four templates below):
  188. - Green: floor
  189. - Cyan: wall
  190. - Yellow: top of wall
  191. - Grey: hidden due to overlap
  192. - Transparent: air
  193. .. image:: img/autotile_template_3x3_minimal_topdown_floor.png
  194. **Template - Top-down wall in 3/4 perspective:**
  195. .. image:: img/autotile_template_3x3_minimal_topdown_walls.png
  196. **Template - Top-down wall in 3/4 perspective (thick walls):**
  197. When using this template, set the TileSet subtile size to ``Vector2(64, 88)``.
  198. .. image:: img/autotile_template_3x3_minimal_topdown_walls_thick.png
  199. **Template - Top-down wall in 3/4 perspective (tall walls):**
  200. When using this template, set the "Snap Options" Step to ``Vector2(64, 184)``
  201. and the "Selected Tile" Texture offset to height minus the cell size.
  202. This means the texture offset should be ``Vector2(0, -120)``:
  203. .. image:: img/autotile_template_3x3_minimal_topdown_walls_tall.png
  204. 3x3
  205. ~~~
  206. In 3x3 mode, each bitmask contains 9 bits (4 corners, 4 edges, 1 center)
  207. Each bit checks a single adjacent cell. Corner bits only check diagonally
  208. adjacent cells. The center bit should be "on" for any tile you wish to use.
  209. A total of 256 tiles would be needed to provide exactly one bitmask for each
  210. arrangement that this mode can test for.
  211. Disabling autotile
  212. ~~~~~~~~~~~~~~~~~~
  213. When using an autotile, it is possible to turn off the autotile behaviour and
  214. select tiles manually, by clicking "Disable Autotile" at the top of the tile
  215. selection window.
  216. Autotile binding
  217. ~~~~~~~~~~~~~~~~
  218. By default, autotile only checks for adjacent cells filled using the same
  219. autotile. This behaviour can be overridden in order to have autotiles bind to
  220. each other, or even bind to empty cells. At present, this can only be done
  221. through scripting. You will need to add a script to your tileset, and define
  222. a function named "_is_tile_bound(drawn_id, neighbor_id)". This function will
  223. be called for each adjacent cell that does not contain the same autotile, and
  224. should return true if you want the drawn cell to "bind" to the neighbor cell.
  225. You can find the id of an autotile using "find_tile_by_name(name)", empty cells
  226. are given an id of -1.
  227. Note that to use this in the editor, the script should start with a "tool"
  228. declaration, and you may need to close and reload the scene for these changes
  229. to take effect.
  230. Tips and tricks
  231. ---------------
  232. - If you're using a :ref:`Camera2D <class_Camera2D>` to scroll your level, you
  233. may notice lines appearing between your tiles. To fix this, open Project
  234. Settings and enable **Use Gpu Pixel Snap** in the **Rendering > 2d > Snapping** section.
  235. - You can flip and rotate tiles using the icons at the top right of the editor.
  236. - To draw straight lines, hold :kbd:`Shift` while clicking and dragging a tile.
  237. - Tools such as copy, paste, and bucket fill, can be found in the "TileMap"
  238. menu in the upper-right.
  239. .. image:: img/tilemap_menu.png