The piston winds up, slams down, holds and retracts on a timer. Catching the slime under the head breaks its current frame into one particle per non-black pixel, which bounce off and settle on the level. The level reloads afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
4.4 KiB
GDScript
139 lines
4.4 KiB
GDScript
class_name PixelBurst
|
|
extends Node2D
|
|
|
|
# Breaks a sprite's current frame into one particle per pixel. Black (outline)
|
|
# and transparent pixels are skipped. Particles fall, bounce off the level and
|
|
# come to rest on it.
|
|
|
|
const GRAVITY = 700.0
|
|
const MAX_FALL_SPEED = 400.0
|
|
const BOUNCE = 0.35 # Speed kept (and reversed) when hitting something.
|
|
const FLOOR_FRICTION = 0.6 # Sideways speed kept when bouncing off a floor.
|
|
const SETTLE_SPEED = 15.0 # Slower than this on a floor and the pixel stops for good.
|
|
const MAX_PUSH_OUT = 16 # Pixels spawned inside the level move out by up to this.
|
|
const BLACK_THRESHOLD = 0.1 # Pixels with all channels below this count as black.
|
|
|
|
# How hard pixels squirt out sideways from under whatever pushed them, and back against it.
|
|
const SIDEWAYS_SPEED_MIN = 40.0
|
|
const SIDEWAYS_SPEED_MAX = 220.0
|
|
const BACK_SPEED_MIN = 20.0
|
|
const BACK_SPEED_MAX = 180.0
|
|
|
|
var positions := PackedVector2Array()
|
|
var velocities := PackedVector2Array()
|
|
var colors := PackedColorArray()
|
|
var settled := PackedByteArray()
|
|
var excluded: Array[RID] = []
|
|
|
|
|
|
# push: the direction the sprite was hit towards (e.g. DOWN for a piston from above).
|
|
# exclude: bodies the pixels shouldn't collide with (e.g. the piston head).
|
|
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
|
|
excluded = exclude
|
|
|
|
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()
|
|
|
|
for y in size.y:
|
|
for x in size.x:
|
|
var color := image.get_pixel(x, y)
|
|
if color.a < 0.5:
|
|
continue
|
|
if color.r < BLACK_THRESHOLD and color.g < BLACK_THRESHOLD and color.b < BLACK_THRESHOLD:
|
|
continue
|
|
|
|
# Pixel centre in the sprite's local space (it's centered).
|
|
var localX := x - size.x / 2.0 + 0.5
|
|
var localY := y - size.y / 2.0 + 0.5
|
|
if sprite.flip_h:
|
|
localX = -localX
|
|
if sprite.flip_v:
|
|
localY = -localY
|
|
var worldPoint := spriteTransform * (Vector2(localX, localY) + sprite.offset)
|
|
|
|
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)
|
|
|
|
positions.append(_push_out_of_solid(worldPoint, -push))
|
|
velocities.append(velocity)
|
|
colors.append(color)
|
|
settled.append(0)
|
|
|
|
queue_redraw()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
for i in positions.size():
|
|
if settled[i]:
|
|
continue
|
|
|
|
var oldPosition := positions[i]
|
|
var velocity := velocities[i]
|
|
velocity.y = minf(velocity.y + GRAVITY * delta, MAX_FALL_SPEED)
|
|
var newPosition := oldPosition + velocity * delta
|
|
|
|
# Pixels that start inside something (squashed into it) move freely until they're out.
|
|
if not _is_solid(oldPosition) and _is_solid(newPosition):
|
|
if _is_solid(Vector2(newPosition.x, oldPosition.y)):
|
|
newPosition.x = oldPosition.x
|
|
velocity.x *= -BOUNCE
|
|
if _is_solid(Vector2(newPosition.x, newPosition.y)):
|
|
var hitFloor := velocity.y > 0.0
|
|
newPosition.y = oldPosition.y
|
|
velocity.y *= -BOUNCE
|
|
if hitFloor:
|
|
velocity.x *= FLOOR_FRICTION
|
|
if velocity.length() < SETTLE_SPEED:
|
|
velocity = Vector2.ZERO
|
|
settled[i] = 1
|
|
|
|
positions[i] = newPosition
|
|
velocities[i] = velocity
|
|
|
|
queue_redraw()
|
|
|
|
|
|
func _draw() -> void:
|
|
for i in positions.size():
|
|
draw_rect(Rect2(positions[i].floor(), Vector2.ONE), colors[i])
|
|
|
|
|
|
# Moves a point that starts inside the level out along `direction`, a pixel at a time.
|
|
func _push_out_of_solid(point: Vector2, direction: Vector2) -> Vector2:
|
|
for step in MAX_PUSH_OUT:
|
|
if not _is_solid(point):
|
|
break
|
|
point += direction
|
|
return point
|
|
|
|
|
|
func _is_solid(point: Vector2) -> bool:
|
|
var query := PhysicsPointQueryParameters2D.new()
|
|
query.position = point
|
|
query.exclude = excluded
|
|
return not get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
|
|
|
|
|
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
|