extends KinematicBody2D

var input = ''
var animation = 'idle'
var motion = Vector2()

func _ready():
	print('generated clinet physics of id: ' + name)

var attacks = ['melee']

var damage = preload('res://damage_player.tscn')

var attack_buffers = {'melee':0}
var vida = 100

func damage(arg, id):
	vida += arg
#	if vida <= 0: 
#		vida = 0
	get_node('../..').hp_update(vida, name)
	if vida <= 0: 
		queue_free()

func regenhp(arg):
	vida += arg
	get_node('../..').hp_update(vida, name)

var sidebuffer = true

#func melee():
#	attack_buffers.melee += 1
#	if fmod(attack_buffers.melee,45) == 0:
#		var newdmg = damage.instance()
#		newdmg.global_position = global_position + (Vector2(70,0) if sidebuffer else Vector2(-70,0))
#		newdmg.attacker = name
#		get_node('../../dmgbox').add_child(newdmg)
#		get_node('../..').player_anim('melee',name)

export var dmg = 35


func melee():
	attack_buffers.melee += 1
	if fmod(attack_buffers.melee,45) == 0:
		dmg()
		get_node('../..').player_anim('melee',name)

func dmg(arg = Vector2.ZERO):
	var dir = Vector2.ZERO
	dir.x = 80 if sidebuffer else -80
#	get_node('../../../Server/enemies').damage_detection(global_position + dir+arg,dmg,name)

func _physics_process(delta):
#	get_node('../..').hp_update(vida, name)
#	print('14')
	if motion.x != 0: sidebuffer = motion.x > 0
	for i in attacks:
		funcref(self,i).call_func()
	match input:
		"ui_down":
			motion.y = 100
			animation = 'walk'
		"ui_up":
			motion.y = -100
			animation = 'walk'
		"ui_left":
			motion.x = -100
			animation = 'walk'
		"ui_right":
			motion.x = 100
			animation = 'walk'
		_:
			motion = Vector2.ZERO
			animation = 'idle'
	move_and_slide(motion)

#power code

func add_power(arg):
	print(arg)
