62 lines
1.9 KiB
C++
62 lines
1.9 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Ammo/LMItemDropComponent.h"
|
|
|
|
#include "LMHealthPack.h"
|
|
#include "Ammo/LMAmmoPack.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Player/LMPlayer.h"
|
|
#include "Weapon/LMWeaponManager.h"
|
|
#include "Windows/WindowsApplication.h"
|
|
|
|
|
|
ULMItemDropComponent::ULMItemDropComponent()
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = false;
|
|
}
|
|
|
|
void ULMItemDropComponent::TryDropItems()
|
|
{
|
|
float Result = FMath::RandRange(0.f, 100.f);
|
|
|
|
if (DropChanceHealthPack >= Result)
|
|
{
|
|
SpawnHealth();
|
|
}
|
|
|
|
if (ALMPlayer* Player = Cast<ALMPlayer>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)))
|
|
{
|
|
Result = FMath::RandRange(0.f, 1.f);
|
|
const FLMAmmoData Test = Player->GetWeaponManager()->GetAmmoDataMap().FindRef(AmmoType);
|
|
const float Percentage = static_cast<float>(Test.AmmoCount) / static_cast<float>(Test.MaxAmmo);
|
|
const float DropChance = DropChanceAtRemainingAmmo->GetFloatValue(Percentage);
|
|
|
|
FString Random = FString::Printf(TEXT("Drop Chance of %f for ammo (%i / %i) %f with random at %f"), DropChance, Test.AmmoCount, Test.MaxAmmo, Percentage, Result);
|
|
GEngine->AddOnScreenDebugMessage(INDEX_NONE, 5.f, FColor::Green, Random);
|
|
if (DropChance >= Result)
|
|
{
|
|
SpawnAmmo();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ULMItemDropComponent::SpawnAmmo()
|
|
{
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
|
|
|
ALMAmmoPack* Pack = GetWorld()->SpawnActor<ALMAmmoPack>(AmmoPack, GetOwner()->GetActorTransform(), SpawnParams);
|
|
}
|
|
|
|
void ULMItemDropComponent::SpawnHealth()
|
|
{
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
|
|
|
|
ALMHealthPack* Pack = GetWorld()->SpawnActor<ALMHealthPack>(HealthPack, GetOwner()->GetActorTransform(), SpawnParams);
|
|
}
|
|
|
|
|
|
|