This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
magician/card_stack.gd

59 lines
1.6 KiB
GDScript

class_name CardStack
extends Node3D
var _card_height: float = 0.02
@export var playing_card_scene: PackedScene
## Surface for allowing peeking at the played cards by hovering the mouse.
@export var peek_surface: Area3D
var _is_expanded: bool = false
func _ready() -> void:
if peek_surface:
peek_surface.mouse_entered.connect(_peek_mouse_entered)
peek_surface.mouse_exited.connect(_peek_mouse_exited)
func add_card(card: PlayingCard) -> void:
var height = self.get_child_count() * _card_height
self.add_child(card)
card.rotate(Vector3.FORWARD, randf_range(-0.3, 0.3))
card.translate(Vector3.BACK * height)
func add_card_by_id(card_id: int) -> void:
var playing_card: PlayingCard = playing_card_scene.instantiate()
playing_card.setup_card_details(card_id)
add_card(playing_card)
func purge_cards() -> void:
while get_child_count() > 0:
remove_child(get_children()[get_child_count() - 1])
func _peek_mouse_entered() -> void:
if get_child_count() > 1:
var index = 0
var child_count = get_child_count()
for child: Node3D in get_children():
create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT).tween_property(
child,
"position",
Vector3(0.2, index * -0.42 - (((child_count - 1) * -0.42) / 2.0), _card_height * index),
0.3,
)
index += 1
_is_expanded = true
func _peek_mouse_exited() -> void:
if _is_expanded:
var index = 0
for child: Node3D in get_children():
create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT).tween_property(
child,
"position",
Vector3(0.0, 0.0, _card_height * index),
0.3,
)
index += 1
_is_expanded = false