extends Area2D

export var dmg = 1
var effect = ''
export var affect = 'enemy'
export var apc = 1.0
signal reloaded
export var singletime = false
var enable = true
var hpsteal = 0
var stealhp = 0
signal regenhealth(arg, arg1, arg2)
var critical_dmg = 1.0
var critical_chance = 0.0

func hpsteal():
	if randf() < stealhp:
		emit_signal("regenhealth", -(dmg*hpsteal), '', self)

func _on_hitbox_body_entered(body):
	
	if not enable : return
	if singletime: enable = false
	if body.is_in_group(affect):
		$expand.set_physics_process(true)
		$expand.buff = 0
		$Target4.scale.y = 1
		$Target4.scale.x = 1
		if body.has_node('Health'):
			if randf() > critical_chance:
				body.get_node('Health')._damage(dmg, effect, self)
			else:
				body.get_node('Health')._damage(dmg*critical_chance, effect, self)
				print('crit')
			hpsteal()
		get_parent().get_node('Health')._damage(body.get_node('Health').reactive_dmg,'',self)
#		print('reactive dmg is : ' + str(body.get_node('Health').reactive_dmg))
	$CollisionShape2D.call_deferred('set_disabled', true)
	$Timer.wait_time = 1.0/apc
	yield($Timer, 'timeout')
	$CollisionShape2D.call_deferred('set_disabled', false)
	emit_signal('reloaded')
	pass # Replace with function body.

signal gotxp(arg)

func _expgain(exppt):
	print(exppt)
	emit_signal('gotxp',exppt)

