project-dung/scripts/damageable_object.gd

29 lines
618 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():
var starting_hp : int = max_hp
if start_broken:
starting_hp = 0
set_hp(starting_hp)
func _ready():
pass
func take_damage(damage : int = 1):
set_hp(current_hp - max_hp)
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)