extends KinematicBody2D

var vida = 2
var speed = 35
var type = 'goblin'

func damage(arg, id):
	vida += arg
	if vida <= 0:
		get_parent().death_count(id)
		queue_free()

var motion = Vector2()
var target
onready var reaction_time = int(rand_range(20,60))

func _ready():
	var array = get_node('../../players').get_children()
	array.shuffle()
	if array.size() > 0:
		target = array[0]

var dir_buffer = 0

var tick = 0
export var tickrate = 4

func _physics_process(delta):
	tick += 1
	if fmod(tick,tickrate) == 0:
		if target == null: _ready()
		if target == null: return
		dir_buffer += 1
		if fmod(dir_buffer,reaction_time) == 0:
			motion = (target.global_position - global_position).normalized() * speed
		position += motion*delta*tickrate
