Gun.cpp

PHOTO EMBED

Mon Aug 29 2022 08:35:01 GMT+0000 (Coordinated Universal Time)

Saved by @coduck #c++

void ABolter::BeginPlay()
{
	Super::BeginPlay();
	
	CurrentAmmoWithYou = FMath::DivideAndRoundUp(MaxAmmoCarry, 2);
	CurrentAmmoInMagazine = MaxAmmoMagazine;
	CurrentAmmoWithYou -= CurrentAmmoInMagazine;
}

void ABolter::Fire()
{
	if (CurrentAmmoInMagazine <= 0)
	{
		UGameplayStatics::PlaySoundAtLocation(SkeletalMesh, EmptyMagazineSound, 
        this->GetActorLocation(), 
        this->GetActorRotation());
		return;
	}
  
	--CurrentAmmoInMagazine;
}

void ABolter::StopFireRate()
{
	if (!GetWorldTimerManager().IsTimerActive(FireRateTimerHandle)) { return; }

	GetWorldTimerManager().ClearTimer(FireRateTimerHandle);
}

bool ABolter::IsShooting()
{
	return GetWorldTimerManager().IsTimerActive(FireRateTimerHandle);
}

void ABolter::Reload()
{
	OwnerCharacterSpaceMarine = Cast<ASpaceMarineCharacter>(GetOwner());

	if (CurrentAmmoWithYou > 0 && CurrentAmmoInMagazine != MaxAmmoMagazine && !IsShooting())
	{
		if (!OwnerCharacterSpaceMarine) { return; }

		OwnerCharacterSpaceMarine->SetIsReloading(true);
		OwnerCharacterSpaceMarine->GetCharacterMovement()->MaxWalkSpeed = 300.f;

		UGameplayStatics::PlaySoundAtLocation(SkeletalMesh, ReloadSound, this->GetActorLocation(), this->GetActorRotation());
		GetWorldTimerManager().SetTimer(ReloadTimer, this, &ABolter::CalculateBulletsAmountToReload, ReloadTime);
	}
}

void ABolter::CalculateBulletsAmountToReload()
{
	OwnerCharacterSpaceMarine->SetIsReloading(false);
	OwnerCharacterSpaceMarine->GetCharacterMovement()->MaxWalkSpeed = 600.f;

	int32 AmmoToReload = MaxAmmoMagazine - CurrentAmmoInMagazine;

	if (CurrentAmmoWithYou < AmmoToReload)
	{
		CurrentAmmoInMagazine += CurrentAmmoWithYou;
		CurrentAmmoWithYou = 0;
	}
	else
	{
		CurrentAmmoInMagazine += AmmoToReload;
		CurrentAmmoWithYou -= AmmoToReload;

		if (CurrentAmmoWithYou < 0)
		{
			CurrentAmmoWithYou = 0;
		}
	}
}

int32 ABolter::GetAmmoInMagazineValue()
{
	return CurrentAmmoInMagazine;
}

int32 ABolter::GetAmmoWithYouValue()
{
	return CurrentAmmoWithYou;
}

void ABolter::ReplenishAmmo()
{
	if (CurrentAmmoWithYou < MaxAmmoCarry)
	{
		UGameplayStatics::PlaySoundAtLocation(SkeletalMesh, TakeAmmo, this->GetActorLocation(), this->GetActorRotation());

		CurrentAmmoWithYou = MaxAmmoCarry;
	}
}
content_copyCOPY