Weapon Refactoring

This commit is contained in:
baz 2025-04-23 01:13:11 +01:00
parent 293d0ae8b2
commit af5c753c08
19 changed files with 136 additions and 209 deletions

BIN
Content/Levels/Level.umap (Stored with Git LFS)

Binary file not shown.

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,10 @@
#include "Weapon.h" #include "Weapon.h"
#include "EXPComponent.h" #include "EnemyCharacter.h"
#include "PlayerCharacter.h"
#include "VampirePlayerController.h"
#include "Components/BoxComponent.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
// Sets default values // Sets default values
@ -11,6 +14,10 @@ AWeapon::AWeapon()
{ {
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false; PrimaryActorTick.bCanEverTick = false;
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));
BoxComponent->SetupAttachment(RootComponent);
BoxComponent->SetCollisionProfileName(TEXT("Weapon"));
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned
@ -18,6 +25,13 @@ void AWeapon::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnWeaponBeginOverlap);
BoxComponent->OnComponentEndOverlap.AddDynamic(this, &AWeapon::OnWeaponEndOverlap);
FViewport::ViewportResizedEvent.AddUObject(this, &AWeapon::ResizeBoxComponent);
ResizeBoxComponent(GEngine->GameViewport->Viewport, -1);
GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true); GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true);
} }
@ -47,3 +61,45 @@ bool AWeapon::UpgradeWeapon_Implementation()
return false; return false;
} }
void AWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void AWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}
void AWeapon::ResizeBoxComponent(FViewport* Viewport, uint32 unused)
{
FVector TopLeft, TopLeftDir;
FVector TopRight, TopRightDir;
FVector BottomLeft, BottomLeftDir;
FVector BottomRight, BottomRightDir;
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
AVampirePlayerController* PlayerController = Cast<AVampirePlayerController>(
UGameplayStatics::GetPlayerController(PlayerCharacter, 0));
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, 0, TopRight, TopRightDir);
PlayerController->DeprojectScreenPositionToWorld(0, ViewportSize.Y, BottomLeft, BottomLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
float width = FVector::Dist(TopLeft, TopRight) * 120;
float height = FVector::Dist(TopLeft, BottomLeft) * 120;
BoxComponent->SetBoxExtent(FVector(height, width, 200.0f));
}

View File

@ -4,8 +4,10 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "UnrealClient.h"
#include "Weapon.generated.h" #include "Weapon.generated.h"
class UBoxComponent;
class UPaperSprite; class UPaperSprite;
class UWeaponDataAsset; class UWeaponDataAsset;
@ -41,9 +43,18 @@ public:
int CurrentLevel = 0; int CurrentLevel = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
bool FollowPlayer = true;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
int MaxLevel = 0; int MaxLevel = 0;
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UBoxComponent* BoxComponent = nullptr;
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
private: private:
FTimerHandle WeaponTimerHandle; FTimerHandle WeaponTimerHandle;
@ -66,4 +77,17 @@ public:
UFUNCTION(BlueprintNativeEvent) UFUNCTION(BlueprintNativeEvent)
bool UpgradeWeapon(); bool UpgradeWeapon();
virtual bool UpgradeWeapon_Implementation(); virtual bool UpgradeWeapon_Implementation();
UFUNCTION()
virtual void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
private:
void ResizeBoxComponent(FViewport* Viewport, uint32 unused);
}; };

View File

@ -39,7 +39,16 @@ void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon
SpawnParameters.Owner = GetOwner(); SpawnParameters.Owner = GetOwner();
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, GetOwner()->GetTransform(), SpawnParameters); AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, GetOwner()->GetTransform(), SpawnParameters);
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform);
if (weapon->FollowPlayer)
{
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform);
}
else
{
weapon->SetActorLocation(FVector::ZeroVector);
}
inventory.Add(weapon); inventory.Add(weapon);
} }

View File

@ -42,7 +42,7 @@ void AGarlicWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AAc
overlappedEnemy), overlappedEnemy),
WeaponCooldown, WeaponCooldown,
true); true);
OverlappedEnemies.Add(overlappedEnemy); GarlicOverlappedEnemies.Add(overlappedEnemy);
} }
} }
@ -51,12 +51,12 @@ void AGarlicWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* Ot
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{ {
for (int i = 0; i < OverlappedEnemies.Num(); i++) for (int i = 0; i < GarlicOverlappedEnemies.Num(); i++)
{ {
if (Enemy == OverlappedEnemies[i].OverlappedEnemyCharacter) if (Enemy == GarlicOverlappedEnemies[i].OverlappedEnemyCharacter)
{ {
GetWorldTimerManager().ClearTimer(OverlappedEnemies[i].OverlappedTimerHandle); GetWorldTimerManager().ClearTimer(GarlicOverlappedEnemies[i].OverlappedTimerHandle);
OverlappedEnemies.RemoveAt(i); GarlicOverlappedEnemies.RemoveAt(i);
return; return;
} }
} }

View File

@ -30,7 +30,7 @@ public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USphereComponent* SphereComponent; USphereComponent* SphereComponent;
TArray<FOverlappedEnemy> OverlappedEnemies; TArray<FOverlappedEnemy> GarlicOverlappedEnemies;
private: private:
float Range; float Range;

View File

@ -10,30 +10,23 @@
ALightningRingWeapon::ALightningRingWeapon() ALightningRingWeapon::ALightningRingWeapon()
{ {
SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Component"));
SphereComponent->SetupAttachment(RootComponent);
SphereComponent->SetSphereRadius(1000.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon"));
Damage = 51.0f; Damage = 51.0f;
} }
void ALightningRingWeapon::BeginPlay() void ALightningRingWeapon::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ALightningRingWeapon::OnBeginOverlap);
SphereComponent->OnComponentEndOverlap.AddDynamic(this, &ALightningRingWeapon::OnEndOverlap);
} }
void ALightningRingWeapon::FireWeaponAction_Implementation() void ALightningRingWeapon::FireWeaponAction_Implementation()
{ {
Super::FireWeaponAction_Implementation(); Super::FireWeaponAction_Implementation();
TArray<AEnemyCharacter*> targetableEnemies = OverlappedEnemies; TArray<AActor*> targetableEnemies = OverlappedEnemies;
for (int i = 0; i < LightningBolts && targetableEnemies.Num() > 0; i++) for (int i = 0; i < LightningBolts && targetableEnemies.Num() > 0; i++)
{ {
AEnemyCharacter* target = targetableEnemies[FMath::RandRange(0, targetableEnemies.Num() - 1)]; AActor* target = targetableEnemies[FMath::RandRange(0, targetableEnemies.Num() - 1)];
TArray<TEnumAsByte<EObjectTypeQuery>> traceObjectTypes; TArray<TEnumAsByte<EObjectTypeQuery>> traceObjectTypes;
traceObjectTypes.Add(UEngineTypes::ConvertToObjectType(ECC_Pawn)); traceObjectTypes.Add(UEngineTypes::ConvertToObjectType(ECC_Pawn));
@ -95,21 +88,3 @@ bool ALightningRingWeapon::UpgradeWeapon_Implementation()
return true; return true;
} }
void ALightningRingWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
}
void ALightningRingWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
}

View File

@ -16,10 +16,6 @@ class VAMPIRES_API ALightningRingWeapon : public AWeapon
{ {
GENERATED_BODY() GENERATED_BODY()
USphereComponent* SphereComponent;
TArray<AEnemyCharacter*> OverlappedEnemies;
public: public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
@ -38,14 +34,4 @@ public:
virtual void FireWeaponAction_Implementation() override; virtual void FireWeaponAction_Implementation() override;
virtual bool UpgradeWeapon_Implementation() override; virtual bool UpgradeWeapon_Implementation() override;
protected:
UFUNCTION()
void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex);
}; };

View File

@ -19,34 +19,6 @@ APentagramWeapon::APentagramWeapon()
WeaponCooldown = 90.0f; WeaponCooldown = 90.0f;
} }
void APentagramWeapon::BeginPlay()
{
Super::BeginPlay();
BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &APentagramWeapon::OnBeginOverlap);
BoxComponent->OnComponentEndOverlap.AddDynamic(this, &APentagramWeapon::OnEndOverlap);
FVector TopLeft, TopLeftDir;
FVector TopRight, TopRightDir;
FVector BottomLeft, BottomLeftDir;
FVector BottomRight, BottomRightDir;
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
AVampirePlayerController* PlayerController = Cast<AVampirePlayerController>(
UGameplayStatics::GetPlayerController(PlayerCharacter, 0));
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, 0, TopRight, TopRightDir);
PlayerController->DeprojectScreenPositionToWorld(0, ViewportSize.Y, BottomLeft, BottomLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
float width = FVector::Dist(TopLeft, TopRight) * 60;
float height = FVector::Dist(TopLeft, BottomLeft) * 60;
BoxComponent->SetBoxExtent(FVector(height, width, 200.0f));
}
void APentagramWeapon::FireWeaponAction_Implementation() void APentagramWeapon::FireWeaponAction_Implementation()
{ {
Super::FireWeaponAction_Implementation(); Super::FireWeaponAction_Implementation();
@ -98,29 +70,28 @@ bool APentagramWeapon::UpgradeWeapon_Implementation()
return true; return true;
} }
void APentagramWeapon::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void APentagramWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
const FHitResult& SweepResult)
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (APickup* Pickup = Cast<APickup>(OtherActor))
{
OverlappedEnemies.Add(Enemy);
}
else if (APickup* Pickup = Cast<APickup>(OtherActor))
{ {
OverlappedPickups.Add(Pickup); OverlappedPickups.Add(Pickup);
} }
else
{
Super::OnWeaponBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
}
} }
void APentagramWeapon::OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, void APentagramWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (APickup* Pickup = Cast<APickup>(OtherActor))
{
OverlappedEnemies.Remove(Enemy);
}
else if (APickup* Pickup = Cast<APickup>(OtherActor))
{ {
OverlappedPickups.Remove(Pickup); OverlappedPickups.Remove(Pickup);
} }
else
{
Super::OnWeaponEndOverlap(OverlappedComp, OtherActor, OtherComp, OtherBodyIndex);
}
} }

View File

@ -17,33 +17,19 @@ class VAMPIRES_API APentagramWeapon : public AWeapon
{ {
GENERATED_BODY() GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UBoxComponent* BoxComponent = nullptr;
private: private:
TArray<AEnemyCharacter*> OverlappedEnemies = TArray<AEnemyCharacter*>();
TArray<APickup*> OverlappedPickups = TArray<APickup*>(); TArray<APickup*> OverlappedPickups = TArray<APickup*>();
public: public:
APentagramWeapon(); APentagramWeapon();
protected:
virtual void BeginPlay() override;
public:
virtual void FireWeaponAction_Implementation() override; virtual void FireWeaponAction_Implementation() override;
bool UpgradeWeapon_Implementation() override; bool UpgradeWeapon_Implementation() override;
protected: virtual void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UFUNCTION() UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION() virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
void OnEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;
int32 OtherBodyIndex);
}; };

View File

@ -3,66 +3,7 @@
#include "ProjectileWeapon.h" #include "ProjectileWeapon.h"
#include "Kismet/GameplayStatics.h"
#include "vampires/EnemyCharacter.h"
#include "vampires/PlayerCharacter.h"
#include "vampires/VampirePlayerController.h"
#include "../Weapon.h" #include "../Weapon.h"
#include "Components/BoxComponent.h"
AProjectileWeapon::AProjectileWeapon()
{
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));
BoxComponent->SetupAttachment(RootComponent);
BoxComponent->SetCollisionProfileName(TEXT("Weapon"));
}
void AProjectileWeapon::BeginPlay()
{
Super::BeginPlay();
BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AProjectileWeapon::OnWeaponBeginOverlap);
BoxComponent->OnComponentEndOverlap.AddDynamic(this, &AProjectileWeapon::OnWeaponEndOverlap);
FVector TopLeft, TopLeftDir;
FVector TopRight, TopRightDir;
FVector BottomLeft, BottomLeftDir;
FVector BottomRight, BottomRightDir;
FVector2d ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
AVampirePlayerController* PlayerController = Cast<AVampirePlayerController>(
UGameplayStatics::GetPlayerController(PlayerCharacter, 0));
PlayerController->DeprojectScreenPositionToWorld(0, 0, TopLeft, TopLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, 0, TopRight, TopRightDir);
PlayerController->DeprojectScreenPositionToWorld(0, ViewportSize.Y, BottomLeft, BottomLeftDir);
PlayerController->DeprojectScreenPositionToWorld(ViewportSize.X, ViewportSize.Y, BottomRight, BottomRightDir);
float width = FVector::Dist(TopLeft, TopRight) * 60;
float height = FVector::Dist(TopLeft, BottomLeft) * 60;
BoxComponent->SetBoxExtent(FVector(height, width, 200.0f));
}
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::FireWeaponAction_Implementation() void AProjectileWeapon::FireWeaponAction_Implementation()
{ {

View File

@ -8,7 +8,6 @@
class UProjectileDataAsset; class UProjectileDataAsset;
class AProjectile; class AProjectile;
class UBoxComponent;
/** /**
* *
@ -19,9 +18,6 @@ class VAMPIRES_API AProjectileWeapon : public AWeapon
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
UBoxComponent* BoxComponent = nullptr;
UPROPERTY(EditAnywhere, Category = "Weapon Properties") UPROPERTY(EditAnywhere, Category = "Weapon Properties")
TObjectPtr<UProjectileDataAsset> ProjectileTemplate = nullptr; TObjectPtr<UProjectileDataAsset> ProjectileTemplate = nullptr;
@ -32,29 +28,12 @@ public:
float ProjectileSpawningDelay = 0.25f; float ProjectileSpawningDelay = 0.25f;
protected: protected:
TArray<AActor*> OverlappedEnemies = TArray<AActor*>();
FTimerHandle FireProjectileTimerHandler; FTimerHandle FireProjectileTimerHandler;
private: private:
int remainingProjectilesToSpawn = 0; int remainingProjectilesToSpawn = 0;
public: 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);
virtual void FireWeaponAction_Implementation() override; virtual void FireWeaponAction_Implementation() override;
protected: protected: