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

104 lines
2.9 KiB
GDScript3
Raw Normal View History

2020-02-03 21:59:43 +00:00
extends Panel
const BattleHelper = preload("res://helpers/battle.gd")
2020-02-03 22:03:42 +00:00
export(PackedScene) var deck_attack
export(PackedScene) var deck_defend
2020-02-18 22:32:33 +00:00
export(PackedScene) var hero_unit_attack
export(PackedScene) var hero_unit_defend
2020-02-18 22:24:00 +00:00
export(Script) var defend_ai
2020-02-23 22:59:01 +00:00
export var interactive = false setget set_interactive
2020-02-03 22:03:42 +00:00
onready var deck_attack_cards = deck_attack.instance()
onready var deck_defend_cards = deck_defend.instance()
2020-02-03 23:22:23 +00:00
var hero_attack
var hero_defend
var turn_attack = true
2020-02-03 22:03:42 +00:00
2020-02-03 21:59:43 +00:00
func _ready():
2020-02-18 22:32:33 +00:00
hero_attack = create_hero_unit(hero_unit_attack, "attack_start", true)
hero_defend = create_hero_unit(hero_unit_defend, "defend_start", false)
2020-02-18 22:24:00 +00:00
$hand_attack.connect("turn_end", self, "end_turn")
$hand_defend.connect("turn_end", self, "end_turn")
2020-02-18 22:24:00 +00:00
$combat_info.visible = false
fill_hands()
adjust_playable()
2020-02-23 22:59:01 +00:00
set_interactive(false)
2020-02-18 22:24:00 +00:00
#print(BattleHelper.get_state(get_tree()))
2020-02-18 22:24:00 +00:00
2020-02-23 22:59:01 +00:00
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")
2020-02-03 21:59:43 +00:00
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
2020-02-18 22:24:00 +00:00
2020-02-18 22:32:33 +00:00
func create_hero_unit(unit: PackedScene, anchor_type: String, attacker: bool):
2020-02-03 21:59:43 +00:00
var anchor = find_free_anchor(anchor_type)
if anchor:
2020-02-18 22:32:33 +00:00
var hero = BattleHelper.spawn_unit(unit, anchor, attacker)
_on_unit_created(hero)
return hero
2020-02-03 23:22:23 +00:00
else:
return null
2020-02-18 22:24:00 +00:00
2020-02-03 23:22:23 +00:00
func fill_hand(hand: Node, deck: Node, hero: Node, limit=4):
while hand.get_card_count() < 4:
2020-02-03 22:03:42 +00:00
var count = deck.get_child_count()
var index = floor(rand_range(0, count))
var card = deck.get_child(index).duplicate()
2020-02-03 23:22:23 +00:00
card.set_hero(hero)
hand.add_card(card)
2020-02-03 22:03:42 +00:00
hand.rearrange()
2020-02-18 22:24:00 +00:00
func fill_hands():
2020-02-23 22:59:01 +00:00
randomize()
2020-02-18 22:24:00 +00:00
fill_hand($hand_attack, deck_attack_cards, hero_attack)
fill_hand($hand_defend, deck_defend_cards, hero_defend)
func end_turn():
2020-02-23 22:59:01 +00:00
set_interactive(false)
turn_attack = not turn_attack
2020-02-18 22:24:00 +00:00
fill_hands()
adjust_playable()
for unit in BattleHelper.list_units(get_tree()):
unit.turn_ended()
2020-02-23 22:59:01 +00:00
show_turn_indicator()
2020-02-18 22:24:00 +00:00
func _process(delta):
2020-02-23 22:59:01 +00:00
if not turn_attack and defend_ai and interactive:
2020-02-18 22:24:00 +00:00
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
2020-02-18 22:24:00 +00:00
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