33 lines
673 B
GDScript
33 lines
673 B
GDScript
extends Spatial
|
|
class_name Damageable
|
|
|
|
const GROUP_STRING : String = "damageable"
|
|
|
|
export var max_hp : int = 1
|
|
export var start_broken : bool = false
|
|
|
|
var current_hp : int = 1 setget set_hp
|
|
|
|
func _init():
|
|
pass
|
|
|
|
func _ready():
|
|
var starting_hp : int = max_hp
|
|
if start_broken:
|
|
starting_hp = 0
|
|
set_hp(starting_hp)
|
|
|
|
func take_damage(damage : int = 1):
|
|
assert(damage > 0)
|
|
set_hp(current_hp - damage)
|
|
|
|
func set_hp(hp_to_set : int):
|
|
current_hp = clamp(hp_to_set,0,max_hp)
|
|
if current_hp == 0 and is_in_group(GROUP_STRING):
|
|
remove_from_group(GROUP_STRING)
|
|
if current_hp > 0 and !is_in_group(GROUP_STRING):
|
|
add_to_group(GROUP_STRING)
|
|
|
|
func full_heal():
|
|
set_hp(max_hp)
|