Here is how I would do it, but I have no idea how you would do collision detection with this method:
You make a black and white image that is the same size as the level, where one color is for solid parts of the level (the light brown dirt in the video above. Let's assume you chose white) and the other color is for the non-solid parts of the level (the dark brown dirt in the video above. Let's assume you chose black). This image will be a bit mask that will be used to figure out which parts of the level is solid dirt, and which parts are the background/non-solid-dirt.
Then you have two images/sprites, one for the solid dirt and another for the background/non-solid-dirt. On the solid dirt you add a shader that uses the black and white bit mask, only rendering pixels that are in the white section. I made a shader that does something similar to this in Godot 2. It could probably be adjusted to work with Godot 3, and to use a inputted texture instead of the screen texture.
Then when you want to destroy part of the ground, you add some black pixels to the bit mask at the location where the destruction occurred.
You could use a Viewport
texture for the black and white bit mask if you wanted, which may make performance a little better (since drawing to a image is slow). By using the _draw
function provided in Node2D
, you should be able to get fairly fast performance adding black and white sections to the bit mask.
Then, whenever you update the black and white bitmask, you update the shader, and then it should look like part of the terrain was destroyed, since the shader will exclude areas that are black from the final image.
The only problem with using a shader for environment destruction like this is collision detection. I have no idea how to go about adding collision using this method. Maybe in Godot 3.1 when the mesh sprites come in you could auto generate a collider from the bit mask using the mesh sprite? In theory you could use the bit mask for collision detection and then get pixel perfect collision detection, but I have no idea on how to go about doing that.
I took a quick look through the source on Github, and at first glance it looks like this is how they are doing it. They are not using a bit mask though, instead it looks like they are just removing pixels from a copy of the level image in memory, but the idea is more or less the same. When something hits the environment, they remove a section of the pixels in the level image at the collision position, then they update the collision (I assume), and the game goes on.
I'm still not sure how to handle the collision detection though...