extends KinematicBody2D

var input = ''
var input2 = ''
var animation = 'idle'
var motion = Vector2()
export var bulletoffset = 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
	get_tree().get_nodes_in_group('server')[0].hp_update(vida, name)
	if vida <= 0: 
		get_tree().get_nodes_in_group('server')[0].player_death(name)
		queue_free()

func regenhp(arg):
	vida += arg
	get_tree().get_nodes_in_group('server')[0].hp_update(vida, name)

var sidebuffer = true

export var dmg = 35
var waitframes_bullet = 12
var aux_bullet = 12

var bullet = preload('res://bullet.tscn')

func melee():
	attack_buffers.melee += 1
	if fmod(attack_buffers.melee,45) == 0:
		dmg()
		get_tree().get_nodes_in_group('server')[0].player_anim('melee',name)

func dmg(arg = Vector2.ZERO):
	var dir = Vector2.ZERO
	dir.x = 80 if sidebuffer else -80

func _physics_process(delta):
	if motion.x != 0: sidebuffer = motion.x > 0
	motion.y += 15
	if motion.y < 0 and is_on_ceiling(): motion.y = 0
	if motion.y > 0 and is_on_floor(): motion.y = 0
	aux_bullet -= 1
	match input2:
		"ui_attack":
			if aux_bullet <= 0:
				var instance = bullet.instance()
				instance.motion.x = 500 if sidebuffer else -500
				instance.global_position = global_position + bulletoffset
				instance.id = name
				get_tree().get_nodes_in_group('proj')[0].add_child(instance)
				get_tree().get_nodes_in_group('server')[0].pshootanim(name)
				aux_bullet = waitframes_bullet
			
	match input:
		"ui_jump":
			if is_on_floor():
				motion.y = -400
		"ui_left":
			motion.x = -200
			animation = 'walk'
		"ui_right":
			motion.x = 200
			animation = 'walk'
		_:
			motion.x = 0
			animation = 'idle'
	if not is_on_floor(): 
		onair -= 1
	else:
		onair = 5
	if onair <= 0: animation = 'jump'
	move_and_slide(motion,Vector2.UP)

var onair = 5

#power code

func add_power(arg):
	print(arg)

