diff --git a/Effects/pixel_burst.gd b/Effects/pixel_burst.gd index caa67c4..975f656 100644 --- a/Effects/pixel_burst.gd +++ b/Effects/pixel_burst.gd @@ -1,38 +1,42 @@ 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. +# 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 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 CHUNK_SIZE = 2 # Sprite pixels per side of a chunk. 1 = a body per pixel. 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. +# 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. 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] = [] +var chunks: Array[RigidBody2D] = [] # 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). +# exclude: bodies the chunks pass through for EXCLUDE_TIME (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) @@ -43,23 +47,36 @@ func burst_from_sprite(sprite: AnimatedSprite2D, push: Vector2, exclude: Array[R var spriteTransform := sprite.get_global_transform() var center := sprite.global_position var sideways := push.orthogonal() + # 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) - 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 + var material := PhysicsMaterial.new() + material.friction = CHUNK_FRICTION + material.bounce = CHUNK_BOUNCE + var shape := RectangleShape2D.new() + shape.size = Vector2.ONE * (CHUNK_SIZE - SHAPE_SHRINK) - # 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 + 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: + continue if sprite.flip_h: - localX = -localX + chunkImage.flip_x() if sprite.flip_v: - localY = -localY - var worldPoint := spriteTransform * (Vector2(localX, localY) + sprite.offset) + chunkImage.flip_y() + for turn in quarterTurns: + chunkImage.rotate_90(CLOCKWISE) + + # 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 + if sprite.flip_h: + localCenter.x = -localCenter.x + if sprite.flip_v: + localCenter.y = -localCenter.y + var worldPoint := spriteTransform * (localCenter + sprite.offset) + worldPoint = _push_out_of_solid(worldPoint, -push, shape, exclude) var side := signf((worldPoint - center).dot(sideways)) if side == 0.0: @@ -67,66 +84,79 @@ func burst_from_sprite(sprite: AnimatedSprite2D, push: Vector2, exclude: Array[R 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() + _spawn_chunk(chunkImage, worldPoint, velocity, shape, material, exclude) -func _physics_process(delta: float) -> void: - for i in positions.size(): - if settled[i]: - continue +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 - var oldPosition := positions[i] - var velocity := velocities[i] - velocity.y = minf(velocity.y + GRAVITY * delta, MAX_FALL_SPEED) - var newPosition := oldPosition + velocity * delta + var collision := CollisionShape2D.new() + collision.shape = shape + body.add_child(collision) - # 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 + var chunkSprite := Sprite2D.new() + chunkSprite.texture = ImageTexture.create_from_image(chunkImage) + body.add_child(chunkSprite) - positions[i] = newPosition - velocities[i] = velocity + add_child(body) + chunks.append(body) - queue_redraw() + 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) + ) -func _draw() -> void: - for i in positions.size(): - draw_rect(Rect2(positions[i].floor(), Vector2.ONE), colors[i]) +# 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 -# 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: +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 for step in MAX_PUSH_OUT: - if not _is_solid(point): + query.transform = Transform2D(0.0, point) + if get_world_2d().direct_space_state.intersect_shape(query, 1).is_empty(): 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: