Added ammo drop logic

This commit is contained in:
TjgL 2025-02-08 11:06:16 +01:00
parent ef04a66138
commit f29a53d065
10 changed files with 89 additions and 12 deletions

BIN
Content/Legumix/Spawner/BP_StaticResupply.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Legumix/Weapon/BP_Ammo.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -30,10 +30,12 @@ void ALMAmmo::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor*
{ {
if (ALMPlayer* Player = Cast<ALMPlayer>(OtherActor)) if (ALMPlayer* Player = Cast<ALMPlayer>(OtherActor))
{ {
Player->PickUpAmmo(this); if (Player->PickUpAmmo(this))
{
Destroy(); Destroy();
} }
} }
}
void ALMAmmo::Tick(float DeltaTime) void ALMAmmo::Tick(float DeltaTime)
{ {

View File

@ -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<ALMAmmo>(AmmoToSpawn, SpawnLocation, SpawnRotation);
}

View File

@ -35,15 +35,20 @@ void ALMPlayer::BeginPlay()
WeaponManager->WeaponSwitched.AddUniqueDynamic(this, &ALMPlayer::WeaponSwitched); WeaponManager->WeaponSwitched.AddUniqueDynamic(this, &ALMPlayer::WeaponSwitched);
} }
void ALMPlayer::PickUpAmmo(ALMAmmo* Ammo) bool ALMPlayer::PickUpAmmo(ALMAmmo* Ammo)
{ {
if (GEngine) 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); GEngine->AddOnScreenDebugMessage(INDEX_NONE, 30.f, FColor::Red, AmmoAmount);
} }
WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount());
if (WeaponManager->AddAmmoType(Ammo->GetAmmoType(), Ammo->GetAmmoAmount()))
{
OnAmmoPickup(); OnAmmoPickup();
return true;
}
return false;
} }
int ALMPlayer::GetAmmoCount(const EAmmoType AmmoType) const int ALMPlayer::GetAmmoCount(const EAmmoType AmmoType) const

View File

@ -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); FString Debug = FString::Printf(TEXT("Adding %i ammo of type %i"), AmmoCount, AmmoType);
GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Cyan, Debug); GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Cyan, Debug);
if (AmmoData.Contains(AmmoType)) if (AmmoData.Contains(AmmoType))
{ {
FLMAmmoData Data = AmmoData[AmmoType];
if (Data.AmmoCount >= Data.MaxAmmo)
return false;
AmmoData[AmmoType].AddAmmo(AmmoCount); AmmoData[AmmoType].AddAmmo(AmmoCount);
} }
else else
@ -127,6 +131,8 @@ void ULMWeaponManager::AddAmmoType(EAmmoType AmmoType, int AmmoCount)
Data.AmmoCount = AmmoCount; Data.AmmoCount = AmmoCount;
AmmoData[AmmoType] = Data; AmmoData[AmmoType] = Data;
} }
return true;
} }
int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType) int ULMWeaponManager::GetAmmoCount(const EAmmoType AmmoType)

View File

@ -25,6 +25,9 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
inline EAmmoType GetAmmoType() { return AmmoType; } inline EAmmoType GetAmmoType() { return AmmoType; }
UFUNCTION(BlueprintCallable)
void SetAmmoCount(const int Count) { AmmoAmount = Count; }
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -37,7 +40,7 @@ protected:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
EAmmoType AmmoType; EAmmoType AmmoType;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(ExposeOnSpawn))
int AmmoAmount; int AmmoAmount;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)

View File

@ -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<ALMAmmo> AmmoToSpawn;
};

View File

@ -23,7 +23,7 @@ public:
ALMPlayer(); ALMPlayer();
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void PickUpAmmo(ALMAmmo* Ammo); bool PickUpAmmo(ALMAmmo* Ammo);
/** /**
* Gets the number of ammo from the given type. * Gets the number of ammo from the given type.

View File

@ -35,7 +35,7 @@ public:
void SetWeapon(int Index); void SetWeapon(int Index);
UFUNCTION(BlueprintCallable, Category=Legumix) UFUNCTION(BlueprintCallable, Category=Legumix)
void AddAmmoType(EAmmoType AmmoType, int AmmoCount); bool AddAmmoType(EAmmoType AmmoType, int AmmoCount);
UFUNCTION(BlueprintCallable, Category=Legumix) UFUNCTION(BlueprintCallable, Category=Legumix)
int GetAmmoCount(EAmmoType AmmoType); int GetAmmoCount(EAmmoType AmmoType);