using_tilemaps.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 we'll 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. When you do this, the "TileSet" panel will open at the bottom of the editor
  60. 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. Click "New Atlas" and drag to select the
  91. entire tile sheet.
  92. .. image:: img/tileset_atlas.png
  93. If you haven't already, make sure to change the "Step" in the snap settings to
  94. `(64, 64)`, or your tiles may be chopped into smaller pieces. You can find
  95. this in the Inspector:
  96. .. image:: img/tileset_snap.png
  97. Once you've defined the atlas you can add collision shapes to the individual
  98. tiles as before. You can also click "Icon" to select one of the tiles to represent
  99. the atlas.
  100. Back in the TileMap, you can select the atlas tile and you'll see all of the
  101. tiles it contains:
  102. .. image:: img/tileset_draw_atlas.png
  103. In addition to saving time when defining the tiles, this can help by grouping
  104. similar tiles together when you're working with a large number of tiles.
  105. Tips and tricks
  106. ---------------
  107. - If you're using a :ref:`Camera2D <class_Camera2D>` to scroll your level, you
  108. may notice lines appearing between your tiles. To fix this, open Project
  109. Settings and enable "Use Pixel Snap" in the "Rendering/Quality" section.
  110. - You can flip and rotate tiles using the icons at the top right of the editor.
  111. - To draw straight lines, hold <Shift> while clicking and dragging a tile.
  112. - Tools such as copy, paste, and bucket fill, can be found in the "TileMap"
  113. menu in the upper-right.
  114. .. image:: img/tilemap_menu.png