extends Panel const BattleHelper = preload("res://helpers/battle.gd") export(PackedScene) var deck_attack export(PackedScene) var deck_defend export(PackedScene) var hero_unit_attack export(PackedScene) var hero_unit_defend export(Script) var defend_ai export var interactive = false setget set_interactive 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(hero_unit_attack, "attack_start", true) hero_defend = create_hero_unit(hero_unit_defend, "defend_start", false) $hand_attack.connect("turn_end", self, "end_turn") $hand_defend.connect("turn_end", self, "end_turn") $combat_info.visible = false fill_hands() adjust_playable() set_interactive(false) #print(BattleHelper.get_state(get_tree())) show_turn_indicator() func set_interactive(val: bool): interactive = val func show_turn_indicator(): $turn_indicator.start("Player Turn" if turn_attack else "AI Turn") 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(unit: PackedScene, anchor_type: String, attacker: bool): var anchor = find_free_anchor(anchor_type) if anchor: var hero = BattleHelper.spawn_unit(unit, anchor, attacker) _on_unit_created(hero) return hero 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 fill_hands(): randomize() fill_hand($hand_attack, deck_attack_cards, hero_attack) fill_hand($hand_defend, deck_defend_cards, hero_defend) func end_turn(): set_interactive(false) turn_attack = not turn_attack fill_hands() adjust_playable() for unit in BattleHelper.list_units(get_tree()): unit.turn_ended() show_turn_indicator() func _process(delta): if not turn_attack and defend_ai and interactive: defend_ai.play($arena, hero_defend, $hand_defend) func adjust_playable(): $hand_attack.visible = turn_attack for unit in BattleHelper.list_units(get_tree()): unit.playable = unit.attacker == turn_attack func _on_unit_created(unit): unit.connect("combat_simulation", self, "_on_combat_simulation") func _on_combat_simulation(attacker, defender): if attacker and defender: $combat_info/attacker_points.set_from(attacker.get_points()) $combat_info/defender_points.set_from(defender.get_points()) BattleHelper.apply_combat($combat_info/attacker_points, $combat_info/defender_points) $combat_info.visible = true $hand_attack.visible = false else: $combat_info.visible = false $hand_attack.visible = turn_attack