-- Enemy constructor

function Enemy.new(enemyType, location, target, difficulty)
	assert(enemies[enemyType], "Invalid enemy type")
	
	local self = setmetatable({}, Enemy)
	
	self._model = enemies[enemyType]:Clone()	
	
	-- Disable unnecessary behaviors of humanoid to make game more efficient
	self._model.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	self._model.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
	self._model.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
	
	-- Set enemy health based on difficulty
	self._model.Humanoid.MaxHealth = GameSettings.EnemyHealthDifficulty[difficulty]
	self._model.Humanoid.Health = GameSettings.EnemyHealthDifficulty[difficulty]
	
	-- Tag the enemy's model
	CollectionService:AddTag(self._model, "Enemy")
	
	self._model.Humanoid.Died:Connect(function()
		self:_destroy()
	end)
	
	self._model.HumanoidRootPart.Touched:Connect(function(otherPart) 
		self:_onTouch(otherPart)
	end)
	
	self._target = target
	self._active = false
	
	self._model:SetPrimaryPartCFrame(location)
	self._model.Parent = game.Workspace.Targets
	
	return self
end

function Enemy:_update()
	if self._model and self._model:FindFirstChild("Humanoid") then
		local targetPosition = self._target.PrimaryPart.Position
		local targetVelocity = self._target.PrimaryPart.AssemblyLinearVelocity
		local predictedPosition = targetPosition + targetVelocity * 4
		self._model.Humanoid:MoveTo(targetPosition)
	end
end
