From f29a53d065b2aec4e34217a38593612e3ed1dd8f Mon Sep 17 00:00:00 2001 From: TjgL Date: Sat, 8 Feb 2025 11:06:16 +0100 Subject: [PATCH] Added ammo drop logic --- .../Legumix/Spawner/BP_StaticResupply.uasset | 3 ++ Content/Legumix/Weapon/BP_Ammo.uasset | 4 +-- Source/LegumeMix/Private/Ammo/LMAmmo.cpp | 6 ++-- Source/LegumeMix/Private/LMStaticResupply.cpp | 29 +++++++++++++++++++ Source/LegumeMix/Private/Player/LMPlayer.cpp | 13 ++++++--- .../Private/Weapon/LMWeaponManager.cpp | 8 ++++- Source/LegumeMix/Public/Ammo/LMAmmo.h | 5 +++- Source/LegumeMix/Public/LMStaticResupply.h | 29 +++++++++++++++++++ Source/LegumeMix/Public/Player/LMPlayer.h | 2 +- .../LegumeMix/Public/Weapon/LMWeaponManager.h | 2 +- 10 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 Content/Legumix/Spawner/BP_StaticResupply.uasset create mode 100644 Source/LegumeMix/Private/LMStaticResupply.cpp create mode 100644 Source/LegumeMix/Public/LMStaticResupply.h diff --git a/Content/Legumix/Spawner/BP_StaticResupply.uasset b/Content/Legumix/Spawner/BP_StaticResupply.uasset new file mode 100644 index 0000000..b7cd804 --- /dev/null +++ b/Content/Legumix/Spawner/BP_StaticResupply.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0d88bbf43dc30df9ad573958dba7c56ad8c944f66689c097ecb6b104987039b3 +size 37621 diff --git a/Content/Legumix/Weapon/BP_Ammo.uasset b/Content/Legumix/Weapon/BP_Ammo.uasset index 2931d44..c073f23 100644 --- a/Content/Legumix/Weapon/BP_Ammo.uasset +++ b/Content/Legumix/Weapon/BP_Ammo.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a965ccb554c95da00351d7cd0b97341a423e5db8a02946f455caae593fac8f1b -size 33627 +oid sha256:75829c440f1ff2619f0934f0beb41b8e8b11a5ce122b2e32f13b0718be1af19f +size 33651 diff --git a/Source/LegumeMix/Private/Ammo/LMAmmo.cpp b/Source/LegumeMix/Private/Ammo/LMAmmo.cpp index e5bd484..0fdaed9 100644 --- a/Source/LegumeMix/Private/Ammo/LMAmmo.cpp +++ b/Source/LegumeMix/Private/Ammo/LMAmmo.cpp @@ -30,8 +30,10 @@ void ALMAmmo::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* { if (ALMPlayer* Player = Cast(OtherActor)) { - Player->PickUpAmmo(this); - Destroy(); + if (Player->PickUpAmmo(this)) + { + Destroy(); + } } } diff --git a/Source/LegumeMix/Private/LMStaticResupply.cpp b/Source/LegumeMix/Private/LMStaticResupply.cpp new file mode 100644 index 0000000..5e48582 --- /dev/null +++ b/Source/LegumeMix/Private/LMStaticResupply.cpp @@ -0,0 +1,29 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "LMStaticResupply.h" + +#include "Ammo/LMAmmo.h" + + +ULMStaticResupply::ULMStaticResupply() +{ + PrimaryComponentTick.bCanEverTick = true; + +} + + +void ULMStaticResupply::BeginPlay() +{ + Super::BeginPlay(); + + +} + +void ULMStaticResupply::SpawnResupply_Implementation() +{ + FVector SpawnLocation = GetOwner()->GetActorLocation(); + FRotator SpawnRotation = GetOwner()->GetActorRotation(); + + ALMAmmo* Ammo = GetWorld()->SpawnActor(AmmoToSpawn, SpawnLocation, SpawnRotation); +} diff --git a/Source/LegumeMix/Private/Player/LMPlayer.cpp b/Source/LegumeMix/Private/Player/LMPlayer.cpp index 8a3a8af..f01a0cd 100644 --- a/Source/LegumeMix/Private/Player/LMPlayer.cpp +++ b/Source/LegumeMix/Private/Player/LMPlayer.cpp @@ -35,15 +35,20 @@ void ALMPlayer::BeginPlay() WeaponManager->WeaponSwitched.AddUniqueDynamic(this, &ALMPlayer::WeaponSwitched); } -void ALMPlayer::PickUpAmmo(ALMAmmo* Ammo) +bool ALMPlayer::PickUpAmmo(ALMAmmo* Ammo) { if (GEngine) { - FString AmmoAmount = FString::Printf(TEXT("Quantité de munition : %i"), Ammo->GetAmmoAmount()); + const FString AmmoAmount = FString::Printf(TEXT("Quantité de munition : %i"), Ammo->GetAmmoAmount()); GEngine->AddOnScreenDebugMessage(INDEX_NONE, 30.f, FColor::Red, AmmoAmount); } - WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount()); - OnAmmoPickup(); + + if (WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount())) + { + OnAmmoPickup(); + return true; + } + return false; } int ALMPlayer::GetAmmoCount(const EAmmoType AmmoType) const diff --git a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp index 0e1b330..f5e1a13 100644 --- a/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp +++ b/Source/LegumeMix/Private/Weapon/LMWeaponManager.cpp @@ -108,13 +108,17 @@ void ULMWeaponManager::Initialize(USkeletalMeshComponent* Mesh) } } -void ULMWeaponManager::AddAmmoType(EAmmoType AmmoType, int AmmoCount) +bool ULMWeaponManager::AddAmmoType(EAmmoType AmmoType, int AmmoCount) { FString Debug = FString::Printf(TEXT("Adding %i ammo of type %i"), AmmoCount, AmmoType); GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Cyan, Debug); if (AmmoData.Contains(AmmoType)) { + FLMAmmoData Data = AmmoData[AmmoType]; + if (Data.AmmoCount >= Data.MaxAmmo) + return false; + AmmoData[AmmoType].AddAmmo(AmmoCount); } else @@ -127,6 +131,8 @@ void ULMWeaponManager::AddAmmoType(EAmmoType AmmoType, int AmmoCount) Data.AmmoCount = AmmoCount; AmmoData[AmmoType] = Data; } + + return true; } int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType) diff --git a/Source/LegumeMix/Public/Ammo/LMAmmo.h b/Source/LegumeMix/Public/Ammo/LMAmmo.h index a799d5a..8318394 100644 --- a/Source/LegumeMix/Public/Ammo/LMAmmo.h +++ b/Source/LegumeMix/Public/Ammo/LMAmmo.h @@ -25,6 +25,9 @@ public: UFUNCTION(BlueprintCallable) inline EAmmoType GetAmmoType() { return AmmoType; } + UFUNCTION(BlueprintCallable) + void SetAmmoCount(const int Count) { AmmoAmount = Count; } + protected: virtual void BeginPlay() override; @@ -37,7 +40,7 @@ protected: UPROPERTY(EditAnywhere) EAmmoType AmmoType; - UPROPERTY(EditAnywhere) + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(ExposeOnSpawn)) int AmmoAmount; UFUNCTION(BlueprintCallable) diff --git a/Source/LegumeMix/Public/LMStaticResupply.h b/Source/LegumeMix/Public/LMStaticResupply.h new file mode 100644 index 0000000..549faad --- /dev/null +++ b/Source/LegumeMix/Public/LMStaticResupply.h @@ -0,0 +1,29 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "LMStaticResupply.generated.h" + + +class ALMAmmo; + +UCLASS(Blueprintable, ClassGroup=Legumix, meta=(BlueprintSpawnableComponent)) +class LEGUMEMIX_API ULMStaticResupply : public UActorComponent +{ + GENERATED_BODY() + +public: + ULMStaticResupply(); + + UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Legumix) + void SpawnResupply(); + +protected: + virtual void BeginPlay() override; + +private: + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Legumix, meta = (AllowPrivateAccess = true)) + TSubclassOf AmmoToSpawn; +}; diff --git a/Source/LegumeMix/Public/Player/LMPlayer.h b/Source/LegumeMix/Public/Player/LMPlayer.h index d8e4909..e9ed6f6 100644 --- a/Source/LegumeMix/Public/Player/LMPlayer.h +++ b/Source/LegumeMix/Public/Player/LMPlayer.h @@ -23,7 +23,7 @@ public: ALMPlayer(); UFUNCTION(BlueprintCallable) - void PickUpAmmo(ALMAmmo* Ammo); + bool PickUpAmmo(ALMAmmo* Ammo); /** * Gets the number of ammo from the given type. diff --git a/Source/LegumeMix/Public/Weapon/LMWeaponManager.h b/Source/LegumeMix/Public/Weapon/LMWeaponManager.h index 840e410..5577da2 100644 --- a/Source/LegumeMix/Public/Weapon/LMWeaponManager.h +++ b/Source/LegumeMix/Public/Weapon/LMWeaponManager.h @@ -35,7 +35,7 @@ public: void SetWeapon(int Index); UFUNCTION(BlueprintCallable, Category=Legumix) - void AddAmmoType(EAmmoType AmmoType, int AmmoCount); + bool AddAmmoType(EAmmoType AmmoType, int AmmoCount); UFUNCTION(BlueprintCallable, Category=Legumix) int GetAmmoCount(EAmmoType AmmoType);