diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index 1a1bc56..42c7546 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -3,8 +3,7 @@ #include "Projectile.h" -#include "EnemyCharacter.h" -#include "Weapons/MagicWandWeapon.h" +#include "Weapons/ProjectileWeapon.h" // Sets default values AProjectile::AProjectile() @@ -13,7 +12,7 @@ AProjectile::AProjectile() PrimaryActorTick.bCanEverTick = true; SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); - SphereComponent->SetupAttachment(RootComponent); + SetRootComponent(SphereComponent); SphereComponent->SetSphereRadius(50.0f); } @@ -21,8 +20,8 @@ AProjectile::AProjectile() void AProjectile::BeginPlay() { Super::BeginPlay(); - AMagicWandWeapon* OwnerWeapon = Cast(GetOwner()); - SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AMagicWandWeapon::OnProjectileBeginOverlap); + AProjectileWeapon* OwnerWeapon = Cast(GetOwner()); + SphereComponent->OnComponentBeginOverlap.AddDynamic(OwnerWeapon, &AProjectileWeapon::OnProjectileBeginOverlap); } // Called every frame diff --git a/Source/vampires/Weapons/MagicWandWeapon.cpp b/Source/vampires/Weapons/MagicWandWeapon.cpp index 6a1d6bb..d5ff066 100644 --- a/Source/vampires/Weapons/MagicWandWeapon.cpp +++ b/Source/vampires/Weapons/MagicWandWeapon.cpp @@ -5,20 +5,14 @@ #include "Kismet/GameplayStatics.h" #include "Kismet/KismetMathLibrary.h" -#include "vampires/EnemyCharacter.h" AMagicWandWeapon::AMagicWandWeapon() { - SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); - SphereComponent->SetupAttachment(RootComponent); - SphereComponent->SetSphereRadius(1000.0f); } void AMagicWandWeapon::BeginPlay() { Super::BeginPlay(); - SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponBeginOverlap); - SphereComponent->OnComponentEndOverlap.AddDynamic(this, &AMagicWandWeapon::OnWeaponEndOverlap); } void AMagicWandWeapon::FireWeaponAction_Implementation() @@ -36,8 +30,8 @@ void AMagicWandWeapon::FireWeaponAction_Implementation() actorSpawnParameters.Owner = this; actorSpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; actorSpawnParameters.TransformScaleMethod = ESpawnActorScaleMethod::MultiplyWithRoot; - - AProjectile* projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetTransform(), + + AProjectile* projectile = GetWorld()->SpawnActor(ProjectileTemplate, GetOwner()->GetTransform(), actorSpawnParameters); FVector direction = UKismetMathLibrary::GetDirectionUnitVector( 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(OtherActor)) - { - OverlappedEnemies.Add(Enemy); - } -} - -void AMagicWandWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) -{ - if (AEnemyCharacter* Enemy = Cast(OtherActor)) - { - OverlappedEnemies.Remove(Enemy); - } -} - -void AMagicWandWeapon::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/MagicWandWeapon.h b/Source/vampires/Weapons/MagicWandWeapon.h index 03845e5..b0aebe6 100644 --- a/Source/vampires/Weapons/MagicWandWeapon.h +++ b/Source/vampires/Weapons/MagicWandWeapon.h @@ -3,29 +3,17 @@ #pragma once #include "CoreMinimal.h" -#include "../Weapon.h" -#include "Components/SphereComponent.h" -#include "vampires/Projectile.h" +#include "ProjectileWeapon.h" #include "MagicWandWeapon.generated.h" /** * */ UCLASS() -class VAMPIRES_API AMagicWandWeapon : public AWeapon +class VAMPIRES_API AMagicWandWeapon : public AProjectileWeapon { GENERATED_BODY() -public: - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - USphereComponent* SphereComponent = nullptr; - - UPROPERTY(EditAnywhere, Category = "Weapon Properties") - TSubclassOf ProjectileTemplate = nullptr; - -private: - TArray OverlappedEnemies = TArray(); - public: AMagicWandWeapon(); @@ -34,17 +22,4 @@ protected: public: 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); }; diff --git a/Source/vampires/Weapons/ProjectileWeapon.cpp b/Source/vampires/Weapons/ProjectileWeapon.cpp new file mode 100644 index 0000000..7d48fff --- /dev/null +++ b/Source/vampires/Weapons/ProjectileWeapon.cpp @@ -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(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(OtherActor)) + { + OverlappedEnemies.Add(Enemy); + } +} + +void AProjectileWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) +{ + if (AEnemyCharacter* Enemy = Cast(OtherActor)) + { + 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 new file mode 100644 index 0000000..d2ecf03 --- /dev/null +++ b/Source/vampires/Weapons/ProjectileWeapon.h @@ -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 ProjectileTemplate = nullptr; + +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); + + 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); +};