Files
mortal-maze-the-game/pickups/pickup.gd
T

111 lines
3.3 KiB
GDScript

extends Area2D
class_name Pickup
signal picked_up(pickup: Pickup, data: PickupData)
# Enemies leave their reward on screen briefly. A player who touches it during
# that interval must still receive it, even if they keep running.
const DROP_COLLECTION_DELAY_SECONDS: float = 0.75
@export var pickup_data: PickupData
@export var collection_delay_seconds: float = 0.0
@export var fall_speed: float = 150.0
@export var bob_amplitude: float = 4.0
@export var bob_speed: float = 3.0
var velocity: Vector2 = Vector2.ZERO
var time: float = 0.0
var start_y: float = 0.0
var grounded: bool = false
var _can_collect: bool = false
var _pending_collector: Node2D # Player who crossed the pickup during its delay.
@onready var label: Label = $Label
@onready var sprite: Sprite2D = $Sprite2D
@onready var placeholder: ColorRect = $Sprite2D/ColorRect
func _ready() -> void:
add_to_group("pickup")
body_entered.connect(_on_body_entered)
if pickup_data == null:
push_error("Pickup without PickupData: %s" % get_path())
label.text = "SIN DATOS"
placeholder.color = Color.MAGENTA
return
label.text = pickup_data.display_name
sprite.texture = pickup_data.icon
placeholder.visible = pickup_data.icon == null
placeholder.color = pickup_data.color
start_y = position.y
if collection_delay_seconds > 0.0:
_enable_collection_after_delay()
else:
_can_collect = true
func _enable_collection_after_delay() -> void:
await get_tree().create_timer(collection_delay_seconds).timeout
if not is_inside_tree():
return
_can_collect = true
# A body_entered signal received during the delay is not lost. Crossing a
# spawning item counts as touching it, even if the player is now elsewhere.
if is_instance_valid(_pending_collector):
_on_body_entered(_pending_collector)
_pending_collector = null
func _physics_process(delta: float) -> void:
time += delta
if not grounded:
velocity.y += fall_speed * delta
position += velocity * delta
if is_on_floor():
grounded = true
velocity = Vector2.ZERO
start_y = position.y
else:
position.y = start_y + sin(time * bob_speed) * bob_amplitude
# body_entered only fires at the start of a contact. Rechecking overlaps
# also handles moving pickups and effects that were unusable on first touch
# (e.g. a heal while HP was full, then the player takes damage).
if pickup_data != null and not is_queued_for_deletion():
for body in get_overlapping_bodies():
_on_body_entered(body)
if is_queued_for_deletion():
break
func is_on_floor() -> bool:
var space = get_world_2d().direct_space_state
var params = PhysicsRayQueryParameters2D.create(global_position, global_position + Vector2(0, 12))
params.collision_mask = 1
var result = space.intersect_ray(params)
return result.size() > 0
func _on_body_entered(body: Node2D) -> void:
if pickup_data == null or is_queued_for_deletion():
return
if not body.is_in_group("player") or not body.has_method("collect_pickup"):
return
if not _can_collect:
_pending_collector = body
return
if body.collect_pickup(pickup_data):
picked_up.emit(self, pickup_data)
queue_free()
elif pickup_data.effect_type == "heal":
# This is not a collision failure: the player already has full HP.
label.text = "%s\n(HP LLENO)" % pickup_data.display_name
elif pickup_data.effect_type == "special_charge":
label.text = "%s\n(ESPECIAL LLENO)" % pickup_data.display_name