using_tilemaps.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. _doc_using_tilemaps:
  2. Using tilemaps
  3. ~~~~~~~~~~~~~~
  4. Introduction
  5. ------------
  6. Tilemaps can be used to make levels for 2D games.
  7. A tilemap is a grid of (usually square) image tiles which form the scenery of a level or area in a game. The placed tiles in a tilemap are instances of 'reference tiles' from a tileset. The tileset can be thought of as a pallete with which a tilemap (level) can be 'painted':
  8. .. image:: img/tilemap.png
  9. Collision shapes can be added to the tiles in a tileset to create solid geometry. Tilemaps can be used for both side view and top-down 2D games.
  10. Making a tileset
  11. ----------------
  12. Here are some tiles for a tileset.
  13. They are all part of the same image file. This is helpful for a game's performance.
  14. There are so-called *texture packers* that will generate these spritesheets
  15. out of several separate tile images.
  16. But keeping them as separate images also works.
  17. .. image:: img/tileset.png
  18. Create a new project and move the above PNG image into the project directory. Next,
  19. go into the image's import settings and turn off ``Filter``. Keeping it on will cause
  20. issues later. ``Mipmaps`` should already be disabled; if not, disable this too.
  21. The easiest way to edit and maintain a tileset is exporting
  22. it from a specially prepared scene. We'll create that next.
  23. TileSet scene
  24. -------------
  25. Create a new scene with a regular Node or Node2D as root. For each reference tile you want to define,
  26. add a sprite node as a child. Since our tiles measure 50x50 pixels, you should turn on the grid
  27. (``View -> Show Grid`` or ``G`` key) and enable snap (``Use Snap`` icon or ``Shift + S`` keys).
  28. Moving tiles with the mouse might still be inaccurate,
  29. so use your arrow keys as well.
  30. If more than one tile is present in the source image, make sure to use
  31. the region property of the sprite to adjust which part of the texture is being
  32. used.
  33. Give the sprite node an appropriate and unique name. This will ensure
  34. that, in subsequent edits to the tileset (for example, if you've added
  35. collision, changed the region, etc), the tile will still be **identified
  36. correctly and updated**.
  37. Here's a screenshot that shows
  38. where everything of relevance is:
  39. .. image:: img/tile_example.png
  40. Add all the reference tiles in the way described above, adjusting the offsets as needed (that is, if you have
  41. multiple tiles in a single source image). Again, *remember that their names must
  42. be unique*.
  43. .. image:: img/tile_example2.png
  44. Collision Shapes
  45. ----------------
  46. To add collision shapes to the tiles, create a StaticBody2D child for each sprite.
  47. This is a static collision node. Then create a CollisionShape2D or
  48. CollisionPolygon as a child of the StaticBody2D. The CollisionPolygon is easier to edit.
  49. .. image:: img/tile_example3.png
  50. Edit the polygon; this will give the tile a collision shape and remove
  51. the warning icon next to the CollisionPolygon node. **Remember to use snap!**
  52. Using snap will make sure collision polygons are aligned properly, allowing
  53. a character to walk seamlessly from tile to tile. Also **do not scale or move**
  54. the collision and/or collision polygon nodes. Leave them at offset 0,0, with
  55. scale 1,1 and rotation 0 with respect to the parent sprite.
  56. .. image:: img/tile_example4.png
  57. Keep adding collision shapes to all tiles that need them. Note that BG is
  58. a background tile, so it should not have a collision shape.
  59. .. image:: img/tile_example5.png
  60. Then save this scene for future editing.
  61. "tileset_edit.scn" would be a sensible name for it.
  62. Exporting a TileSet
  63. -------------------
  64. With the scene created and still open in the editor, use Scene > Convert To > Tile Set from the Scene Menu:
  65. .. image:: img/tileset_export.png
  66. Then choose a filename, like "mytiles.tres". Make sure the "Merge With
  67. Existing" option is toggled on. This way, every time the tileset
  68. resource file is overwritten, existing tiles are merged and updated
  69. (they are referenced by their unique name, so again, **name your tiles
  70. properly**).
  71. .. image:: img/tileset_merge.png
  72. Using the TileSet in a TileMap
  73. ------------------------------
  74. Create a new scene, using any node or node2d as root, and then create a
  75. :ref:`TileMap <class_TileMap>` as
  76. a child.
  77. .. image:: img/tilemap_scene.png
  78. Go to the TileSet property of this node and assign the one created in
  79. previous steps:
  80. .. image:: img/tileset_property.png
  81. Also set the cell size to '50', since that is the size used by the
  82. tiles. Quadrant size is a tuning value. The default value of 16 means that the engine will
  83. draw and cull (erase) the tilemap in blocks of 16x16 tiles. This value is
  84. usually fine, but can be used to fine tune
  85. performance in specific cases if you know what you're doing.
  86. Painting your world
  87. -------------------
  88. Make sure the TileMap node is selected. A red grid will
  89. appear on the screen, allowing you to paint on it with the tile currently selected in the
  90. left palette.
  91. .. image:: img/tile_example6.png
  92. It's easy to accidentally select and move the tilemap node. To avoid this
  93. use the node's lock button:
  94. .. image:: img/tile_lock.png
  95. If you accidentally place a tile somewhere you don't want it to be, you
  96. can delete it with ``RMB`` (the right mouse button) while in the tilemap editor.
  97. You can flip and rotate sprites in the TileMap editor (note:
  98. flipping the sprite in the TileSet will have no effect). Icons at the
  99. top right of the editor allow flipping and rotating of the currently
  100. selected sprite - you can also use the A and S keys to flip the sprite
  101. horizontally and vertically. With a brick pattern like this tutorial uses,
  102. flipping the sprites would create unpleasant discontinuities unless you're
  103. flipping an entire region of bricks. But for some kinds of tiles, flipping
  104. can be a convenient and space-saving feature.
  105. Offset and scaling artifacts
  106. ----------------------------
  107. When using a single texture for all the tiles, scaling the tileset or moving it to a non pixel-aligned location can cause filtering artifacts:
  108. .. image:: img/tileset_filter.png
  109. To avoid this situation use the approach below that makes the most sense for your game:
  110. - Disable filtering and mipmaps for either the tileset texture or all tile textures if using separate images (see the :ref:`doc_import_images` asset pipeline tutorial).
  111. - Enable pixel snap (Set ``Project > Project Settings >
  112. Rendering > Quality > 2d > Use Pixel Snap`` to true; you can also search for ``Pixel Snap``).
  113. - Viewport Scaling can often help with shrinking the map (see the
  114. :ref:`doc_viewports` tutorial). Adding a camera, setting it to ``Current`` and playing around with its ``Zoom`` may be a good starting point.
  115. - You can use a single, separate image for each tile. This will remove all artifacts, but
  116. can be more cumbersome to implement and is less optimized.