Create Projectile Weapon class

This commit is contained in:
baz 2024-07-31 14:19:50 +01:00
parent 3f089caa33
commit b054db1478
5 changed files with 114 additions and 82 deletions

View File

@ -3,8 +3,7 @@
#include "Projectile.h" #include "Projectile.h"
#include "EnemyCharacter.h" #include "Weapons/ProjectileWeapon.h"
#include "Weapons/MagicWandWeapon.h"
// Sets default values // Sets default values
AProjectile::AProjectile() AProjectile::AProjectile()
@ -13,7 +12,7 @@ AProjectile::AProjectile()
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component")); SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent); SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(50.0f); SphereComponent->SetSphereRadius(50.0f);
} }
@ -21,8 +20,8 @@ AProjectile::AProjectile()
void AProjectile::BeginPlay() void AProjectile::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
AMagicWandWeapon* OwnerWeapon = Cast<AMagicWandWeapon>(GetOwner()); AProjectileWeapon* OwnerWeapon = Cast<AProjectileWeapon>(GetOwner());
SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AMagicWandWeapon::OnProjectileBeginOverlap); SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap);
} }
// Called every frame // Called every frame

View File

@ -5,20 +5,14 @@
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h" #include "Kismet/KismetMathLibrary.h"
#include "vampires/EnemyCharacter.h"
AMagicWandWeapon::AMagicWandWeapon() AMagicWandWeapon::AMagicWandWeapon()
{ {
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(1000.0f);
} }
void AMagicWandWeapon::BeginPlay() void AMagicWandWeapon::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponEndOverlap);
} }
void AMagicWandWeapon::FireWeaponAction_Implementation() void AMagicWandWeapon::FireWeaponAction_Implementation()
@ -36,8 +30,8 @@ void AMagicWandWeapon::FireWeaponAction_Implementation()
actorSpawnParameters.Owner = this; actorSpawnParameters.Owner = this;
actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot; actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot;
AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetTransform(), AProjectile* projectile = GetWorld()->SpawnActor<AProjectile>(ProjectileTemplate, GetOwner()->GetTransform(),
actorSpawnParameters); actorSpawnParameters);
FVector direction = UKismetMathLibrary::GetDirectionUnitVector( FVector direction = UKismetMathLibrary::GetDirectionUnitVector(
GetActorLocation(), nearestActor->GetActorLocation()); GetActorLocation(), nearestActor->GetActorLocation());
@ -47,45 +41,3 @@ void AMagicWandWeapon::FireWeaponAction_Implementation()
} }
} }
} }
void AMagicWandWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void AMagicWandWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}
void AMagicWandWeapon::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

@ -3,29 +3,17 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "../Weapon.h" #include "ProjectileWeapon.h"
#include "Components/SphereComponent.h"
#include "vampires/Projectile.h"
#include "MagicWandWeapon.generated.h" #include "MagicWandWeapon.generated.h"
/** /**
* *
*/ */
UCLASS() UCLASS()
class VAMPIRES_API AMagicWandWeapon : public AWeapon class VAMPIRES_API AMagicWandWeapon : public AProjectileWeapon
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr;
UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TSubclassOf<AProjectile> ProjectileTemplate = nullptr;
private:
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
public: public:
AMagicWandWeapon(); AMagicWandWeapon();
@ -34,17 +22,4 @@ protected:
public: public:
virtual void FireWeaponAction_Implementation() override; virtual void FireWeaponAction_Implementation() override;
UFUNCTION()
void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
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);
}; };

View File

@ -0,0 +1,60 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "ProjectileWeapon.h"
#include "vampires/EnemyCharacter.h"
AProjectileWeapon::AProjectileWeapon()
{
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(1000.0f);
}
void AProjectileWeapon::BeginPlay()
{
Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AProjectileWeapon::OnWeaponBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &AProjectileWeapon::OnWeaponEndOverlap);
}
void AProjectileWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void AProjectileWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
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

@ -0,0 +1,46 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "../Weapon.h"
#include "vampires/Projectile.h"
#include "ProjectileWeapon.generated.h"
/**
*
*/
UCLASS()
class VAMPIRES_API AProjectileWeapon : public AWeapon
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
USphereComponent* SphereComponent = nullptr;
UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TSubclassOf<AProjectile> ProjectileTemplate = nullptr;
protected:
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
public:
AProjectileWeapon();
protected:
virtual void BeginPlay() override;
public:
UFUNCTION()
void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
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);
};