1
0
Fork 0
spacetac/helpers/battle.gd

78 lines
1.8 KiB
GDScript

static func update_anchors(tree: SceneTree, position, acceptability) -> Anchor:
""" Update anchor visual hint, and return selected one
"""
var anchors = tree.get_nodes_in_group("anchors")
for anchor in anchors:
anchor.selectable = position and acceptability.call_func(anchor)
var selected_anchor = null
var selected_distance = INF
for anchor in anchors:
if anchor.selectable:
var distance = anchor.global_position.distance_to(position)
if distance < 150 and distance < selected_distance:
selected_anchor = anchor
selected_distance = distance
for anchor in anchors:
anchor.selected = (anchor == selected_anchor)
return selected_anchor
static func spawn_unit(name: String, anchor: Anchor, attacker: bool) -> Node:
""" Spawn a unit on an empty anchor
"""
var scene = load("res://units/" + name + ".tscn")
var node = scene.instance()
anchor.set_content(node)
node.attacker = attacker
return node
static func list_units(tree: SceneTree):
""" List all units in battle
"""
var units = []
for anchor in tree.get_nodes_in_group("anchors"):
var content = anchor.get_content()
if content:
units.append(content)
return units
static func apply_combat(attacker: UnitPoints, defender: UnitPoints):
""" Apply a unit combat to points
"""
while attacker.damage:
attacker.damage -= 1
if defender.shield:
defender.shield -= 1
elif defender.hull:
defender.hull -= 1
while defender.damage:
defender.damage -= 1
if attacker.shield:
attacker.shield -= 1
elif attacker.hull:
attacker.hull -= 1
static func get_state(tree: SceneTree):
""" Build the simple state associated with current battle scene
"""
var nodes = []
var routes = []
for anchor in tree.get_nodes_in_group("anchors"):
pass
for route in tree.get_nodes_in_group("routes"):
pass
return {
"nodes": nodes,
"routes": routes
}