ld38-blacksheep/scripts/fence.gd

41 lines
879 B
GDScript

extends StaticBody2D
signal hp_changed(b)
signal hp_empty(b)
# Defaults
var max_health = 50
var min_health = 0
var health = 50
func _ready():
# Called every time the node is added to the scene.
# Initialization here
get_node('HealthBar').set_max(max_health)
get_node('HealthBar').set_value(max_health)
pass
func heal(amount):
change_hp(amount)
func damage(amount):
change_hp(-amount)
func change_hp(amount):
var previous_health = health;
health += amount
if (health > max_health):
health = max_health
if (previous_health != health):
emit_signal('hp_changed', self)
if (health <= min_health):
emit_signal('hp_empty', self)
queue_free()
get_node('HealthBar').set_value(health)
if (health < max_health):
if (get_global_rot() != 0):
get_node('HealthBar').set_rotation(-get_rot())
get_node('HealthBar').show()
else:
get_node('HealthBar').hide()