2019-05-04 19:33:47 +00:00
|
|
|
extends Spatial
|
|
|
|
|
2019-05-05 23:35:58 +00:00
|
|
|
class_name Board
|
|
|
|
|
2019-05-05 22:39:55 +00:00
|
|
|
onready var CardTemplate := preload("res://Scenes/Components/Card.tscn")
|
|
|
|
|
2019-05-04 19:33:47 +00:00
|
|
|
onready var camera := $Camera
|
2019-05-05 19:19:39 +00:00
|
|
|
onready var hand := $Camera/Hand
|
2019-05-05 22:39:55 +00:00
|
|
|
onready var ui := $BoardUI
|
2019-05-05 19:19:39 +00:00
|
|
|
onready var cards := $Cards
|
|
|
|
|
2019-05-06 21:18:04 +00:00
|
|
|
export var mouseHandThreshold = 0.85
|
2019-05-05 19:19:39 +00:00
|
|
|
|
2019-05-06 11:12:57 +00:00
|
|
|
var holdingCard: Card = null
|
|
|
|
var focusedCard: Card = null
|
2019-05-04 19:33:47 +00:00
|
|
|
|
|
|
|
var mouseOrigin: Vector2
|
|
|
|
var lastCameraTransform: Transform
|
|
|
|
|
2019-05-05 22:39:55 +00:00
|
|
|
func _ready():
|
2019-05-06 11:12:57 +00:00
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, true)
|
2019-05-05 22:39:55 +00:00
|
|
|
add_card("ff36", 0, true)
|
|
|
|
add_card("ff36", 0, false)
|
2019-05-06 11:12:57 +00:00
|
|
|
reorder_hand()
|
2019-05-05 22:39:55 +00:00
|
|
|
|
2019-05-04 19:33:47 +00:00
|
|
|
func _input(event: InputEvent):
|
|
|
|
# Camera zoom
|
|
|
|
if event.is_action("zoom_in"):
|
|
|
|
camera.zoom(true)
|
|
|
|
elif event.is_action("zoom_out"):
|
|
|
|
camera.zoom(false)
|
|
|
|
# Save original camera and mouse position before panning
|
|
|
|
if event.is_action_pressed("pan"):
|
|
|
|
mouseOrigin = get_viewport().get_mouse_position()
|
|
|
|
lastCameraTransform = camera.transform
|
|
|
|
|
2019-05-04 21:42:07 +00:00
|
|
|
func _process(delta: float):
|
2019-05-05 19:19:39 +00:00
|
|
|
# If mouse is under a certain area then we're managing our hand
|
|
|
|
var absMousePos := get_viewport().get_mouse_position()
|
2019-05-04 19:33:47 +00:00
|
|
|
# If panning, translate mouse delta to camera delta
|
|
|
|
if Input.is_action_pressed("pan"):
|
2019-05-05 19:19:39 +00:00
|
|
|
var mouseDelta := absMousePos - mouseOrigin
|
2019-05-05 22:39:55 +00:00
|
|
|
var mousePos: Vector2 = mouseDelta * 0.0096 * (1-camera.get_zoom()/5) # Magic numbers everywhere
|
2019-05-05 19:19:39 +00:00
|
|
|
camera.transform.origin = lastCameraTransform.origin - Vector3(mousePos.x, 0, mousePos.y)
|
|
|
|
# If holding a card, move it between board/hand
|
2019-05-05 22:39:55 +00:00
|
|
|
if holdingCard != null:
|
2019-05-05 19:19:39 +00:00
|
|
|
var relPos: float = absMousePos.y / get_viewport().size.y
|
|
|
|
var selectingHand: bool = relPos > mouseHandThreshold
|
2019-05-05 22:39:55 +00:00
|
|
|
if selectingHand and not holdingCard.inHand:
|
|
|
|
holdingCard.inHand = true
|
|
|
|
call_deferred("reparent", holdingCard, cards, hand)
|
|
|
|
elif not selectingHand and holdingCard.inHand:
|
|
|
|
holdingCard.inHand = false
|
|
|
|
call_deferred("reparent", holdingCard, hand, cards)
|
2019-05-05 19:19:39 +00:00
|
|
|
|
2019-05-05 22:39:55 +00:00
|
|
|
func _card_picked(card: Card):
|
|
|
|
holdingCard = card
|
2019-05-06 21:18:04 +00:00
|
|
|
holdingCard.animation.play("lift")
|
2019-05-05 19:19:39 +00:00
|
|
|
|
2019-05-05 22:39:55 +00:00
|
|
|
func _card_dropped(card: Card):
|
2019-05-06 21:18:04 +00:00
|
|
|
if card.inHand:
|
|
|
|
card.animation.play("drop")
|
|
|
|
else:
|
|
|
|
card.animation.play_backwards("lift")
|
2019-05-05 22:39:55 +00:00
|
|
|
holdingCard = null
|
2019-05-06 11:12:57 +00:00
|
|
|
|
|
|
|
func _card_selected(card: Card):
|
|
|
|
if card.inHand and focusedCard == null:
|
|
|
|
focusedCard = card
|
|
|
|
card.animation.play("focus")
|
|
|
|
|
|
|
|
func _card_unselected(card: Card):
|
|
|
|
if focusedCard == card:
|
|
|
|
focusedCard = null
|
|
|
|
card.animation.play("blur")
|
2019-05-05 19:19:39 +00:00
|
|
|
|
|
|
|
func reparent(object: Node, from: Node, to: Node):
|
|
|
|
from.remove_child(object)
|
|
|
|
to.add_child(object)
|
2019-05-05 22:39:55 +00:00
|
|
|
object.set_owner(to)
|
2019-05-06 11:12:57 +00:00
|
|
|
reorder_hand()
|
2019-05-05 22:39:55 +00:00
|
|
|
|
|
|
|
func add_card(cardID: String, playerID: int, inHand: bool):
|
|
|
|
var card := CardTemplate.instance()
|
|
|
|
card.cardID = cardID
|
|
|
|
card.playerID = playerID
|
|
|
|
card.inHand = inHand
|
|
|
|
card.connect("card_dropped", self, "_card_dropped", [card])
|
|
|
|
card.connect("card_picked", self, "_card_picked", [card])
|
|
|
|
card.connect("card_selected", ui, "_card_selected", [card])
|
|
|
|
card.connect("card_unselected", ui, "_card_unselected", [card])
|
2019-05-06 11:12:57 +00:00
|
|
|
card.connect("card_selected", self, "_card_selected", [card])
|
|
|
|
card.connect("card_unselected", self, "_card_unselected", [card])
|
2019-05-06 21:18:04 +00:00
|
|
|
card.connect("card_dropped_anim", self, "check_hand_drop", [card])
|
2019-05-05 23:35:58 +00:00
|
|
|
card.connect("card_menu", ui, "show_card_menu", [card])
|
2019-05-05 22:39:55 +00:00
|
|
|
if inHand:
|
|
|
|
# TODO if player != me, put in opponent's hand
|
|
|
|
hand.add_child(card)
|
|
|
|
else:
|
2019-05-06 11:12:57 +00:00
|
|
|
cards.add_child(card)
|
|
|
|
|
2019-05-06 21:18:04 +00:00
|
|
|
func check_hand_drop(card: Card):
|
|
|
|
# Re-order hand as soon as the animation is over
|
|
|
|
if card.inHand:
|
|
|
|
card.reset_transform()
|
|
|
|
reorder_hand()
|
|
|
|
|
2019-05-06 11:12:57 +00:00
|
|
|
const MAX_CARD_DISTANCE := 0.5
|
2019-05-06 21:18:04 +00:00
|
|
|
const HAND_SCREEN_PERC := 0.6
|
2019-05-06 11:12:57 +00:00
|
|
|
const CARD_ROTATION := 0.03
|
|
|
|
const UNITSPERPX := 0.003
|
|
|
|
|
|
|
|
func reorder_hand():
|
|
|
|
var cardsInHand: Array = hand.get_children()
|
|
|
|
cardsInHand.sort_custom(TransformSorter, "sort")
|
|
|
|
var size := cardsInHand.size()
|
|
|
|
|
2019-05-06 22:18:10 +00:00
|
|
|
# Check if we have nothing to order
|
|
|
|
if size < 1:
|
|
|
|
return
|
|
|
|
|
2019-05-06 11:12:57 +00:00
|
|
|
# Calculate total width of the player's hand and other things
|
|
|
|
# This is done in two ways, for small hands, MAX_CARD_DISTANCE is usually used
|
|
|
|
# as constant distance between cards, however, as the hand gets larger we don't
|
|
|
|
# want them to go offscreen, therefore a "maximum screen %" is used to determine
|
|
|
|
# how to fit all the cards within a % of the viewport's width
|
|
|
|
var distancePerc := get_viewport().size.x * HAND_SCREEN_PERC * UNITSPERPX / size
|
2019-05-06 21:18:04 +00:00
|
|
|
var distance := min(distancePerc, MAX_CARD_DISTANCE)
|
2019-05-06 11:12:57 +00:00
|
|
|
var totalWidth := distance * (size-1)
|
|
|
|
var minX := -totalWidth/2
|
2019-05-06 22:18:10 +00:00
|
|
|
|
2019-05-06 11:12:57 +00:00
|
|
|
# Iterate over all items, keep track of the index
|
|
|
|
var i := 0
|
|
|
|
for child in cardsInHand:
|
2019-05-06 21:18:04 +00:00
|
|
|
child.tween_move_to(Vector3(minX + distance * i, 0.02*i, -0.005*i))
|
|
|
|
child.tween_rotate(Vector3(0, CARD_ROTATION * (size/2.0 - i), 0))
|
2019-05-06 11:12:57 +00:00
|
|
|
i += 1
|
|
|
|
child.reset_transform()
|
2019-05-06 21:18:04 +00:00
|
|
|
child.tween()
|
2019-05-06 11:12:57 +00:00
|
|
|
|
|
|
|
class TransformSorter:
|
|
|
|
static func sort(a, b):
|
|
|
|
if a.transform.origin.x < b.transform.origin.x:
|
|
|
|
return true
|
|
|
|
return false
|