158 lines
5.5 KiB
GDScript
158 lines
5.5 KiB
GDScript
class_name NotificationDisplay extends Control
|
|
|
|
@export var icon: TextureRect
|
|
@export var label: Label
|
|
@onready var _container: HBoxContainer = $HBoxContainer
|
|
|
|
var _player: Node2D = null
|
|
var _sprite_size: Vector2 = Vector2.ZERO
|
|
var _origin_offset: float = 0.0
|
|
var _follow_player: bool = false
|
|
|
|
var _connector_line: Line2D = null
|
|
var _connector_visible: bool = false
|
|
|
|
|
|
func show_notification(weapon_name: String, player_node: Node2D, player_sprite_size: Vector2, origin_offset: float) -> void:
|
|
label.add_theme_font_size_override("font_size", 8)
|
|
label.text = weapon_name
|
|
|
|
# Auto-load icon from graphics folder matching the weapon name
|
|
var icon_path = "res://graphics/" + weapon_name.to_lower() + ".png"
|
|
if ResourceLoader.exists(icon_path):
|
|
icon.texture = load(icon_path)
|
|
else:
|
|
icon.texture = null
|
|
|
|
_player = player_node
|
|
_player.tree_exiting.connect(queue_free)
|
|
_sprite_size = player_sprite_size
|
|
_origin_offset = origin_offset
|
|
_follow_player = false # Disable movement for testing
|
|
|
|
# Wait one frame for layout to resolve shrink-to-fit widths
|
|
await get_tree().process_frame
|
|
|
|
# Use container's natural width, but fix height at 16px
|
|
size = Vector2(_container.size.x, 16.0)
|
|
|
|
# Calculate target position
|
|
_update_position()
|
|
var target_pos = position
|
|
|
|
# Start 10px above destination, fully transparent
|
|
position.y = target_pos.y - 10.0
|
|
modulate.a = 0.0
|
|
|
|
# Pause following during fade-in so the tween isn't overridden by _process
|
|
_follow_player = false
|
|
|
|
# Fade in and slide into position with overshoot
|
|
var tween = create_tween().set_parallel()
|
|
tween.tween_property(self, "modulate:a", 1.0, 0.3)
|
|
var move_tween = tween.tween_property(self, "position:y", target_pos.y, 0.3)
|
|
move_tween.set_trans(Tween.TRANS_ELASTIC).set_ease(Tween.EASE_OUT)
|
|
|
|
# Wait for fade-in to finish, then resume following
|
|
await tween.finished
|
|
_follow_player = true
|
|
|
|
# Create connector line from notification to player origin
|
|
_connector_line = Line2D.new()
|
|
_connector_line.default_color = Color.WHITE
|
|
_connector_line.width = 1.0
|
|
add_child(_connector_line)
|
|
_connector_visible = true
|
|
|
|
# Stay fully visible for a moment before fading
|
|
await get_tree().create_timer(0.5).timeout
|
|
|
|
# Record current position as the fade-out starting point
|
|
await get_tree().process_frame
|
|
var fade_start_y = position.y
|
|
|
|
# Fade out and drift downward with steady acceleration
|
|
_follow_player = false
|
|
var fade_out = create_tween().set_parallel()
|
|
fade_out.tween_property(self, "modulate:a", 0.0, .35)
|
|
var drift = fade_out.tween_property(self, "position:y", fade_start_y + 7, .3)
|
|
drift.set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_IN)
|
|
await fade_out.finished
|
|
queue_free()
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
if _follow_player and _player:
|
|
_update_position()
|
|
if _connector_visible and _player:
|
|
_update_connector()
|
|
|
|
|
|
func _update_position() -> void:
|
|
if _player == null:
|
|
return
|
|
|
|
var player_pos = _player.position
|
|
|
|
# Y position uses the weapon origin offset from player center
|
|
var notif_y = player_pos.y + _origin_offset - (size.y / 2.0)
|
|
|
|
# X position is 10px to the right of player's sprite edge
|
|
var right_edge = player_pos.x + (_sprite_size.x / 2.0)
|
|
var notif_x = right_edge + 10.0
|
|
|
|
var is_flipped = false
|
|
|
|
# Clamp to screen bounds — flip to left if it would go off-screen right
|
|
var viewport_size = get_viewport_rect().size
|
|
if notif_x + size.x > viewport_size.x:
|
|
is_flipped = true
|
|
var left_edge = player_pos.x - (_sprite_size.x / 2.0)
|
|
notif_x = left_edge - 10.0 - size.x
|
|
|
|
# Swap icon/label order so the icon is always closest to the player
|
|
if is_flipped:
|
|
_container.move_child(label, 0)
|
|
_container.move_child(icon, 1)
|
|
else:
|
|
_container.move_child(icon, 0)
|
|
_container.move_child(label, 1)
|
|
|
|
# Final clamp: ensure the entire notification is within screen bounds
|
|
notif_x = clamp(notif_x, 0, viewport_size.x - size.x)
|
|
notif_y = clamp(notif_y, 0, viewport_size.y - size.y)
|
|
|
|
position = Vector2(notif_x, notif_y)
|
|
|
|
|
|
func _update_connector() -> void:
|
|
if _connector_line == null or _player == null:
|
|
return
|
|
|
|
# Icon position in the notification's local space
|
|
var icon_pos = _container.position + icon.position
|
|
var icon_mid_y = icon_pos.y + icon.size.y / 2.0
|
|
|
|
# Point 0: edge of the container closest to the icon, at icon's vertical midpoint
|
|
var point0: Vector2
|
|
var direction: float
|
|
|
|
if _container.get_child(0) == icon:
|
|
# Icon on the left (not flipped) — container's left edge, line goes left toward player
|
|
point0 = Vector2(_container.position.x, icon_mid_y)
|
|
direction = -1.0
|
|
else:
|
|
# Icon on the right (flipped) — container's right edge, line goes right toward player
|
|
point0 = Vector2(_container.position.x + _container.size.x, icon_mid_y)
|
|
direction = 1.0
|
|
|
|
# Point 1: 8px straight from point 0 toward the player
|
|
const SEGMENT_ORIGIN: float = 8.0 # length of initial segment from origin
|
|
var point1 = Vector2(point0.x + SEGMENT_ORIGIN * direction, icon_mid_y)
|
|
|
|
# Point 2: player's origin + ENDPOINT_OFFSET, converted to notification's local space
|
|
const ENDPOINT_OFFSET = Vector2(4, 7) # offset from player's origin to connector endpoint
|
|
var target = _player.position + Vector2(+ENDPOINT_OFFSET.x, _origin_offset + ENDPOINT_OFFSET.y)
|
|
var point2 = target - position
|
|
|
|
_connector_line.points = PackedVector2Array([point0, point1, point2])
|