2026-11-08 13:41:01 -06:00
|
|
|
class_name PixelBurst
|
|
|
|
|
extends Node2D
|
|
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
# Breaks a sprite's current frame into chunks of CHUNK_SIZE x CHUNK_SIZE pixels.
|
|
|
|
|
# Each chunk is a small physics body, so they bounce off the level and pile up
|
|
|
|
|
# on each other. Black (outline) and transparent pixels are left out; a chunk
|
|
|
|
|
# with nothing else in it isn't made at all.
|
|
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 2 # Sprite pixels per side of a chunk. 1 = a body per pixel.
|
2026-11-08 13:41:01 -06:00
|
|
|
const BLACK_THRESHOLD = 0.1 # Pixels with all channels below this count as black.
|
|
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
# Chunks are on their own physics layer (3) so the slime doesn't bump into them.
|
|
|
|
|
const CHUNK_LAYER = 1 << 2
|
|
|
|
|
const CHUNK_MASK = 1 | CHUNK_LAYER # The level, hazards, and other chunks.
|
|
|
|
|
# A hair smaller than the chunk so neighbours don't start out overlapping.
|
|
|
|
|
const SHAPE_SHRINK = 0.1
|
|
|
|
|
const MAX_PUSH_OUT = 16 # Chunks spawned inside the level move out by up to this.
|
|
|
|
|
# How long chunks ignore the `exclude` bodies, e.g. to get out from under the piston head.
|
|
|
|
|
const EXCLUDE_TIME = 1.0
|
|
|
|
|
|
|
|
|
|
const CHUNK_FRICTION = 0.8
|
|
|
|
|
const CHUNK_BOUNCE = 0.2
|
|
|
|
|
const CHUNK_MASS = 0.05
|
|
|
|
|
|
|
|
|
|
# How hard chunks squirt out sideways from under whatever pushed them, and back against it.
|
2026-11-08 13:41:01 -06:00
|
|
|
const SIDEWAYS_SPEED_MIN = 40.0
|
|
|
|
|
const SIDEWAYS_SPEED_MAX = 220.0
|
|
|
|
|
const BACK_SPEED_MIN = 20.0
|
|
|
|
|
const BACK_SPEED_MAX = 180.0
|
|
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
var chunks: Array[RigidBody2D] = []
|
2026-11-08 13:41:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# push: the direction the sprite was hit towards (e.g. DOWN for a piston from above).
|
2026-11-08 14:09:00 -06:00
|
|
|
# exclude: bodies the chunks pass through for EXCLUDE_TIME (e.g. the piston head).
|
2026-11-08 13:41:01 -06:00
|
|
|
func burst_from_sprite(sprite: AnimatedSprite2D, push: Vector2, exclude: Array[RID] = []) -> void:
|
|
|
|
|
top_level = true
|
|
|
|
|
global_position = Vector2.ZERO
|
|
|
|
|
z_index = sprite.z_index + 1
|
|
|
|
|
|
|
|
|
|
var texture := sprite.sprite_frames.get_frame_texture(sprite.animation, sprite.frame)
|
|
|
|
|
var image := _get_image(texture)
|
|
|
|
|
if image == null:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var size := image.get_size()
|
|
|
|
|
var spriteTransform := sprite.get_global_transform()
|
|
|
|
|
var center := sprite.global_position
|
|
|
|
|
var sideways := push.orthogonal()
|
2026-11-08 14:09:00 -06:00
|
|
|
# Chunks stay upright (rotation locked), so turn their pixels to the nearest
|
|
|
|
|
# quarter turn of the sprite instead.
|
|
|
|
|
var quarterTurns := posmod(roundi(sprite.global_rotation / (PI / 2)), 4)
|
|
|
|
|
|
|
|
|
|
var material := PhysicsMaterial.new()
|
|
|
|
|
material.friction = CHUNK_FRICTION
|
|
|
|
|
material.bounce = CHUNK_BOUNCE
|
|
|
|
|
var shape := RectangleShape2D.new()
|
|
|
|
|
shape.size = Vector2.ONE * (CHUNK_SIZE - SHAPE_SHRINK)
|
|
|
|
|
|
|
|
|
|
for chunkY in range(0, size.y, CHUNK_SIZE):
|
|
|
|
|
for chunkX in range(0, size.x, CHUNK_SIZE):
|
|
|
|
|
var chunkImage := _make_chunk_image(image, Vector2i(chunkX, chunkY))
|
|
|
|
|
if chunkImage == null:
|
2026-11-08 13:41:01 -06:00
|
|
|
continue
|
2026-11-08 14:09:00 -06:00
|
|
|
if sprite.flip_h:
|
|
|
|
|
chunkImage.flip_x()
|
|
|
|
|
if sprite.flip_v:
|
|
|
|
|
chunkImage.flip_y()
|
|
|
|
|
for turn in quarterTurns:
|
|
|
|
|
chunkImage.rotate_90(CLOCKWISE)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
# Chunk centre in the sprite's local space (it's centered).
|
|
|
|
|
var localCenter := Vector2(chunkX, chunkY) + Vector2.ONE * CHUNK_SIZE / 2.0 - Vector2(size) / 2.0
|
2026-11-08 13:41:01 -06:00
|
|
|
if sprite.flip_h:
|
2026-11-08 14:09:00 -06:00
|
|
|
localCenter.x = -localCenter.x
|
2026-11-08 13:41:01 -06:00
|
|
|
if sprite.flip_v:
|
2026-11-08 14:09:00 -06:00
|
|
|
localCenter.y = -localCenter.y
|
|
|
|
|
var worldPoint := spriteTransform * (localCenter + sprite.offset)
|
|
|
|
|
worldPoint = _push_out_of_solid(worldPoint, -push, shape, exclude)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
|
|
|
|
var side := signf((worldPoint - center).dot(sideways))
|
|
|
|
|
if side == 0.0:
|
|
|
|
|
side = [-1.0, 1.0].pick_random()
|
|
|
|
|
var velocity := sideways * side * randf_range(SIDEWAYS_SPEED_MIN, SIDEWAYS_SPEED_MAX)
|
|
|
|
|
velocity -= push * randf_range(BACK_SPEED_MIN, BACK_SPEED_MAX)
|
|
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
_spawn_chunk(chunkImage, worldPoint, velocity, shape, material, exclude)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
|
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
func _spawn_chunk(chunkImage: Image, at: Vector2, velocity: Vector2, shape: Shape2D,
|
|
|
|
|
material: PhysicsMaterial, exclude: Array[RID]) -> void:
|
|
|
|
|
var body := RigidBody2D.new()
|
|
|
|
|
body.collision_layer = CHUNK_LAYER
|
|
|
|
|
body.collision_mask = CHUNK_MASK
|
|
|
|
|
body.lock_rotation = true
|
|
|
|
|
body.mass = CHUNK_MASS
|
|
|
|
|
body.physics_material_override = material
|
|
|
|
|
body.continuous_cd = RigidBody2D.CCD_MODE_CAST_RAY # Small and fast: don't skip through things.
|
|
|
|
|
body.position = at
|
|
|
|
|
body.linear_velocity = velocity
|
2026-11-08 13:41:01 -06:00
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
var collision := CollisionShape2D.new()
|
|
|
|
|
collision.shape = shape
|
|
|
|
|
body.add_child(collision)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
var chunkSprite := Sprite2D.new()
|
|
|
|
|
chunkSprite.texture = ImageTexture.create_from_image(chunkImage)
|
|
|
|
|
body.add_child(chunkSprite)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
add_child(body)
|
|
|
|
|
chunks.append(body)
|
2026-11-08 13:41:01 -06:00
|
|
|
|
2026-11-08 14:09:00 -06:00
|
|
|
if exclude.is_empty():
|
|
|
|
|
return
|
|
|
|
|
for rid in exclude:
|
|
|
|
|
PhysicsServer2D.body_add_collision_exception(body.get_rid(), rid)
|
|
|
|
|
get_tree().create_timer(EXCLUDE_TIME, false, true).timeout.connect(func() -> void:
|
|
|
|
|
if is_instance_valid(body):
|
|
|
|
|
for rid in exclude:
|
|
|
|
|
PhysicsServer2D.body_remove_collision_exception(body.get_rid(), rid)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The chunk's pixels with black ones made transparent, or null if nothing's left.
|
|
|
|
|
func _make_chunk_image(image: Image, topLeft: Vector2i) -> Image:
|
|
|
|
|
var region := Rect2i(topLeft, Vector2i.ONE * CHUNK_SIZE).intersection(Rect2i(Vector2i.ZERO, image.get_size()))
|
|
|
|
|
var chunkImage := image.get_region(region)
|
|
|
|
|
chunkImage.convert(Image.FORMAT_RGBA8)
|
|
|
|
|
var hasColor := false
|
|
|
|
|
for y in chunkImage.get_height():
|
|
|
|
|
for x in chunkImage.get_width():
|
|
|
|
|
var color := chunkImage.get_pixel(x, y)
|
|
|
|
|
if color.a < 0.5 or _is_black(color):
|
|
|
|
|
chunkImage.set_pixel(x, y, Color.TRANSPARENT)
|
|
|
|
|
else:
|
|
|
|
|
hasColor = true
|
|
|
|
|
if not hasColor:
|
|
|
|
|
return null
|
|
|
|
|
return chunkImage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _is_black(color: Color) -> bool:
|
|
|
|
|
return color.r < BLACK_THRESHOLD and color.g < BLACK_THRESHOLD and color.b < BLACK_THRESHOLD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Moves a spot where the chunk would start inside the level out along `direction`, a pixel at a time.
|
|
|
|
|
func _push_out_of_solid(point: Vector2, direction: Vector2, shape: Shape2D, exclude: Array[RID]) -> Vector2:
|
|
|
|
|
var query := PhysicsShapeQueryParameters2D.new()
|
|
|
|
|
query.shape = shape
|
|
|
|
|
query.collision_mask = 1 # Just the level and hazards, not other chunks.
|
|
|
|
|
query.exclude = exclude
|
2026-11-08 13:41:01 -06:00
|
|
|
for step in MAX_PUSH_OUT:
|
2026-11-08 14:09:00 -06:00
|
|
|
query.transform = Transform2D(0.0, point)
|
|
|
|
|
if get_world_2d().direct_space_state.intersect_shape(query, 1).is_empty():
|
2026-11-08 13:41:01 -06:00
|
|
|
break
|
|
|
|
|
point += direction
|
|
|
|
|
return point
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _get_image(texture: Texture2D) -> Image:
|
|
|
|
|
var image: Image
|
|
|
|
|
if texture is AtlasTexture:
|
|
|
|
|
var atlas := texture as AtlasTexture
|
|
|
|
|
image = atlas.atlas.get_image().get_region(Rect2i(atlas.region))
|
|
|
|
|
else:
|
|
|
|
|
image = texture.get_image()
|
|
|
|
|
if image != null and image.is_compressed():
|
|
|
|
|
image.decompress()
|
|
|
|
|
return image
|