Bentley
Member
I'm working on destructible terrain using objects. I keep cutting blocks into 4th's until they reach a minimum size. I was going off this image: https://www.dropbox.com/s/ml4mggx7vjwvhko/90qQO.png?dl=0
The with loop looks at a lot of blocks that are no where near the collision area. Does anyone have any ideas for reducing the amount of instance's checked?
Just a heads up about the code: the sprites are named based on their size. So s_block_1 is a 1x1 sprite, s_block_16 is a 16x16 sprite, etc.. Also, they are ordered in the resource tree from smallest to largest so I can easily set the sprites. Here's the code:
o_control
Mouse left pressed
o_block
Destroy event
Thanks for reading.
The with loop looks at a lot of blocks that are no where near the collision area. Does anyone have any ideas for reducing the amount of instance's checked?
Just a heads up about the code: the sprites are named based on their size. So s_block_1 is a 1x1 sprite, s_block_16 is a 16x16 sprite, etc.. Also, they are ordered in the resource tree from smallest to largest so I can easily set the sprites. Here's the code:
o_control
Mouse left pressed
Code:
var rad = 8;
// While there are still blocks in the collision area
while (collision_circle(mouse_x, mouse_y, rad, o_block, false, false))
{
// Loop through all blocks
with (o_block)
{
// If this block is in collision
if (collision_circle(mouse_x, mouse_y, rad, id, false, false))
{
// If it's a 1x1 sprite
if (sprite_index == s_block_1)
{
// Destroy it and don't split (perform events = false)
instance_destroy(id, false);
}
// Otherwise it's a 2x2, 4x4, 8x8, or 16x16 sprite
else
{
// Destroy it and split (perform events = true)
instance_destroy(id, true);
}
}
}
}
Destroy event
Code:
// Ex: If the block's sprite is s_block_16 than "next_sprite" equals s_block_8
var next_sprite = sprite_index - 1;
// Create 4 blocks and assign them smaller sprites
with (instance_create_layer(x, y, layer, o_block))
{
sprite_index = next_sprite;
}
with (instance_create_layer(x + sprite_width / 2, y, layer, o_block))
{
sprite_index = next_sprite;
}
with (instance_create_layer(x, y + sprite_height / 2, layer, o_block))
{
sprite_index = next_sprite;
}
with (instance_create_layer(x + sprite_width / 2, y + sprite_height / 2, layer, o_block))
{
sprite_index = next_sprite;
}
Last edited: