From 0ee681cc61e9001190c9b0c2dcc2d1b8377c2ebc Mon Sep 17 00:00:00 2001 From: TjgL Date: Fri, 14 Mar 2025 19:19:50 +0100 Subject: [PATCH] Started working on new drop system Signed-off-by: TjgL --- Source/LegumeMix/Private/Ammo/LMAmmoPack.cpp | 10 +++ .../Private/Ammo/LMItemDropComponent.cpp | 61 +++++++++++++++++++ Source/LegumeMix/Private/LMHealthPack.cpp | 11 ++++ Source/LegumeMix/Private/LMItemDrop.cpp | 25 ++++++++ .../Private/Weapon/LMWeaponManager.cpp | 5 ++ Source/LegumeMix/Public/Ammo/LMAmmoPack.h | 24 ++++++++ .../Public/Ammo/LMItemDropComponent.h | 42 +++++++++++++ Source/LegumeMix/Public/LMHealthPack.h | 20 ++++++ Source/LegumeMix/Public/LMItemDrop.h | 33 ++++++++++ .../LegumeMix/Public/Weapon/LMWeaponManager.h | 2 + 10 files changed, 233 insertions(+) create mode 100644 Source/LegumeMix/Private/Ammo/LMAmmoPack.cpp create mode 100644 Source/LegumeMix/Private/Ammo/LMItemDropComponent.cpp create mode 100644 Source/LegumeMix/Private/LMHealthPack.cpp create mode 100644 Source/LegumeMix/Private/LMItemDrop.cpp create mode 100644 Source/LegumeMix/Public/Ammo/LMAmmoPack.h create mode 100644 Source/LegumeMix/Public/Ammo/LMItemDropComponent.h create mode 100644 Source/LegumeMix/Public/LMHealthPack.h create mode 100644 Source/LegumeMix/Public/LMItemDrop.h diff --git a/Source/LegumeMix/Private/Ammo/LMAmmoPack.cpp b/Source/LegumeMix/Private/Ammo/LMAmmoPack.cpp new file mode 100644 index 0000000..cfc4560 --- /dev/null +++ b/Source/LegumeMix/Private/Ammo/LMAmmoPack.cpp @@ -0,0 +1,10 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "Ammo/LMAmmoPack.h" + + +ALMAmmoPack::ALMAmmoPack() +{ + PrimaryActorTick.bCanEverTick = false; +} diff --git a/Source/LegumeMix/Private/Ammo/LMItemDropComponent.cpp b/Source/LegumeMix/Private/Ammo/LMItemDropComponent.cpp new file mode 100644 index 0000000..8fcf066 --- /dev/null +++ b/Source/LegumeMix/Private/Ammo/LMItemDropComponent.cpp @@ -0,0 +1,61 @@ +// 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(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))) + { + Result = FMath::RandRange(0.f, 1.f); + const FLMAmmoData Ammo = Player->GetWeaponManager()->GetAmmoData(AmmoType); + const float Percentage = static_cast(Ammo.AmmoCount) / static_cast(Ammo.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, Ammo.AmmoCount, Ammo.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(AmmoPack, GetOwner()->GetActorTransform(), SpawnParams); +} + +void ULMItemDropComponent::SpawnHealth() +{ + FActorSpawnParameters SpawnParams; + SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn; + + ALMHealthPack* Pack = GetWorld()->SpawnActor(HealthPack, GetOwner()->GetActorTransform(), SpawnParams); +} + + + diff --git a/Source/LegumeMix/Private/LMHealthPack.cpp b/Source/LegumeMix/Private/LMHealthPack.cpp new file mode 100644 index 0000000..51b9614 --- /dev/null +++ b/Source/LegumeMix/Private/LMHealthPack.cpp @@ -0,0 +1,11 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LMHealthPack.h" + + +ALMHealthPack::ALMHealthPack() +{ + PrimaryActorTick.bCanEverTick = false; +} + diff --git a/Source/LegumeMix/Private/LMItemDrop.cpp b/Source/LegumeMix/Private/LMItemDrop.cpp new file mode 100644 index 0000000..de74b26 --- /dev/null +++ b/Source/LegumeMix/Private/LMItemDrop.cpp @@ -0,0 +1,25 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LMItemDrop.h" + + +ALMItemDrop::ALMItemDrop() +{ + PrimaryActorTick.bCanEverTick = true; + StaticMeshComponent = CreateDefaultSubobject("MeshComponent"); + RootComponent = StaticMeshComponent; +} + +void ALMItemDrop::BeginPlay() +{ + Super::BeginPlay(); + GetWorldTimerManager().SetTimer(DespawnHandle, this, &ALMItemDrop::Despawn, TimeBeforeDespawn); +} + +void ALMItemDrop::Despawn() +{ + GetWorldTimerManager().ClearTimer(DespawnHandle); + Destroy(); +} + diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp index f5e1a13..86cc68e 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp @@ -143,6 +143,11 @@ int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType) return 0; } +FLMAmmoData& ULMWeaponManager::GetAmmoData(const EAmmoType AmmoType) +{ + return AmmoData[AmmoType]; +} + void ULMWeaponManager::Fire() { ALMWeaponBase* Weapon = GetCurrentWeapon(); diff --git a/Source/LegumeMix/Public/Ammo/LMAmmoPack.h b/Source/LegumeMix/Public/Ammo/LMAmmoPack.h new file mode 100644 index 0000000..71d1d65 --- /dev/null +++ b/Source/LegumeMix/Public/Ammo/LMAmmoPack.h @@ -0,0 +1,24 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "LMAmmoType.h" +#include "LMItemDrop.h" +#include "LMAmmoPack.generated.h" + +UCLASS() +class LEGUMEMIX_API ALMAmmoPack : public ALMItemDrop +{ + GENERATED_BODY() + +public: + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true, ClampMin=0)) + int AmmoCount = 6; + + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true)) + EAmmoType AmmoType = EAmmoType::EAT_CornAmmo; + +public: + ALMAmmoPack(); +}; diff --git a/Source/LegumeMix/Public/Ammo/LMItemDropComponent.h b/Source/LegumeMix/Public/Ammo/LMItemDropComponent.h new file mode 100644 index 0000000..fbef531 --- /dev/null +++ b/Source/LegumeMix/Public/Ammo/LMItemDropComponent.h @@ -0,0 +1,42 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "LMAmmoType.h" +#include "Components/ActorComponent.h" +#include "LMItemDropComponent.generated.h" + +class ALMAmmoPack; +class ALMHealthPack; + +UCLASS(Blueprintable, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) +class LEGUMEMIX_API ULMItemDropComponent : public UActorComponent +{ + GENERATED_BODY() + +private: + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true)) + TObjectPtr DropChanceAtRemainingAmmo; + + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true, ClampMin=0, ClampMax=100, UIMin=0, UIMax=100)) + float DropChanceHealthPack = 10.f; + + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true)) + EAmmoType AmmoType = EAmmoType::EAT_CornAmmo; + + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true)) + TSubclassOf AmmoPack; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true)) + TSubclassOf HealthPack; + +public: + ULMItemDropComponent(); + + UFUNCTION(BlueprintCallable) + void TryDropItems(); + UFUNCTION(BlueprintCallable) + void SpawnAmmo(); + UFUNCTION(BlueprintCallable) + void SpawnHealth(); +}; diff --git a/Source/LegumeMix/Public/LMHealthPack.h b/Source/LegumeMix/Public/LMHealthPack.h new file mode 100644 index 0000000..ef6f956 --- /dev/null +++ b/Source/LegumeMix/Public/LMHealthPack.h @@ -0,0 +1,20 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "LMItemDrop.h" +#include "LMHealthPack.generated.h" + +UCLASS() +class LEGUMEMIX_API ALMHealthPack : public ALMItemDrop +{ + GENERATED_BODY() + +public: + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix, meta=(AllowPrivateAccess=true, ClampMin=0)) + float HealthGain = 10.f; + +public: + ALMHealthPack(); +}; diff --git a/Source/LegumeMix/Public/LMItemDrop.h b/Source/LegumeMix/Public/LMItemDrop.h new file mode 100644 index 0000000..423c7dd --- /dev/null +++ b/Source/LegumeMix/Public/LMItemDrop.h @@ -0,0 +1,33 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "LMItemDrop.generated.h" + +class ULMDropData; + +UCLASS() +class LEGUMEMIX_API ALMItemDrop : public AActor +{ + GENERATED_BODY() + +protected: + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Legumix) + float TimeBeforeDespawn = 30.f; + +private: + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Legumix, meta = (AllowPrivateAccess = true)) + TObjectPtr StaticMeshComponent; + + FTimerHandle DespawnHandle; + +public: + ALMItemDrop(); + virtual void BeginPlay() override; + +protected: + UFUNCTION(BlueprintCallable) + void Despawn(); +}; diff --git a/Source/LegumeMix/Public/Weapon/LMWeaponManager.h b/Source/LegumeMix/Public/Weapon/LMWeaponManager.h index 5577da2..31e4b36 100644 --- a/Source/LegumeMix/Public/Weapon/LMWeaponManager.h +++ b/Source/LegumeMix/Public/Weapon/LMWeaponManager.h @@ -38,6 +38,8 @@ public: bool AddAmmoType(EAmmoType AmmoType, int AmmoCount); UFUNCTION(BlueprintCallable, Category=Legumix) int GetAmmoCount(EAmmoType AmmoType); + UFUNCTION(BlueprintCallable, Category=Legumix) + FLMAmmoData& GetAmmoData(EAmmoType AmmoType); UFUNCTION(BlueprintCallable, Category=Legumix) void Fire();