extends KinematicBody2D

var player
var motion = Vector2()
export var speed = 150
export var attackrange = 64
export var reactiontime = 1
var active = false
export var detectionzone = 128
export var exppt = 15

func _ready():
	player = get_tree().get_nodes_in_group('player')[0]
	$types.play($types.get_animation_list()[round(rand_range(1,2))])
	pass # Replace with function body.

func _physics_process(delta):
	if player:
		for x in range(-1,1):
			for y in range(-1,1):
				if checkapproxpos(Vector2(x,y)):
					active = true
		if active:
			reactiontime -= 1
			if reactiontime == 0:
				motion = (player.global_position - global_position).normalized()
				reactiontime = 180
				$hitbox.position = motion * attackrange
			move_and_slide(motion * speed)

func checkapproxpos(var offset = Vector2()):
	var pos1 = Vector2(int(player.global_position.x/detectionzone), int(player.global_position.y/detectionzone))
	var pos2 = Vector2(int(global_position.x/detectionzone), int(global_position.y/detectionzone))
	if pos1.y+offset.y == pos2.y+offset.y and  pos1.x+offset.x == pos2.x+offset.x: 
		return true
