A script that takes a sprite, divides it into blocks and makes them explode 💥!
A script that takes a sprite, divides it into blocks and makes them explode 💥!
Right now, the sprites must be squares or rectangles for this script to work properly.
Each destructible object must follow this structure and must be its own
Scenefile.
RigidBody2D ├── Sprite └── CollisionShape2D └── RectangleShape2D
Node2Dthat will contain all the destructibles objects (e.g.
destructible_objects).
Node2Das a child node of the prior
Node2D(e.g.
destructible_object_01).
destructible_objectscene file.
explode_object.gdto the destructible object as a
Script.
The reason for organizing it this way is because then you can add particles (
Partcicles2Dor
CPUParticles2D), fake particles (like the ones provided with this project), hitboxes (
Area2D) or whatever you feel like to the
Node2D(e.g.
destructible_object_01) holding the main
RigidBody2Dand you can then use this script to control those nodes.
Of course, you can recreate that tree in GDSscript, with something like this:
var node = Node2D.new() node.name = "destructible_container" get_parent().add_child(node, true)var rigid_body = RigidBody2D.new() rigid_body.name = "destructible_object"
var sprite = Sprite.new()
Set the sprite's texture, size, etc.
sprite.texture = preload("res://path/to/texture.png") ...
var collision = CollisionShape2D.new() collision.shape = RectangleShape2D.new() collision.shape.extents = Vector2(..., ...)
rigid_body.add_child(sprite, true) rigid_body.add_child(collision, true)
var script = preload("res://path/to/explode_object.gd") rigid_body.set_script(script)
Here you can set the 'rigid_body' variables from the script.
rigid_body.blocks_per_side = ... rigid_body.blocks_impulse = ...
node.add_child(rigid_body, true)
| Name | Type | Description | Default | | --- | --- | --- | --- | |
blocks_per_side|
int| The blocks per side. Minium
2. Maximum
10(for performance reasons). |
6|
Example:
4block per side makes a total of
16blocks.
| Name | Type | Description | Default | | --- | --- | --- | --- | |
blocks_impulse|
float| The force of the blocks when they explode. |
600|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
blocks_gravity_scale|
float| The gravity of the blocks. |
10|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
debris_max_time|
float| The seconds it will pass until the blocks become
STATICor, if
remove_debrisis set to
true, they dissapear. |
5|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
remove_debris|
bool| Controls whether the debris stays or disappears. If set to
true, the debris will dissapear when
debris_max_timeis over. |
false|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
collision_layers|
int| The collision layers of the blocks. |
1|
Sum all the values of the layers.
Example:
Layer 1value is
1.
Layer 5value is
16. So
collision_layerswould be
17.
| Name | Type | Description | Default | | --- | --- | --- | --- | |
collision_masks|
int| The collision masks of the blocks. |
1|
Sum all the values of the layers.
Example:
Layer 1value is
1.
Layer 5value is
16. So
collision_layerswould be
17.
| Name | Type | Description | Default | | --- | --- | --- | --- | |
collision_one_way|
bool| Set
one_way_collisionfor the blocks. |
false|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
explosion_delay|
bool| Adds a delay of before setting
object.detonateto
false. |
false|
Sometimes
object.detonateis set to
falseso quickly that the explosion never happens. If this happens, try setting
explosion_delayto
true.
| Name | Type | Description | Default | | --- | --- | --- | --- | |
fake_explosions_group|
String| Renames the group's name of the fake explosion particles. |
fake_explosion_particles|
This project provides an extra script for creating fake explosion particles. That script uses a group name to be able to find the fake explosion particles more easily.
| Name | Type | Description | Default | | --- | --- | --- | --- | |
randomize_seed|
bool| Randomize the seed. |
false|
| Name | Type | Description | Default | | --- | --- | --- | --- | |
debug_mode|
bool| Prints some debug data. |
false|
See CHANGELOG.
Thanks to: