GithubHelp home page GithubHelp logo

hiulit / godot-3-2d-destructible-objects Goto Github PK

View Code? Open in Web Editor NEW
384.0 9.0 27.0 2.27 MB

A script that takes a sprite, divides it into blocks and makes them explode ๐Ÿ’ฅ!

License: MIT License

GDScript 100.00%
godot-engine 2d destructible objects

godot-3-2d-destructible-objects's Introduction

Godot 3 2D Destructible Objects

A script that takes a sprite, divides it into blocks and makes them explode ๐Ÿ’ฅ!

Godot-3-2D-Destructible-Objects

Limitations

Right now, the sprites must be squares or rectangles for this script to work properly.

Prerequisites

Each destructible object must follow this structure and must be its own Scene file.

RigidBody2D
โ”œโ”€โ”€ Sprite
โ””โ”€โ”€ CollisionShape2D
    โ””โ”€โ”€ RectangleShape2D

Usage

  • Create a Node2D that will contain all the destructibles objects (e.g. destructible_objects).
  • Add a Node2D as a child node of the prior Node2D (e.g. destructible_object_01).
  • Instance the destructible_object scene file.
  • Attach explode_object.gd to the destructible object as a Script.

Godot-3-2D-Destructible-Objects-Tree

The reason for organizing it this way is because then you can add particles (Partcicles2D or 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 RigidBody2D and 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)

Parameters

Godot-3-2D-Destructible-Objects-Parameters

Blocks Per Side

Name Type Description Default
blocks_per_side int The blocks per side. Minium 2. Maximum 10 (for performance reasons). 6

Example: 4 block per side makes a total of 16 blocks.

Blocks Impulse

Name Type Description Default
blocks_impulse float The force of the blocks when they explode. 600

Blocks Gravity Scale

Name Type Description Default
blocks_gravity_scale float The gravity of the blocks. 10

Debris max time

Name Type Description Default
debris_max_time float The seconds it will pass until the blocks become STATIC or, if remove_debris is set to true, they dissapear. 5

Remove debris

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_time is over. falseย 

Collision layers

Name Type Description Default
collision_layers int The collision layers of the blocks. 1

Sum all the values of the layers.

Example: Layer 1 value is 1. Layer 5 value is 16. So collision_layers would be 17.

Collision masks

Name Type Description Default
collision_masks int The collision masks of the blocks. 1

Sum all the values of the layers.

Example: Layer 1 value is 1. Layer 5 value is 16. So collision_layers would be 17.

Collision one way

Name Type Description Default
collision_one_way bool Set one_way_collision for the blocks. false

Explosion delay

Name Type Description Default
explosion_delay bool Adds a delay of before setting object.detonate to false. false

Sometimes object.detonate is set to false so quickly that the explosion never happens. If this happens, try setting explosion_delay to true.

Fake explosions group

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.

Randomize seed

Name Type Description Default
randomize_seed bool Randomize the seed. false

Debug mode

Name Type Description Default
debug_mode bool Prints some debug data. false

Changelog

See CHANGELOG.

Authors

Credits

Thanks to:

  • Airvikar - For this Youtube video that is the code base for this script.
  • Securas - For all the great games and Twitch streams that give me lots of ideas, and particularly, the destructible objects one.
  • Scott Lembcke - For letting me know about Voronoi regions (which aren't currently available) and helping me with adding more depth to the explosion (random collisions and z-index).

License

MIT License.

godot-3-2d-destructible-objects's People

Contributors

hiulit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

godot-3-2d-destructible-objects's Issues

Add Voronoi regions

To have a better scattering.

D-zZ1v8XUAAg_Gf

We will need a way to create sprites and its respective collisions following this pattern.

Fake Particles visiblity

Pretty easy one, but probably worth mentioning that fake particles can be visible and distracting pre explosion as shown in this simple low bit pixel game I'm working on.

image

The yellow there is the bunched up fake particles I assume.

Easy fix is to set visible = false in _ready, and visible = true before _particles_explode(delta), unless I'm missing some further nuance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.