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() var hero_attack var hero_defend var turn_attack = true func _ready(): hero_attack = create_hero_unit("tomahawk", "attack_start", true) hero_defend = create_hero_unit("rhino", "defend_start", false) fill_hand($hand_attack, deck_attack_cards, hero_attack) fill_hand($hand_defend, deck_defend_cards, hero_defend) $hand_attack.connect("turn_end", self, "end_turn") $hand_defend.connect("turn_end", self, "end_turn") adjust_playable() #print(BattleHelper.get_state(get_tree())) 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, attacker: bool): var anchor = find_free_anchor(anchor_type) if anchor: return BattleHelper.spawn_unit("heroes/" + name, anchor, attacker) else: return null func fill_hand(hand: Node, deck: Node, hero: Node, limit=4): while hand.get_card_count() < 4: var count = deck.get_child_count() var index = floor(rand_range(0, count)) var card = deck.get_child(index).duplicate() card.set_hero(hero) hand.add_card(card) hand.rearrange() func end_turn(): turn_attack = not turn_attack adjust_playable() func adjust_playable(): $hand_attack.visible = turn_attack for unit in BattleHelper.list_units(get_tree()): unit.playable = unit.attacker == turn_attack