// Fill out your copyright notice in the Description page of Project Settings.


#include "ShotgunClass.h"
#include "EndlessVendetta/AI/EnemyCharacter.h"
#include "Engine/DamageEvents.h"

AShotgunClass::AShotgunClass()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	ShotgunBulletSpread = FVector(10,10,10);
}

void AShotgunClass::BeginPlay()
{
	Super::BeginPlay();
}

void AShotgunClass::Fire()
{
	if(currentAmmoCount > 0)
	{
		traceStart = GunStartArrow->GetComponentLocation();
		if (GetWorldTimerManager().IsTimerActive(timerHandle)) return;
		for (int i = 0; i < 5; i++)
		{
			FVector newStartTrace = UKismetMathLibrary::RandomPointInBoundingBox(traceStart, ShotgunBulletSpread);
			traceEnd = newStartTrace + (GunStartArrow->GetForwardVector() * BulletDistance);
			GetWorld()->LineTraceSingleByChannel(outHit, newStartTrace, traceEnd, ECC_Visibility, collisionParams);
			DrawDebugLine(this->GetWorld(), newStartTrace, traceEnd, FColor::Black , false, 0.2f, 0U, 0.2f);
			currentAmmoCount --;
		}
		playerControllerRef->PlayerCameraManager->StartCameraShake(CameraShakeClass, 1);
		GenerateRecoilVector();
		ClickDetectionTimer();
		bulletCountShoot += 1;
		bStopShooting = false;
		if (outHit.bBlockingHit)
		{
			if (outHit.Distance >= FMath::Floor(BulletDistance/2))
			{
				tempWeaponDamage = WeaponDamage / 2;
			}
			else
			{
				tempWeaponDamage = WeaponDamage;
			}
			if (!Cast<AAICharacter>(outHit.GetActor())) return;
			Cast<AAICharacter>(outHit.GetActor())->TakeDamage(tempWeaponDamage, FPointDamageEvent(), GetWorld()->GetFirstPlayerController(), this);
		}
	}
	else if(currentAmmoCount <= 0)
	{
		UE_LOG(LogTemp, Display, TEXT("No ammo, Ammo count: %d"), currentAmmoCount);
	}
}

