1
0
Fork 0
spacetac/screens/battle/battle.gd

46 lines
1.2 KiB
GDScript

extends Panel
const BattleHelper = preload("res://helpers/battle.gd")
export(PackedScene) var deck_attack
export(PackedScene) var deck_defend
onready var deck_attack_cards = deck_attack.instance()
onready var deck_defend_cards = deck_defend.instance()
func _ready():
create_hero_units()
fill_hand($hand_attack, deck_attack_cards)
fill_hand($hand_defend, deck_defend_cards)
func find_free_anchor(anchor_type: String):
for anchor in get_tree().get_nodes_in_group("anchors"):
if anchor.anchor_type == anchor_type and anchor.is_empty():
return anchor
return null
func create_hero_unit(name: String, anchor_type: String):
var anchor = find_free_anchor(anchor_type)
if anchor:
BattleHelper.spawn_unit("heroes/" + name, anchor)
func create_hero_units():
create_hero_unit("tomahawk", "attack_start")
create_hero_unit("rhino", "defend_start")
func fill_hand(hand: Node, deck: Node, limit=4):
while hand.get_child_count() < 4:
var count = deck.get_child_count()
var index = floor(rand_range(0, count))
var card = deck.get_child(index).duplicate()
hand.add_child(card)
reorganize_hand(hand)
func reorganize_hand(hand: Node):
var i = 0
for card in hand.get_children():
card.rect_position = Vector2(i * 210, 0)
i += 1