extends "res://Scenes/Enemies/Enemy.gd"

var MainInstances = ResourceLoader.MainInstances

const Bullet = preload("res://Scenes/Objects/EnemyBullet.tscn")

export (int) var ACCELERATION = 70

onready var rightWallCheck = $RightWallCheck
onready var leftWallCheck = $LeftWallCheck

func _process(delta):
	chase_player(delta)
	
func chase_player(delta):
	var player = MainInstances.Player
	if player != null: # We not dead
		var direction_to_move = sign(player.global_position.x - global_position.x)
		motion.x += ACCELERATION * delta * direction_to_move
		motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
		global_position.x += motion.x * delta
		rotation_degrees = lerp(rotation_degrees, (motion.x / MAX_SPEED) * 10, 0.3)
		
		if rightWallCheck.is_colliding() and motion.x > 0:
			motion.x *= -0.5
			
		if rightWallCheck.is_colliding() and motion.x <= 0:
			motion.x *= -0.5

func fire_bullet() -> void:
	var bullet = Utils.instance_scene_on_main(Bullet, global_position)
	var velocity = Vector2.DOWN * 50
	velocity = velocity.rotated(deg2rad(rand_range(-30, 30)))
	bullet.velocity = velocity

func _on_Timer_timeout():
	fire_bullet()
