From fc93e35ef18b07650e867be807c7f67627e632b5 Mon Sep 17 00:00:00 2001 From: baz Date: Thu, 5 Sep 2024 00:36:44 +0100 Subject: [PATCH] Change GarlicWeapon to use a timer per OverlappedEnemy --- Content/Player/BP_PlayerCharacter.uasset | 4 +- Source/vampires/Weapons/GarlicWeapon.cpp | 82 +++++++++++++----------- Source/vampires/Weapons/GarlicWeapon.h | 15 ++++- 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index 02ee5f8..168ce0d 100644 --- a/Content/Player/BP_PlayerCharacter.uasset +++ b/Content/Player/BP_PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:852c60f9f125e964733cc9fe6234325e10ce7ea36fab369b504f6888bf0d4bb9 -size 52469 +oid sha256:d6b3052c9ed4804cdaeae311229341b50b1512193b324a34d324ac0e7b960cc6 +size 53705 diff --git a/Source/vampires/Weapons/GarlicWeapon.cpp b/Source/vampires/Weapons/GarlicWeapon.cpp index 4aff802..5e36a59 100644 --- a/Source/vampires/Weapons/GarlicWeapon.cpp +++ b/Source/vampires/Weapons/GarlicWeapon.cpp @@ -20,40 +20,6 @@ void AGarlicWeapon::BeginPlay() void AGarlicWeapon::FireWeaponAction_Implementation() { - TArray 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(GetOwner())) - { - ownerController = character->GetController(); - } - - EnemyHealthComponent->TakeDamage(OverlappedEnemiesCache[i], Damage, nullptr, ownerController, this); - } - - if (deadCheck) - { - i -= 1; - } - } } void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, @@ -62,7 +28,15 @@ void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AAc { if (AEnemyCharacter* Enemy = Cast(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(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(GetOwner())) + { + ownerController = character->GetController(); + } + + EnemyHealthComponent->TakeDamage(EnemyCharacter.OverlappedEnemyCharacter, Damage, nullptr, + ownerController, this); + } } } diff --git a/Source/vampires/Weapons/GarlicWeapon.h b/Source/vampires/Weapons/GarlicWeapon.h index 4e9f0ca..b23b8b0 100644 --- a/Source/vampires/Weapons/GarlicWeapon.h +++ b/Source/vampires/Weapons/GarlicWeapon.h @@ -8,6 +8,16 @@ #include "vampires/EnemyCharacter.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; - TArray OverlappedEnemies; + TArray OverlappedEnemies; public: AGarlicWeapon(); @@ -29,6 +39,9 @@ protected: public: virtual void FireWeaponAction_Implementation() override; + UFUNCTION() + void GarlicFireWeaponAction(FOverlappedEnemy EnemyCharacter); + protected: UFUNCTION() void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,