From 41a72709df3cdeb279399a9a4e260e35c6ff3b5c Mon Sep 17 00:00:00 2001 From: Kenshia <73539778+Kenshia@users.noreply.github.com> Date: Sun, 26 Jan 2025 11:14:32 +0700 Subject: [PATCH] feat: card hover --- player_side.gd | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/player_side.gd b/player_side.gd index aec31f2..1075011 100644 --- a/player_side.gd +++ b/player_side.gd @@ -23,6 +23,13 @@ var show_buttons: bool var signal_connected: bool var selected_card: Control +var hovering_cards_this_frame: Array[Control] = [] +var hovering_cards_duration: Dictionary = {} +const original_hover_position = 50 # hard coded idk what's a good way to find this value +const hover_animation_time = 0.1 +const hover_height_offset = -50 +const selected_height_offset = -100 + func attach(match_manager: MatchManager, show_buttons: bool = true): self.show_buttons = show_buttons self.match_manager = match_manager @@ -39,6 +46,7 @@ func _ready(): child.queue_free() func _on_update(transition): + hovering_cards_duration.clear() selected_card = null for child in deck.get_children(): @@ -87,6 +95,7 @@ func _on_update(transition): skip_btn.left_clicked.connect(func (): _pop_this_card(skip_btn) play_card.emit(null)) + skip_btn.hovering.connect(func (delta): _do_hover(skip_btn, delta)) deck.add_child(skip_btn) for card: Card in player.hand: @@ -118,6 +127,7 @@ func _on_update(transition): monster_card.left_clicked.connect(func (): _pop_this_card(monster_card) play_card.emit(card)) + monster_card.hovering.connect(func (delta): _do_hover(monster_card, delta)) deck.add_child(monster_card) elif card is SupportCard and card.type == "red": @@ -133,6 +143,7 @@ func _on_update(transition): btn.left_clicked.connect(func (): _pop_this_card(btn) play_card.emit(card)) + btn.hovering.connect(func (delta): _do_hover(btn, delta)) if rps_button_container: rps_button_container.visible = match_manager.phase == Match.Phase.RPS @@ -142,10 +153,48 @@ func _on_update(transition): func _pop_this_card(control: Control): if selected_card: - selected_card.size_flags_vertical = Control.SIZE_SHRINK_END + selected_card.position = Vector2(selected_card.position.x, original_hover_position) - control.size_flags_vertical = Control.SIZE_SHRINK_BEGIN selected_card = control + selected_card.position = Vector2(selected_card.position.x, original_hover_position + selected_height_offset) + +func _do_hover(control: Control, delta: float): + if control == selected_card: + if control in hovering_cards_duration: + hovering_cards_duration.erase(control) + return + + var duration: float + if control in hovering_cards_duration: + duration = hovering_cards_duration[control] + duration = clampf(duration + delta, 0, hover_animation_time) + hovering_cards_duration[control] = duration + else: + hovering_cards_duration[control] = delta + duration = delta + + var height = lerpf(0, hover_height_offset, duration / hover_animation_time) + original_hover_position + control.position = Vector2(control.position.x, height) + + hovering_cards_this_frame.append(control) + +func _process(delta): + for card in hovering_cards_duration.keys(): + if !card or card in hovering_cards_this_frame: + continue + + var duration = hovering_cards_duration[card] + duration = maxf(0, duration - delta) + + var height = lerpf(0, hover_height_offset, duration / hover_animation_time) + original_hover_position + card.position = Vector2(card.position.x, height) + + if duration == 0: + hovering_cards_duration.erase(card) + else: + hovering_cards_duration[card] = duration + + hovering_cards_this_frame.clear() func _on_kertas_button_button_down() -> void: