Make Enemys Pooled

This commit is contained in:
baz 2025-01-24 21:51:01 +00:00
parent 741a41ddfa
commit 58d4a57226
16 changed files with 179 additions and 21 deletions

BIN
Content/Enemy/BP_EnemyCharacter.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Enemy/BP_TestEnemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Enemy/DA_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Gamemode/BP_DefaultGamemode.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -3,11 +3,15 @@
#include "EnemyCharacter.h"
#include "EnemyDataAsset.h"
#include "EXPPickup.h"
#include "HealthComponent.h"
#include "ObjectPoolComponent.h"
#include "ObjectPoolManager.h"
#include "PaperFlipbookComponent.h"
#include "VampireAIController.h"
#include "VampireGameMode.h"
#include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h"
AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer)
@ -49,6 +53,49 @@ void AEnemyCharacter::OnDeath(FDamageInfo damageInfo)
actorSpawnParameters);
}
void AEnemyCharacter::LoadDataFromDataAsset_Implementation(UEnemyDataAsset* enemyDataAsset)
{
if (enemyDataAsset != nullptr)
{
// TODO: Load more data
PaperFlipbookComponent->SetFlipbook(enemyDataAsset->PaperFlipbook);
BehaviorTree = enemyDataAsset->BehaviorTree;
}
}
void AEnemyCharacter::ResetData_Implementation()
{
// TODO: Reset more data
PaperFlipbookComponent->SetFlipbook(nullptr);
BehaviorTree = nullptr;
}
float AEnemyCharacter::GetCapsuleRadius_Implementation()
{
return GetCapsuleComponent() ? GetCapsuleComponent()->GetScaledCapsuleRadius() : -1.0f;
}
void AEnemyCharacter::SpawnController_Implementation()
{
if (!Controller)
{
SpawnDefaultController();
}
if (BehaviorTree != nullptr)
{
AVampireAIController* controller = Cast<AVampireAIController>(Controller);
controller->RunBehaviorTree(BehaviorTree);
}
}
UHealthComponent* AEnemyCharacter::GetEnemyHealthComponent_Implementation()
{
return GetHealthComponent();
}
void AEnemyCharacter::ResetHealth()
{
GetHealthComponent()->ResetHealth();

View File

@ -3,8 +3,8 @@
#pragma once
#include "CoreMinimal.h"
#include "HealthComponent.h"
#include "VampireCharacter.h"
#include "Interfaces/Enemyable.h"
#include "EnemyCharacter.generated.h"
class UObjectPoolComponent;
@ -14,7 +14,7 @@ class AEXPPickup;
*
*/
UCLASS()
class VAMPIRES_API AEnemyCharacter : public AVampireCharacter
class VAMPIRES_API AEnemyCharacter : public AVampireCharacter, public IEnemyable
{
GENERATED_BODY()
@ -44,6 +44,21 @@ public:
UFUNCTION()
virtual void OnDeath(FDamageInfo damageInfo);
UFUNCTION()
virtual void LoadDataFromDataAsset_Implementation(UEnemyDataAsset* enemyDataAsset) override;
UFUNCTION()
virtual void ResetData_Implementation() override;
UFUNCTION()
virtual float GetCapsuleRadius_Implementation() override;
UFUNCTION()
virtual void SpawnController_Implementation() override;
UFUNCTION()
virtual UHealthComponent* GetEnemyHealthComponent_Implementation() override;
private:
UFUNCTION()

View File

@ -0,0 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyDataAsset.h"

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "EnemyDataAsset.generated.h"
class UPaperFlipbook;
class UBehaviorTree;
/**
*
*/
UCLASS()
class VAMPIRES_API UEnemyDataAsset : public UDataAsset
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere)
UPaperFlipbook* PaperFlipbook;
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
UBehaviorTree* BehaviorTree = nullptr;
};

View File

@ -0,0 +1,7 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Enemyable.h"
// Add default functionality here for any IEnemyable functions that are not pure virtual.

View File

@ -0,0 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Enemyable.generated.h"
class UHealthComponent;
class UEnemyDataAsset;
// This class does not need to be modified.
UINTERFACE()
class UEnemyable : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class VAMPIRES_API IEnemyable
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
UFUNCTION(BlueprintNativeEvent)
void LoadDataFromDataAsset(UEnemyDataAsset* projectileDataAsset);
UFUNCTION(BlueprintNativeEvent)
void ResetData();
UFUNCTION(BlueprintNativeEvent)
float GetCapsuleRadius();
UFUNCTION(BlueprintNativeEvent)
void SpawnController();
UFUNCTION(BlueprintNativeEvent)
UHealthComponent* GetEnemyHealthComponent();
};

View File

@ -4,6 +4,7 @@
#include "Projectile.h"
#include "EnemyCharacter.h"
#include "HealthComponent.h"
#include "ObjectPoolManager.h"
#include "ProjectileDataAsset.h"
#include "Components/SphereComponent.h"

View File

@ -78,24 +78,26 @@ void AVampireGameMode::SpawnEnemy()
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
if (AActor* object = GetEnemyObjectPoolManager_Implementation()->GetObject())
if (AActor* enemy = GetEnemyObjectPoolManager_Implementation()->GetObject())
{
AEnemyCharacter* Actor = Cast<AEnemyCharacter>(object);
Actor->SetActorTransform(Transform);
float CapsuleRadius = Actor->GetCapsuleComponent()->GetScaledCapsuleRadius();
FVector Direction = SpawnLocation - PlayerCharacter->GetActorLocation();
Direction.Normalize();
Direction *= CapsuleRadius;
Actor->SetActorLocation(SpawnLocation + Direction);
if (!Actor->Controller)
if (UKismetSystemLibrary::DoesImplementInterface(enemy, UEnemyable::StaticClass()) && EnemyDataAssets.Num() > 0)
{
Actor->SpawnDefaultController();
}
IEnemyable::Execute_LoadDataFromDataAsset(enemy, EnemyDataAssets[FMath::RandRange(0, EnemyDataAssets.Num() - 1)]);
enemy->SetActorTransform(Transform);
float CapsuleRadius = IEnemyable::Execute_GetCapsuleRadius(enemy);
FVector Direction = SpawnLocation - PlayerCharacter->GetActorLocation();
Direction.Normalize();
Direction *= CapsuleRadius;
enemy->SetActorLocation(SpawnLocation + Direction);
if (!Actor->GetHealthComponent()->OnDeath.IsAlreadyBound(this, &AVampireGameMode::HandleOnEnemyDeath))
{
Actor->GetHealthComponent()->OnDeath.AddDynamic(this, &AVampireGameMode::HandleOnEnemyDeath);
IEnemyable::Execute_SpawnController(enemy);
UHealthComponent* healthComponent = IEnemyable::Execute_GetEnemyHealthComponent(enemy);
if (!healthComponent->OnDeath.IsAlreadyBound(this, &AVampireGameMode::HandleOnEnemyDeath))
{
healthComponent->OnDeath.AddDynamic(this, &AVampireGameMode::HandleOnEnemyDeath);
}
}
}
}

View File

@ -8,6 +8,7 @@
#include "Interfaces/Pools.h"
#include "VampireGameMode.generated.h"
class UEnemyDataAsset;
class AProjectile;
class AObjectPoolManager;
class AVampirePlayerController;
@ -30,6 +31,9 @@ public:
FOnEnemyDeathCountIncrementDelegate OnEnemyDeathCountIncrementDelegate;
UPROPERTY(EditDefaultsOnly)
TArray<TObjectPtr<UEnemyDataAsset>> EnemyDataAssets;
private:
TObjectPtr<APlayerCharacter> PlayerCharacter;

View File

@ -4,6 +4,7 @@
#include "GarlicWeapon.h"
#include "Components/SphereComponent.h"
#include "vampires/EnemyCharacter.h"
#include "vampires/HealthComponent.h"
AGarlicWeapon::AGarlicWeapon()
{

View File

@ -4,6 +4,7 @@
#include "LightningRingWeapon.h"
#include "Components/SphereComponent.h"
#include "vampires/EnemyCharacter.h"
#include "vampires/HealthComponent.h"
ALightningRingWeapon::ALightningRingWeapon()
{

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=54EDB041_002DBA9B_002D35E0_002D8F53_002D9AB64E706FB2_002Fdl_003ASource_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FProgram_0020Files_003FEpic_0020Games_003FUE_005F5_002E4_003FEngine_003FSource_002Fd_003ARuntime_002Fd_003AAIModule_002Fd_003APrivate_002Ff_003AAIController_002Ecpp/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>