1
0
Fork 0
spacetac/units/base_unit.gd

147 lines
3.4 KiB
GDScript3
Raw Normal View History

2020-02-03 21:59:43 +00:00
tool
extends Panel
2020-02-09 23:28:05 +00:00
class_name BaseUnit
2020-02-03 21:59:43 +00:00
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
2020-02-03 21:59:43 +00:00
func _ready():
$move_hint.visible = false
2020-02-03 21:59:43 +00:00
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)
2020-02-09 23:28:05 +00:00
func destroy():
if get_parent() is Anchor:
(get_parent() as Anchor).set_content(null)
queue_free()
2020-02-03 21:59:43 +00:00
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
2020-02-09 23:28:05 +00:00
func can_target(other):
if other == self:
return false
else:
return true
2020-02-03 21:59:43 +00:00
func can_be_used_on_anchor(anchor):
2020-02-09 23:28:05 +00:00
return anchor is Anchor and anchor.is_connected_to(get_parent()) and (anchor.is_empty() or can_target(anchor.get_content()))
2020-02-03 21:59:43 +00:00
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"))
2020-02-03 21:59:43 +00:00
func play(anchor):
2020-02-09 23:28:05 +00:00
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):
2020-02-03 21:59:43 +00:00
anchor.set_content(self)
2020-02-09 23:28:05 +00:00
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
2020-02-03 21:59:43 +00:00
func return_to_base():
dragged = null
$move_hint.visible = false
$move_hint.set_point_position(1, Vector2(0, 0))
2020-02-03 21:59:43 +00:00
func _gui_input(event):
if event is InputEventMouseButton and event.button_index == 1:
if event.pressed:
dragged = event.position
$move_hint.visible = true
2020-02-03 21:59:43 +00:00
elif dragged:
dragged = null
$move_hint.visible = false
2020-02-03 21:59:43 +00:00
if selected_anchor:
play(selected_anchor)
else:
return_to_base()
if event is InputEventMouseMotion and event.button_mask == BUTTON_LEFT:
dragged = event.position
2020-02-03 21:59:43 +00:00
update_anchors()
if dragged:
$move_hint.set_point_position(1, selected_anchor.global_position - rect_global_position if selected_anchor else dragged)