From d307f4919447e556d087a3d784d4041922372eaf Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 20 May 2019 23:52:38 +0200 Subject: [PATCH] Refactor functions to board --- Scenes/Components/Card.gd | 46 ++++++------------------------------ Scenes/Components/Stack.gd | 3 +++ Scenes/Scripts/Board.gd | 48 ++++++++++++++++++++++++++++++++++++-- Scenes/Scripts/BoardUI.gd | 4 ++-- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/Scenes/Components/Card.gd b/Scenes/Components/Card.gd index a3d20f2..24d19dc 100644 --- a/Scenes/Components/Card.gd +++ b/Scenes/Components/Card.gd @@ -8,6 +8,8 @@ signal card_picked() signal card_dropped() signal card_dropped_anim() signal card_menu() +signal card_moved(origin, direction, t) +signal card_clicked() var clicked := false var exhausted := false @@ -21,6 +23,7 @@ var zoneName = "" var cardMat = preload("res://MLPAssets/Placeholders/CardMat.tres") var ready = false +onready var border := $CardModel/Border onready var animation := $CardModel/Border/AnimationPlayer onready var handanim := $CardModel/Border/HandAnimPlayer # Hack to get hand animations off the main queue onready var cardImage := $CardModel/Border/CardImage @@ -52,35 +55,15 @@ func _input_event(camera, event, click_position, click_normal, shape_idx): lifted = true var origin: Vector3 = camera.project_ray_origin(event.position) var direction: Vector3 = camera.project_ray_normal(event.position) - if flipped: - $CardModel/Border.translation = Vector3(0, 0.01, 0) - $CardModel/Border.rotation = Vector3(0, 0, PI) - else: - $CardModel/Border.translation = Vector3.ZERO - $CardModel/Border.rotation = Vector3.ZERO - if inHand: - translation = Vector3(direction.x * 3.0, 0.25, -0.25) - # Fix rotation if coming from hand - rotation = Vector3.ZERO - elif inZone: - translation = Vector3.ZERO - rotation = Vector3.ZERO - else: - var denom := Vector3.UP.dot(direction) - var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom; - translation = origin + direction * t - # Rotate depending on what side of the board it is - if translation.z < 0: - rotation.y = PI - else: - rotation.y = 0 + var denom := Vector3.UP.dot(direction) + var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom; + emit_signal("card_moved", origin, direction, t) # Right click if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_RIGHT: # Show menu emit_signal("card_menu") # Left click if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and clickable(): - # Update click status clicked = event.pressed @@ -92,22 +75,7 @@ func _input_event(camera, event, click_position, click_normal, shape_idx): # Check double click if event.doubleclick and not inHand and not lifted and not animation.is_playing(): - if exhausted: - animation.queue("tap-back") - exhausted = false - else: - animation.queue("tap") - exhausted = true - -func _menu_action(id: int): - match id: - 0: # Flip - if flipped: - animation.queue("flip-back") - flipped = false - else: - animation.queue("flip") - flipped = true + emit_signal("card_clicked") func _check_drop_anim(anim_name): if anim_name == "drop": diff --git a/Scenes/Components/Stack.gd b/Scenes/Components/Stack.gd index f3c7977..b915b05 100644 --- a/Scenes/Components/Stack.gd +++ b/Scenes/Components/Stack.gd @@ -49,6 +49,9 @@ func pop_card() -> Card: card.flipped = false return card +func stack_size(): + return cards.size() + const CARD_DISTANCE = 0.005 func reorder_cards(): diff --git a/Scenes/Scripts/Board.gd b/Scenes/Scripts/Board.gd index 493bfc1..c2cbfae 100644 --- a/Scenes/Scripts/Board.gd +++ b/Scenes/Scripts/Board.gd @@ -142,7 +142,49 @@ func _card_selected(card: Card): func _card_unselected(card: Card): if focusedCard == card: focusedCard = null - card.handanim.play("blur") + if card.inHand: + card.handanim.play("blur") + +func _card_moved(origin: Vector3, direction: Vector3, t: float, card: Card): + if card.flipped: + card.border.translation = Vector3(0, 0.01, 0) + card.border.rotation = Vector3(0, 0, PI) + else: + card.border.translation = Vector3.ZERO + card.border.rotation = Vector3.ZERO + if card.inHand: + card.translation = Vector3(direction.x * 3.0, 0.25, -0.25) + # Fix rotation if coming from hand + card.rotation = Vector3.ZERO + elif card.inZone: + card.translation = Vector3.ZERO + card.rotation = Vector3.ZERO + else: + card.translation = origin + direction * t + # Rotate depending on what side of the board it is + if card.translation.z < 0: + card.rotation.y = PI + else: + card.rotation.y = 0 + +func _card_clicked(card: Card): + if card.exhausted: + card.animation.queue("tap-back") + card.exhausted = false + else: + card.animation.queue("tap") + card.exhausted = true + + +func _card_menu_action(id: int, card: Card): + match id: + 0: # Flip + if card.flipped: + card.animation.queue("flip-back") + card.flipped = false + else: + card.animation.queue("flip") + card.flipped = true func reparent(object: Node, from: Node, to: Node): from.remove_child(object) @@ -162,7 +204,9 @@ func add_card(cardID: String, playerID: int, zone: String): card.connect("card_selected", self, "_card_selected", [card]) card.connect("card_unselected", self, "_card_unselected", [card]) card.connect("card_dropped_anim", self, "check_hand_drop", [card]) - card.connect("card_menu", ui, "show_card_menu", [card]) + card.connect("card_moved", self, "_card_moved", [card]) + card.connect("card_menu", ui, "show_card_menu", [card, self, "_card_menu_action"]) + card.connect("card_clicked", self, "_card_clicked", [card]) if card.inHand: # TODO support for >2 players if playerID == 0: diff --git a/Scenes/Scripts/BoardUI.gd b/Scenes/Scripts/BoardUI.gd index 06ceae4..15b42e6 100644 --- a/Scenes/Scripts/BoardUI.gd +++ b/Scenes/Scripts/BoardUI.gd @@ -13,7 +13,7 @@ func _card_selected(card: Card): func _card_unselected(card: Card): animation.play_backwards("fade ui card") -func show_card_menu(card): +func show_card_menu(card: Card, cbobj: Object, cbfn): # Close any open menus if menu != null: menu.queue_free() @@ -32,6 +32,6 @@ func show_card_menu(card): menu.add_item("Set Cutie-Marked", 2) menu.add_item("Add color", 3) menu.add_item("Add marker", 4) - menu.connect("id_pressed", card, "_menu_action") + menu.connect("id_pressed", cbobj, cbfn, [card]) add_child(menu) menu.popup(Rect2(get_viewport().get_mouse_position(), Vector2(150,10))) \ No newline at end of file