mlpcardgame/Scenes/Scripts/Board.gd

78 lines
2.4 KiB
GDScript3
Raw Normal View History

2019-05-04 19:33:47 +00:00
extends Spatial
2019-05-05 23:35:58 +00:00
class_name Board
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
onready var ui := $BoardUI
2019-05-05 19:19:39 +00:00
onready var cards := $Cards
export var mouseHandThreshold = 0.9
var holdingCard: Card
2019-05-04 19:33:47 +00:00
var mouseOrigin: Vector2
var lastCameraTransform: Transform
func _ready():
add_card("ff36", 0, true)
add_card("ff36", 0, false)
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
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
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
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
func _card_picked(card: Card):
holdingCard = card
2019-05-05 19:19:39 +00:00
func _card_dropped(card: Card):
holdingCard = null
2019-05-05 19:19:39 +00:00
func reparent(object: Node, from: Node, to: Node):
from.remove_child(object)
to.add_child(object)
object.set_owner(to)
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-05 23:35:58 +00:00
card.connect("card_menu", ui, "show_card_menu", [card])
if inHand:
# TODO if player != me, put in opponent's hand
hand.add_child(card)
else:
cards.add_child(card)