Allow for multiple projectiles to be spawned per weapon activation

This commit is contained in:
baz 2025-04-15 21:22:50 +01:00
parent e4d1e05fea
commit c5435718de
5 changed files with 51 additions and 4 deletions

View File

@ -69,12 +69,14 @@ void AProjectile::LoadDataFromDataAsset_Implementation(UProjectileDataAsset* pro
ProjectileSpeed = projectileDataAsset->ProjectileSpeed;
ProjectileMovement->InitialSpeed = ProjectileSpeed;
ProjectileMovement->MaxSpeed = ProjectileSpeed;
RemainingDamagableEnemies = projectileDataAsset->DamagableEnemies;
}
void AProjectile::ResetData_Implementation()
{
ProjectileSpeed = NULL;
StaticMeshComponent->SetStaticMesh(nullptr);
RemainingDamagableEnemies = 1;
}
void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
@ -96,13 +98,18 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon
AProjectileWeapon* ownerWeapon = Cast<AProjectileWeapon>(GetOwner());
EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this);
AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld());
RemainingDamagableEnemies--;
if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass()))
if (RemainingDamagableEnemies == 0)
{
if (AObjectPoolManager* objectPoolManager = IPools::Execute_GetProjectileObjectPoolManager(gamemode))
AGameModeBase* gamemode = UGameplayStatics::GetGameMode(GetWorld());
if (UKismetSystemLibrary::DoesImplementInterface(gamemode, UPools::StaticClass()))
{
objectPoolManager->ReturnObject(this);
if (AObjectPoolManager* objectPoolManager = IPools::Execute_GetProjectileObjectPoolManager(gamemode))
{
objectPoolManager->ReturnObject(this);
}
}
}
}

View File

@ -31,6 +31,9 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStaticMeshComponent* StaticMeshComponent = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int RemainingDamagableEnemies = 1;
// Sets default values for this actor's properties
AProjectile();

View File

@ -20,4 +20,7 @@ public:
UPROPERTY(EditAnywhere)
TObjectPtr<UStaticMesh> StaticMesh;
UPROPERTY(EditAnywhere)
int DamagableEnemies = 1;
};

View File

@ -63,3 +63,20 @@ void AProjectileWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp,
OverlappedEnemies.Remove(Enemy);
}
}
void AProjectileWeapon::FireWeaponAction_Implementation()
{
Super::FireWeaponAction_Implementation();
remainingProjectilesToSpawn = ProjectilesPerActivation;
GetWorldTimerManager().SetTimer(FireProjectileTimerHandler, this, &AProjectileWeapon::FireProjectile, ProjectileSpawningDelay, true, 0.0f);
}
void AProjectileWeapon::FireProjectile()
{
remainingProjectilesToSpawn--;
if (remainingProjectilesToSpawn == 0)
{
GetWorldTimerManager().ClearTimer(FireProjectileTimerHandler);
}
}

View File

@ -25,9 +25,20 @@ public:
UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TObjectPtr<UProjectileDataAsset> ProjectileTemplate = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
int ProjectilesPerActivation = 1;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float ProjectileSpawningDelay = 0.25f;
protected:
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
FTimerHandle FireProjectileTimerHandler;
private:
int remainingProjectilesToSpawn = 0;
public:
AProjectileWeapon();
@ -43,4 +54,10 @@ public:
UFUNCTION()
void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
virtual void FireWeaponAction_Implementation() override;
protected:
UFUNCTION()
virtual void FireProjectile();
};