Refactor functions to board

This commit is contained in:
Hamcha 2019-05-20 23:52:38 +02:00
parent c2ace7f531
commit d307f49194
Signed by: hamcha
GPG key ID: A40413D21021EAEE
4 changed files with 58 additions and 43 deletions

View file

@ -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":

View file

@ -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():

View file

@ -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:

View file

@ -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)))