From 09d795600cc70cbdb6944f07c308226f9f233334 Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 18 Nov 2024 02:37:53 +0000 Subject: [PATCH] Projectile Weapon OnBeginOverlap refactor --- Source/vampires/Projectile.cpp | 34 +++++++++++++++++--- Source/vampires/Projectile.h | 5 +++ Source/vampires/Weapons/ProjectileWeapon.cpp | 23 ------------- Source/vampires/Weapons/ProjectileWeapon.h | 17 ++++------ 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index 9e12c96..15dc3ba 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -3,19 +3,21 @@ #include "Projectile.h" +#include "EnemyCharacter.h" #include "Components/SphereComponent.h" #include "GameFramework/ProjectileMovementComponent.h" #include "Weapons/ProjectileWeapon.h" // Sets default values AProjectile::AProjectile() -{ SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); +{ + SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); SetRootComponent(SphereComponent); SphereComponent->SetSphereRadius(50.0f); ProjectileMovement = CreateDefaultSubobject(TEXT("Projectile Movement")); ProjectileMovement->ProjectileGravityScale = 0.0f; - ProjectileMovement->Friction = 0.0f;; + ProjectileMovement->Friction = 0.0f; ProjectileMovement->bIsSliding = true; } @@ -23,9 +25,8 @@ AProjectile::AProjectile() void AProjectile::BeginPlay() { Super::BeginPlay(); - AProjectileWeapon* OwnerWeapon = Cast(GetOwner()); - SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap); - + + SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AProjectile::OnProjectileBeginOverlap); ProjectileMovement->InitialSpeed = ProjectileSpeed; ProjectileMovement->MaxSpeed = ProjectileSpeed; } @@ -36,3 +37,26 @@ void AProjectile::SetTargetDirection(FVector direction) ProjectileMovement->SetVelocityInLocalSpace(TargetDirection * ProjectileSpeed); } +void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult) +{ + if (AEnemyCharacter* Enemy = Cast(OtherActor)) + { + UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent(); + + if (!EnemyHealthComponent->GetIsDead()) + { + AController* ownerController = nullptr; + if (AVampireCharacter* character = Cast(GetOwner())) + { + ownerController = character->GetController(); + } + + AProjectileWeapon* ownerWeapon = Cast(GetOwner()); + EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); + + OverlappedComponent->GetAttachmentRootActor()->Destroy(); + } + } +} diff --git a/Source/vampires/Projectile.h b/Source/vampires/Projectile.h index 5165f10..7699976 100644 --- a/Source/vampires/Projectile.h +++ b/Source/vampires/Projectile.h @@ -37,4 +37,9 @@ protected: public: void SetTargetDirection(FVector direction); + +private: + UFUNCTION() + void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); }; diff --git a/Source/vampires/Weapons/ProjectileWeapon.cpp b/Source/vampires/Weapons/ProjectileWeapon.cpp index b38ccb4..2a2485d 100644 --- a/Source/vampires/Weapons/ProjectileWeapon.cpp +++ b/Source/vampires/Weapons/ProjectileWeapon.cpp @@ -62,26 +62,3 @@ void AProjectileWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, OverlappedEnemies.Remove(Enemy); } } - -void AProjectileWeapon::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, - const FHitResult& SweepResult) -{ - if (AEnemyCharacter* Enemy = Cast(OtherActor)) - { - UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent(); - - if (!EnemyHealthComponent->GetIsDead()) - { - AController* ownerController = nullptr; - if (AVampireCharacter* character = Cast(GetOwner())) - { - ownerController = character->GetController(); - } - - EnemyHealthComponent->TakeDamage(Enemy, Damage, nullptr, ownerController, this); - - OverlappedComponent->GetAttachmentRootActor()->Destroy(); - } - } -} diff --git a/Source/vampires/Weapons/ProjectileWeapon.h b/Source/vampires/Weapons/ProjectileWeapon.h index 9af30b6..4ded999 100644 --- a/Source/vampires/Weapons/ProjectileWeapon.h +++ b/Source/vampires/Weapons/ProjectileWeapon.h @@ -16,6 +16,7 @@ UCLASS() class VAMPIRES_API AProjectileWeapon : public AWeapon { GENERATED_BODY() + public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UBoxComponent* BoxComponent = nullptr; @@ -25,24 +26,20 @@ public: protected: TArray OverlappedEnemies = TArray(); - + public: AProjectileWeapon(); - + protected: virtual void BeginPlay() override; - + public: UFUNCTION() void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, - const FHitResult& SweepResult); + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult); UFUNCTION() void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, - int32 OtherBodyIndex); - - UFUNCTION() - void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult); + int32 OtherBodyIndex); };