Projectile Weapon OnBeginOverlap refactor

This commit is contained in:
baz 2024-11-18 02:37:53 +00:00
parent e55d62ff05
commit 09d795600c
4 changed files with 41 additions and 38 deletions

View File

@ -3,19 +3,21 @@
#include "Projectile.h" #include "Projectile.h"
#include "EnemyCharacter.h"
#include "Components/SphereComponent.h" #include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h" #include "GameFramework/ProjectileMovementComponent.h"
#include "Weapons/ProjectileWeapon.h" #include "Weapons/ProjectileWeapon.h"
// Sets default values // Sets default values
AProjectile::AProjectile() AProjectile::AProjectile()
{ SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component")); {
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SetRootComponent(SphereComponent); SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(50.0f); SphereComponent->SetSphereRadius(50.0f);
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement")); ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
ProjectileMovement->ProjectileGravityScale = 0.0f; ProjectileMovement->ProjectileGravityScale = 0.0f;
ProjectileMovement->Friction = 0.0f;; ProjectileMovement->Friction = 0.0f;
ProjectileMovement->bIsSliding = true; ProjectileMovement->bIsSliding = true;
} }
@ -23,9 +25,8 @@ AProjectile::AProjectile()
void AProjectile::BeginPlay() void AProjectile::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
AProjectileWeapon* OwnerWeapon = Cast<AProjectileWeapon>(GetOwner());
SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap);
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AProjectile::OnProjectileBeginOverlap);
ProjectileMovement->InitialSpeed = ProjectileSpeed; ProjectileMovement->InitialSpeed = ProjectileSpeed;
ProjectileMovement->MaxSpeed = ProjectileSpeed; ProjectileMovement->MaxSpeed = ProjectileSpeed;
} }
@ -36,3 +37,26 @@ void AProjectile::SetTargetDirection(FVector direction)
ProjectileMovement->SetVelocityInLocalSpace(TargetDirection * ProjectileSpeed); ProjectileMovement->SetVelocityInLocalSpace(TargetDirection * ProjectileSpeed);
} }
void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
AProjectileWeapon* ownerWeapon = Cast<AProjectileWeapon>(GetOwner());
EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this);
OverlappedComponent->GetAttachmentRootActor()->Destroy();
}
}
}

View File

@ -37,4 +37,9 @@ protected:
public: public:
void SetTargetDirection(FVector direction); void SetTargetDirection(FVector direction);
private:
UFUNCTION()
void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
}; };

View File

@ -62,26 +62,3 @@ void AProjectileWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp,
OverlappedEnemies.Remove(Enemy); OverlappedEnemies.Remove(Enemy);
} }
} }
void AProjectileWeapon::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
EnemyHealthComponent->TakeDamage(Enemy, Damage, nullptr, ownerController, this);
OverlappedComponent->GetAttachmentRootActor()->Destroy();
}
}
}

View File

@ -16,6 +16,7 @@ UCLASS()
class VAMPIRES_API AProjectileWeapon : public AWeapon class VAMPIRES_API AProjectileWeapon : public AWeapon
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UBoxComponent* BoxComponent = nullptr; UBoxComponent* BoxComponent = nullptr;
@ -41,8 +42,4 @@ public:
UFUNCTION() UFUNCTION()
void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex); int32 OtherBodyIndex);
UFUNCTION()
void OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
}; };