- Carry item (Items/): picked up on touch, rides see-through in the slime's center, left behind at the mouth when the slime enters a tube. Has gravity. Placeholder Godot icon sprite. One placed in the test level. - Wall-press sprite (new frame 9, PressWall animation): shown when pushing into a wall from the floor or ceiling, into a tube mouth in a wall, or from a wall into a floor/ceiling mouth. Flipped/turned and shifted to sit flush. - Ceiling corners: pushing sideways into a wall keeps the slime on the ceiling; holding down moves it onto the wall. - Tubes: squeezing closes the gap to the mouth; mouths are found a frame ahead so climbing past one can't skip it; backing out of a wall mouth entered from the floor or ceiling puts the slime on the wall. - Tube goo draws behind the tiles (shows only through the channel); the manual tube's leader circle stays in front. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.8 KiB
GDScript
106 lines
3.8 KiB
GDScript
class_name CarryItem
|
|
extends CharacterBody2D
|
|
|
|
# Something the slime can carry inside its body. Touch it to pick it up; it rides
|
|
# in the slime's center until the slime goes into a tube, which drops it.
|
|
# On its own it just falls and slides to a stop.
|
|
#
|
|
# It's on no collision layer, so it never blocks the slime or its raycasts, but
|
|
# it collides with the world (mask) like the slime does.
|
|
#
|
|
# It's always drawn in front of the slime, and of its goo in a tube. While
|
|
# carried it's see-through, so it looks like it's inside the slime. Left behind,
|
|
# it fades back to solid. (Behind the goo, bits of it would flicker through the
|
|
# moving goo pixels as the slime poured into the tube.)
|
|
|
|
const MAX_FALL_SPEED = 400.0
|
|
const GROUND_FRICTION = 600.0
|
|
# After being dropped, how long before it can be picked up again. Stops a slime
|
|
# backing straight out of a tube's mouth from grabbing it on the same frame.
|
|
const PICKUP_COOLDOWN = 0.3
|
|
# How big the touch zone is: a little bigger than the item, so brushing it counts.
|
|
const PICKUP_SIZE = Vector2(14, 14)
|
|
const DRAW_ORDER = 2 # z_index. The slime's sprite is 0 and tube goo is 1.
|
|
|
|
@export_range(0.0, 1.0, 0.05) var carriedOpacity := 0.5
|
|
@export_range(0.0, 1.0, 0.05) var fadeTime := 0.15 # Seconds to fade between solid and see-through.
|
|
|
|
var carrier: Node2D = null # The slime carrying us, or null.
|
|
var pickupCooldown := 0.0
|
|
var pickupShape := RectangleShape2D.new()
|
|
var fadeTween: Tween
|
|
|
|
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
|
@onready var sprite: Sprite2D = $Sprite2D
|
|
|
|
|
|
func _ready() -> void:
|
|
# Run after the slime has moved, so we sit in its center on the same frame
|
|
# rather than one frame behind.
|
|
process_physics_priority = 10
|
|
pickupShape.size = PICKUP_SIZE
|
|
z_index = DRAW_ORDER
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if carrier != null:
|
|
global_position = carrier.global_position
|
|
return
|
|
|
|
pickupCooldown = maxf(pickupCooldown - delta, 0.0)
|
|
if pickupCooldown == 0.0 and _try_pick_up():
|
|
return
|
|
|
|
velocity.y = minf(velocity.y + get_gravity().y * delta, MAX_FALL_SPEED)
|
|
if is_on_floor():
|
|
velocity.x = move_toward(velocity.x, 0.0, GROUND_FRICTION * delta)
|
|
move_and_slide()
|
|
|
|
|
|
# Asks the physics world what's touching us. The slime double-checks it's really
|
|
# in reach (see pick_up_item), as physics can be a frame behind after a tube.
|
|
func _try_pick_up() -> bool:
|
|
var query := PhysicsShapeQueryParameters2D.new()
|
|
query.shape = pickupShape
|
|
query.transform = Transform2D(0.0, global_position)
|
|
query.collision_mask = collision_mask
|
|
query.exclude = [get_rid()]
|
|
for hit in get_world_2d().direct_space_state.intersect_shape(query):
|
|
var body: Object = hit.collider
|
|
if body != null and body.has_method("pick_up_item") and body.pick_up_item(self):
|
|
return true
|
|
return false
|
|
|
|
|
|
# Called by the slime once it has decided to take us.
|
|
func attach_to(newCarrier: Node2D) -> void:
|
|
carrier = newCarrier
|
|
# Never bump into the slime's body. It shares the world's layer, and coming out
|
|
# of a tube, physics has its shape back where it went in for a frame: right on
|
|
# top of us if we were left there, which shoved us out of it.
|
|
add_collision_exception_with(carrier)
|
|
velocity = Vector2.ZERO
|
|
collisionShape.set_deferred("disabled", true)
|
|
_fade_to(carriedOpacity)
|
|
global_position = carrier.global_position
|
|
reset_physics_interpolation() # Snap in, don't slide across.
|
|
|
|
|
|
# Left behind where the slime is now, falling from there.
|
|
func drop(dropVelocity := Vector2.ZERO) -> void:
|
|
if carrier == null:
|
|
return
|
|
global_position = carrier.global_position
|
|
carrier = null
|
|
velocity = dropVelocity
|
|
pickupCooldown = PICKUP_COOLDOWN
|
|
collisionShape.set_deferred("disabled", false)
|
|
_fade_to(1.0)
|
|
reset_physics_interpolation()
|
|
|
|
|
|
func _fade_to(opacity: float) -> void:
|
|
if fadeTween:
|
|
fadeTween.kill()
|
|
fadeTween = create_tween()
|
|
fadeTween.tween_property(sprite, "modulate:a", opacity, fadeTime)
|