1
0
Fork 0
spacetac/core/unit.gd

92 lines
1.9 KiB
GDScript3
Raw Normal View History

2022-10-06 13:41:45 +00:00
@tool
class_name Unit
extends Composer
2022-10-26 18:29:17 +00:00
signal promoted
2022-10-06 13:41:45 +00:00
@export var sprite: Texture2D:
set(val):
sprite = val
_check_compose()
2022-10-05 23:21:40 +00:00
@export var player: Node:
set(val):
if val is Player:
player = val
else:
player = null
2022-10-19 21:23:47 +00:00
_check_compose()
2022-10-06 22:34:33 +00:00
@export var hitpoints := 1
2022-10-19 21:23:47 +00:00
var chief_ref := WeakRef.new()
2022-10-05 23:21:40 +00:00
2022-10-19 21:23:47 +00:00
var target_position := Vector2.ZERO
2022-10-31 10:59:41 +00:00
var target_linear_velocity := Vector2.ZERO
var target_angular_velocity := 0.0
var target_reached := false
2022-10-19 21:23:47 +00:00
var is_chief := false
var subordinates := 0
func _ready():
target_position = global_position
super._ready()
2022-10-06 13:41:45 +00:00
func _compose():
$sprite.texture = sprite
2022-10-05 23:21:40 +00:00
if player:
# TODO something more subtle
2022-10-19 21:23:47 +00:00
$sprite.self_modulate = player.color
2022-10-05 23:21:40 +00:00
if player.code and not Engine.is_editor_hint():
add_to_group("player:" + player.code)
2022-10-31 10:59:41 +00:00
func _process(delta):
if Engine.is_editor_hint():
return
if target_reached:
target_linear_velocity = Vector2.ZERO
else:
var current_position = self.global_position
var diff = target_position - current_position
if diff.length() > 5.0:
target_linear_velocity = diff.normalized() * clamp(diff.length() * 0.1, 1.0, 10.0) * 10.0
else:
2022-10-05 23:21:40 +00:00
target_reached = true
2022-10-31 10:59:41 +00:00
self.linear_velocity = Tools.accel(self.linear_velocity, target_linear_velocity, 0.4, delta)
2022-10-05 23:21:40 +00:00
2022-10-06 22:34:33 +00:00
func move_to(pos: Vector2):
2022-10-05 23:21:40 +00:00
if pos != target_position:
target_position = pos
target_reached = false
2022-10-06 22:34:33 +00:00
func damage(lost_hitpoints: float):
hitpoints = max(0.0, hitpoints - lost_hitpoints)
if hitpoints <= 0:
die()
2022-10-19 21:23:47 +00:00
func get_chief():
return chief_ref.get_ref()
2022-10-06 22:34:33 +00:00
func die():
2022-10-19 21:23:47 +00:00
var chief = get_chief()
if chief:
chief.subordinates -= 1
2022-10-06 22:34:33 +00:00
queue_free()
2022-10-19 21:23:47 +00:00
func promote_chief():
2022-10-26 18:29:17 +00:00
if not is_chief:
is_chief = true
if player:
add_to_group("chief:" + player.code)
promoted.emit()
2022-10-19 21:23:47 +00:00
func set_chief(chief):
if chief is Unit and chief.is_chief and chief != self:
chief_ref = weakref(chief)
chief.subordinates += 1
func list_chiefs():
return get_tree().get_nodes_in_group("chief:" + player.code)