Change GarlicWeapon to use a timer per OverlappedEnemy

This commit is contained in:
baz 2024-09-05 00:36:44 +01:00
parent c3bbb1ac6e
commit fc93e35ef1
3 changed files with 62 additions and 39 deletions

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -20,40 +20,6 @@ void AGarlicWeapon::BeginPlay()
void AGarlicWeapon::FireWeaponAction_Implementation() void AGarlicWeapon::FireWeaponAction_Implementation()
{ {
TArray<AEnemyCharacter*> OverlappedEnemiesCache = OverlappedEnemies;
for (int i = 0; i < OverlappedEnemiesCache.Num(); i++)
{
bool deadCheck = false;
UHealthComponent* EnemyHealthComponent = OverlappedEnemiesCache[i]->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
FVector Direction = OverlappedEnemiesCache[i]->GetActorLocation() - this->GetActorLocation();
Direction.Normalize();
float distance = SphereComponent->GetScaledSphereRadius();
Direction *= distance;
OverlappedEnemiesCache[i]->SetActorLocation(OverlappedEnemiesCache[i]->GetActorLocation() + Direction);
if (EnemyHealthComponent->GetCurrentHealth() < Damage)
{
deadCheck = true;
}
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
EnemyHealthComponent->TakeDamage(OverlappedEnemiesCache[i], Damage, nullptr, ownerController, this);
}
if (deadCheck)
{
i -= 1;
}
}
} }
void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
@ -62,7 +28,15 @@ void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AAc
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{ {
OverlappedEnemies.Add(Enemy); FOverlappedEnemy overlappedEnemy = FOverlappedEnemy(Enemy);
GetWorldTimerManager().SetTimer(overlappedEnemy.OverlappedTimerHandle,
FTimerDelegate::CreateUObject(this, &AGarlicWeapon::GarlicFireWeaponAction,
overlappedEnemy),
WeaponCooldown,
true);
OverlappedEnemies.Add(overlappedEnemy);
} }
} }
@ -71,6 +45,42 @@ void AGarlicWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* Ot
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{ {
OverlappedEnemies.Remove(Enemy); for (int i = 0; i < OverlappedEnemies.Num(); i++)
{
if (Enemy == OverlappedEnemies[i].OverlappedEnemyCharacter)
{
GetWorldTimerManager().ClearTimer(OverlappedEnemies[i].OverlappedTimerHandle);
OverlappedEnemies.RemoveAt(i);
return;
}
}
}
}
void AGarlicWeapon::GarlicFireWeaponAction(FOverlappedEnemy EnemyCharacter)
{
UHealthComponent* EnemyHealthComponent = EnemyCharacter.OverlappedEnemyCharacter->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
if (!EnemyHealthComponent->GetIsDead())
{
FVector Direction = EnemyCharacter.OverlappedEnemyCharacter->GetActorLocation() - this->
GetActorLocation();
Direction.Normalize();
float distance = SphereComponent->GetScaledSphereRadius();
Direction *= distance;
EnemyCharacter.OverlappedEnemyCharacter->SetActorLocation(
EnemyCharacter.OverlappedEnemyCharacter->GetActorLocation() + Direction);
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
EnemyHealthComponent->TakeDamage(EnemyCharacter.OverlappedEnemyCharacter, Damage, nullptr,
ownerController, this);
}
} }
} }

View File

@ -8,6 +8,16 @@
#include "vampires/EnemyCharacter.h" #include "vampires/EnemyCharacter.h"
#include "GarlicWeapon.generated.h" #include "GarlicWeapon.generated.h"
USTRUCT()
struct FOverlappedEnemy
{
GENERATED_BODY()
AEnemyCharacter* OverlappedEnemyCharacter;
FTimerHandle OverlappedTimerHandle;
};
/** /**
* *
*/ */
@ -18,7 +28,7 @@ class VAMPIRES_API AGarlicWeapon : public AWeapon
USphereComponent* SphereComponent; USphereComponent* SphereComponent;
TArray<AEnemyCharacter*> OverlappedEnemies; TArray<FOverlappedEnemy> OverlappedEnemies;
public: public:
AGarlicWeapon(); AGarlicWeapon();
@ -29,6 +39,9 @@ protected:
public: public:
virtual void FireWeaponAction_Implementation() override; virtual void FireWeaponAction_Implementation() override;
UFUNCTION()
void GarlicFireWeaponAction(FOverlappedEnemy EnemyCharacter);
protected: protected:
UFUNCTION() UFUNCTION()
void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,