55 lines
1.4 KiB
GDScript
55 lines
1.4 KiB
GDScript
extends Unit
|
|
|
|
var current_command := WeakRef.new()
|
|
|
|
func _compose():
|
|
$chief.visible = is_chief
|
|
super._compose()
|
|
|
|
func tick():
|
|
chief_maintenance()
|
|
|
|
if is_chief:
|
|
var command := current_command.get_ref() as Command
|
|
if command and command.global_position.distance_to(global_position) > 100.0:
|
|
# go near current command
|
|
move_to(Tools.jitter(command.global_position, 50.0, 100.0))
|
|
return
|
|
else:
|
|
var chief = get_chief()
|
|
if chief and chief.global_position.distance_to(global_position) > 100.0:
|
|
# go near chief
|
|
move_to(Tools.jitter(chief.global_position, 50.0, 100.0))
|
|
return
|
|
|
|
var enemy := Tools.get_nearest(self, get_tree().get_nodes_in_group("unit"),
|
|
func filter(unit): return unit is Unit and unit.player != player)
|
|
if enemy:
|
|
# go near current target enemy
|
|
move_to(Tools.jitter(enemy.global_position, 100.0, 300.0))
|
|
$turret.set_target(enemy)
|
|
|
|
func chief_maintenance():
|
|
if is_chief:
|
|
# already chief
|
|
return
|
|
|
|
var chief = get_chief()
|
|
if chief == null:
|
|
# find a chief
|
|
var chiefs = list_chiefs()
|
|
# TODO sort by distance, experience and subordinate count
|
|
if chiefs.size() > 0:
|
|
set_chief(chiefs[0])
|
|
else:
|
|
promote_chief()
|
|
_compose()
|
|
|
|
func _on_commands_changed(commands):
|
|
if is_chief:
|
|
var defend := Tools.get_nearest(self, commands,
|
|
func filter(command): return command.player == player and command.code == "defend")
|
|
|
|
if defend:
|
|
current_command = weakref(defend)
|