extends Node2D

export var health_max = 0
onready var health = health_max
signal die
signal damage(dmg)
export(NodePath) var progressbar
export(NodePath) var text
var regen = 0.0
var regen_time = 7
var reactive_dmg = 0.0
var hpmult = 1.0
var buff = 0

func _physics_process(delta):
	buff += 1
	if fmod(buff,regen_time*60) == 00:
#		print('regen')
		_damage(-regen * health_max ,'',self)


var vulnerable = true

func _damage(dmg, effect, caller):
	if vulnerable:
		health -= dmg * hpmult
		health = min(health,health_max)
		emit_signal('damage', dmg)
#		print('dmg to ' + str(get_path()) + " : " + str(health) + " : " +str(dmg))
		if health <= 0:
			emit_signal('die')
			caller._expgain(get_parent().exppt)
		effect_processor(effect)
		if has_node(progressbar):
			get_node(progressbar).set_value( health )
			get_node(progressbar).set_max( health_max )
			get_node(text).set_text( str(int(health)) )
		if dmg > 0 : invutime()

var chance = 0.0
var invutime = 2.5

func invutime():
	if randf() < chance:
		vulnerable = false
		print('invunerability')
		get_parent().modulate.b = 10
		yield(Timergen.createtime(self,invutime),'timeout')
		print('vunerability')
		vulnerable = true
		get_parent().modulate.b = 1
	else:
		get_parent().modulate.r = 10
		yield(Timergen.createtime(self,0.25),'timeout')
		get_parent().modulate.r = 1


func _expgain(arg):
	pass

func _ready():
	if has_node(progressbar):
		get_node(progressbar).set_value( health )
		get_node(progressbar).set_max( health_max )
		get_node(text).set_text( str(health_max) )

func effect_processor(effect):
	pass
