96 lines
2.2 KiB
GDScript
96 lines
2.2 KiB
GDScript
tool
|
|
extends Node2D
|
|
|
|
signal played(card, anchor, unit)
|
|
|
|
const AnimHelper = preload("res://helpers/anims.gd")
|
|
const BattleHelper = preload("res://helpers/battle.gd")
|
|
|
|
export var title = "Card" setget set_title
|
|
export(Texture) var portrait setget set_portrait
|
|
export(PackedScene) var spawned_unit setget set_spawned_unit
|
|
|
|
var dragged = false
|
|
var base_position
|
|
var selected_anchor
|
|
var hero
|
|
|
|
func _ready():
|
|
set_portrait(portrait)
|
|
set_title(title)
|
|
set_spawned_unit(spawned_unit)
|
|
base_position = position
|
|
|
|
func get_size() -> Vector2:
|
|
return $background.texture.get_size()
|
|
|
|
func set_title(val):
|
|
title = val
|
|
if has_node("title"):
|
|
$title.text = title
|
|
|
|
func set_portrait(val):
|
|
portrait = val
|
|
if has_node("portrait"):
|
|
$portrait.texture = portrait
|
|
|
|
func set_spawned_unit(val):
|
|
spawned_unit = val
|
|
if has_node("points"):
|
|
if spawned_unit:
|
|
var unit = spawned_unit.instance()
|
|
$points.move = unit.base_move_points
|
|
$points.hull = unit.base_hull_points
|
|
$points.shield = unit.base_shield_points
|
|
$points.damage = unit.base_damage_points
|
|
$points.visible = true
|
|
else:
|
|
$points.visible = false
|
|
|
|
func set_hero(val: Node):
|
|
hero = val
|
|
|
|
func can_be_used_on_anchor(anchor):
|
|
if anchor is Anchor:
|
|
if spawned_unit:
|
|
return anchor.is_empty() and hero and anchor.is_connected_to(hero.get_parent())
|
|
else:
|
|
return true
|
|
else:
|
|
return false
|
|
|
|
func set_hand_location(loc: Vector2):
|
|
position = loc
|
|
base_position = loc
|
|
|
|
func update_anchors():
|
|
var position = global_position + (get_size() / 2) if dragged else null
|
|
selected_anchor = BattleHelper.update_anchors(get_tree(), position, funcref(self, "can_be_used_on_anchor"))
|
|
|
|
func play(anchor):
|
|
var unit
|
|
|
|
if spawned_unit:
|
|
unit = BattleHelper.spawn_unit(spawned_unit, anchor, hero.attacker if hero else false)
|
|
|
|
emit_signal("played", self, anchor, unit)
|
|
|
|
func return_to_base():
|
|
AnimHelper.linear_goto(self, base_position, 0.3)
|
|
|
|
func on_dragged(active, relative, absolute):
|
|
if dragged != active:
|
|
dragged = active
|
|
modulate = Color(1, 1, 1, 0.5) if dragged else Color(1, 1, 1, 1)
|
|
|
|
if not dragged:
|
|
if selected_anchor:
|
|
play(selected_anchor)
|
|
else:
|
|
return_to_base()
|
|
|
|
if relative:
|
|
set_position(position + relative)
|
|
|
|
update_anchors()
|