147 lines
3.4 KiB
GDScript
147 lines
3.4 KiB
GDScript
tool
|
|
extends Panel
|
|
|
|
class_name BaseUnit
|
|
|
|
const AnimHelper = preload("res://helpers/anims.gd")
|
|
const BattleHelper = preload("res://helpers/battle.gd")
|
|
|
|
export(Texture) var sprite setget set_sprite
|
|
export var base_move_points = 0 setget set_base_move_points
|
|
export var base_hull_points = 0 setget set_base_hull_points
|
|
export var base_shield_points = 0 setget set_base_shield_points
|
|
export var base_damage_points = 0 setget set_base_damage_points
|
|
|
|
var selected_anchor
|
|
var dragged
|
|
|
|
func _ready():
|
|
$move_hint.visible = false
|
|
|
|
set_sprite(sprite)
|
|
|
|
set_base_damage_points(base_damage_points)
|
|
set_base_move_points(base_move_points)
|
|
set_base_hull_points(base_hull_points)
|
|
set_base_shield_points(base_shield_points)
|
|
|
|
func destroy():
|
|
if get_parent() is Anchor:
|
|
(get_parent() as Anchor).set_content(null)
|
|
|
|
queue_free()
|
|
|
|
func set_sprite(val):
|
|
sprite = val
|
|
if has_node("sprite"):
|
|
$sprite.texture = sprite
|
|
|
|
func set_base_move_points(val):
|
|
base_move_points = val
|
|
if has_node("points"):
|
|
$points.move = val
|
|
|
|
func set_base_hull_points(val):
|
|
base_hull_points = val
|
|
if has_node("points"):
|
|
$points.hull = val
|
|
|
|
func set_base_shield_points(val):
|
|
base_shield_points = val
|
|
if has_node("points"):
|
|
$points.shield = val
|
|
|
|
func set_base_damage_points(val):
|
|
base_damage_points = val
|
|
if has_node("points"):
|
|
$points.damage = val
|
|
|
|
func can_target(other):
|
|
if other == self:
|
|
return false
|
|
else:
|
|
return true
|
|
|
|
func can_be_used_on_anchor(anchor):
|
|
return anchor is Anchor and anchor.is_connected_to(get_parent()) and (anchor.is_empty() or can_target(anchor.get_content()))
|
|
|
|
func update_anchors():
|
|
var cursor = rect_global_position + dragged if dragged else null
|
|
selected_anchor = BattleHelper.update_anchors(get_tree(), cursor, funcref(self, "can_be_used_on_anchor"))
|
|
|
|
func play(anchor):
|
|
if anchor.is_empty():
|
|
move_to(anchor)
|
|
else:
|
|
var other = anchor.get_content()
|
|
if other.has_method("get_points"):
|
|
var won = attack(other)
|
|
if won:
|
|
move_to(anchor)
|
|
else:
|
|
return_to_base()
|
|
else:
|
|
return_to_base()
|
|
|
|
func move_to(anchor):
|
|
anchor.set_content(self)
|
|
|
|
func get_points() -> UnitPoints:
|
|
if $points is UnitPoints:
|
|
return $points as UnitPoints
|
|
else:
|
|
return null
|
|
|
|
func attack(other) -> bool:
|
|
var selfpoints = get_points()
|
|
var otherpoints = other.get_points()
|
|
|
|
while selfpoints.damage:
|
|
selfpoints.damage -= 1
|
|
|
|
if otherpoints.shield:
|
|
otherpoints.shield -= 1
|
|
elif otherpoints.hull:
|
|
otherpoints.hull -= 1
|
|
|
|
while otherpoints.damage:
|
|
otherpoints.damage -= 1
|
|
|
|
if selfpoints.shield:
|
|
selfpoints.shield -= 1
|
|
elif selfpoints.hull:
|
|
selfpoints.hull -= 1
|
|
|
|
if selfpoints.hull <= 0:
|
|
destroy()
|
|
if otherpoints.hull <= 0:
|
|
other.destroy()
|
|
|
|
return selfpoints.hull > 0 && otherpoints.hull == 0
|
|
|
|
func return_to_base():
|
|
dragged = null
|
|
$move_hint.visible = false
|
|
$move_hint.set_point_position(1, Vector2(0, 0))
|
|
|
|
func _gui_input(event):
|
|
if event is InputEventMouseButton and event.button_index == 1:
|
|
if event.pressed:
|
|
dragged = event.position
|
|
$move_hint.visible = true
|
|
elif dragged:
|
|
dragged = null
|
|
$move_hint.visible = false
|
|
if selected_anchor:
|
|
play(selected_anchor)
|
|
else:
|
|
return_to_base()
|
|
|
|
if event is InputEventMouseMotion and event.button_mask == BUTTON_LEFT:
|
|
dragged = event.position
|
|
|
|
update_anchors()
|
|
|
|
if dragged:
|
|
$move_hint.set_point_position(1, selected_anchor.global_position - rect_global_position if selected_anchor else dragged)
|