ld38-blacksheep/scripts/fence.gd

50 lines
1.1 KiB
GDScript3
Raw Normal View History

2017-04-22 22:58:11 +00:00
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
2017-04-23 19:34:18 +00:00
randomize()
2017-04-22 22:58:11 +00:00
get_node('HealthBar').set_max(max_health)
2017-04-23 19:34:18 +00:00
if randf() <= 0.1:
set_hp(rand_range(max_health * 0.5, max_health))
else:
set_hp(max_health)
2017-04-22 22:58:11 +00:00
pass
func heal(amount):
change_hp(amount)
func damage(amount):
change_hp(-amount)
func change_hp(amount):
2017-04-23 19:34:18 +00:00
var previous_health = health
var next_health = previous_health + amount
if (next_health > max_health):
next_health = max_health
if (previous_health != next_health):
set_hp(next_health)
2017-04-22 22:58:11 +00:00
emit_signal('hp_changed', self)
2017-04-23 19:34:18 +00:00
if (next_health <= min_health):
2017-04-22 22:58:11 +00:00
emit_signal('hp_empty', self)
queue_free()
2017-04-23 19:34:18 +00:00
func set_hp(h):
health = h
2017-04-22 22:58:11 +00:00
get_node('HealthBar').set_value(health)
if (health < max_health):
if (get_global_rot() != 0):
get_node('HealthBar').set_rotation(-get_rot())
2017-04-23 19:34:18 +00:00
get_node('HealthBar').set_pos(Vector2(-18,15))
2017-04-22 22:58:11 +00:00
get_node('HealthBar').show()
else:
get_node('HealthBar').hide()