This is deadbox . With it we want to explore the design and the development of a platform game in GodotEngine.
Today we are going to talk about One-way Collision box. Here is an example of our little hero with a one way collision box.
Example of Deadbox jumping on and off one-way collision tiles
I see lot of tutorial that explains how to add manually a box and CollisionShape2D directly from Godot. But I haven’t seen anything on Tiled.
The maps in our games are made in Tiled. This allows us to build them in a easy way, and use all the fantastic features that Tiled has to offer. In another article we’d post something about our tileset setup, check it out here.
Below there is an image that shows our rules.tmx file. You can see the top left image showing the mapping layers:
- harmful: layer for the elements of the map that hurt the player (or anything actually)
- solid: layer for the collision elements, like wall, floor, ceiling, platform
- bouncy: layer that allows the player to bounce when step on it (To be implemented yet)
- water: layer object for the water tile (more in a future post)
- jumpthrough: layer that will identify the one-way platform
- regions: layer that allows the automapping for Tiled
- art: layer that identifies our own tileset
In order to understand better the automapping, I suggest to read the docs.
properties of one-way collision tile. As you can see ID is 122
tile that will map our main layer (“art”)
In the top right picure we have the properties set of the above mentioned tile. We need to know its ID, in order to let Godot know what is the tile to which apply the function tile_set.tile_set_shape_one_way. Function that is not documented, that allows us to set, via code, CollisionShape2D to One way collision (the same as checking the box “One Way Collision” in Godot).
After having the tilemap imported we need to find the proper layer and put the right CollisionShape to it. Unfortunately for us, the script doesn’t allow, yet, handle types of tiles in a better way than we did it. A big shout out to the developer of this incredible plugin and hopefully the 3.1 version will be released soon enough, as stated here in this issue.
We adopted this workaround: on the gdscript of the root node World we add the following script
# enable one-ways collision in jumpthrough layer
var jtl = map.get_node('jumpthrough')
# workaround - waiting for a better implementation in godot-tiled-importer
# reference: https://github.com/vnen/godot-tiled-importer/issues/37
jtl.tile_set.tile_set_shape_one_way(123, 0, true)
#123 here is the index of the current tile into the tileset. For some reason the imported tileset starts the index from 1 instead of 0.
#0 is the collisionShape associated to that tile in the tileset.
This workaround is not stable because there is no way to find out the index of the CollisionShape that the plugin gives to the tile when importing the tilemap. That might change if you reimport it with a new map.
We might be wrong about the automapping and setting the right properties, let us know with a comment down below. We would like to know if there is anoter or a better way.
Fail and learn. 🙂
See you next time!