2019-05-04 19:33:47 +00:00
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
onready var camera := $Camera
|
2019-05-05 19:19:39 +00:00
|
|
|
onready var hand := $Camera/Hand
|
|
|
|
onready var cards := $Cards
|
|
|
|
onready var handCard := $Camera/Hand/Card4
|
|
|
|
|
|
|
|
export var mouseHandThreshold = 0.9
|
|
|
|
|
|
|
|
var holdingCard: bool
|
2019-05-04 19:33:47 +00:00
|
|
|
|
|
|
|
var mouseOrigin: Vector2
|
|
|
|
var lastCameraTransform: Transform
|
|
|
|
|
|
|
|
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-04 19:33:47 +00:00
|
|
|
var mousePos: Vector2 = mouseDelta * 0.0096 * (1-camera.getZoomLevel()/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:
|
|
|
|
var relPos: float = absMousePos.y / get_viewport().size.y
|
|
|
|
var selectingHand: bool = relPos > mouseHandThreshold
|
|
|
|
if selectingHand and not handCard.inHand:
|
|
|
|
call_deferred("reparent", handCard, cards, hand)
|
|
|
|
handCard.inHand = true
|
|
|
|
elif not selectingHand and handCard.inHand:
|
|
|
|
call_deferred("reparent", handCard, hand, cards)
|
|
|
|
handCard.inHand = false
|
|
|
|
|
|
|
|
func _card_picked():
|
|
|
|
holdingCard = true
|
|
|
|
|
|
|
|
func _card_dropped():
|
|
|
|
holdingCard = false
|
|
|
|
|
|
|
|
func reparent(object: Node, from: Node, to: Node):
|
|
|
|
from.remove_child(object)
|
|
|
|
to.add_child(object)
|
|
|
|
object.set_owner(to)
|