Minor refactor to base weapon class

This commit is contained in:
baz 2025-07-25 23:03:58 +01:00
parent 274ae9fc27
commit e0dd825be9
12 changed files with 92 additions and 83 deletions

View File

@ -110,7 +110,7 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon
} }
AProjectileWeapon* ownerWeapon = Cast<AProjectileWeapon>(GetOwner()); AProjectileWeapon* ownerWeapon = Cast<AProjectileWeapon>(GetOwner());
EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->GetDamage(), nullptr, ownerController, this);
RemainingDamagableEnemies--; RemainingDamagableEnemies--;

View File

@ -64,7 +64,8 @@ bool AWeapon::UpgradeWeapon_Implementation()
} }
void AWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, void AWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult)
{ {
if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor)) if (AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(OtherActor))
{ {
@ -81,10 +82,12 @@ void AWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* Ot
} }
} }
void AWeapon::ResizeBoxComponent(FViewport* Viewport, uint32 unused) void AWeapon::ResizeBoxComponent(FViewport* Viewport, const uint32 Unused)
{ {
if (!GEngine->GameViewport)
if (!GEngine->GameViewport) return; {
return;
}
FVector TopLeft, TopLeftDir; FVector TopLeft, TopLeftDir;
FVector TopRight, TopRightDir; FVector TopRight, TopRightDir;
@ -108,7 +111,7 @@ void AWeapon::ResizeBoxComponent(FViewport* Viewport, uint32 unused)
// I am using the unused flag to work around a bug where the DeprojectScreenPositionToWorld doesn't match the // I am using the unused flag to work around a bug where the DeprojectScreenPositionToWorld doesn't match the
// values that I am expecting, in that they are way too small, for any other resize event we can skip this nonsense. // values that I am expecting, in that they are way too small, for any other resize event we can skip this nonsense.
if (unused == -2) if (Unused == -2)
{ {
width *= 266.666; width *= 266.666;
height *= 266.666; height *= 266.666;

View File

@ -16,78 +16,86 @@ class VAMPIRES_API AWeapon : public AActor
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information")
FText Name; FText Name;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information")
FText Description; FText Description;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information")
TObjectPtr<UTexture2D> Icon; TObjectPtr<UTexture2D> Icon;
UPROPERTY(EditDefaultsOnly, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, Category = "Weapon | Properties")
TObjectPtr<USoundBase> WeaponActivatedSoundBase = nullptr; TObjectPtr<USoundBase> WeaponActivatedSoundBase = nullptr;
UPROPERTY(EditDefaultsOnly, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties")
TObjectPtr<UPaperSprite> WeaponSprite = nullptr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
float WeaponCooldown = 1.0f; float WeaponCooldown = 1.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties")
float Damage; float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties")
TArray<FText> UpgradeDescriptions;
int CurrentLevel = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
bool FollowPlayer = true; bool FollowPlayer = true;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Upgrades")
TArray<FText> UpgradeDescriptions;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon | Upgrades")
int MaxLevel = 0; int MaxLevel = 0;
protected: UPROPERTY()
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TObjectPtr<UBoxComponent> BoxComponent = nullptr; TObjectPtr<UBoxComponent> BoxComponent = nullptr;
TArray<TObjectPtr<AActor>> OverlappedEnemies = TArray<TObjectPtr<AActor>>(); TArray<TObjectPtr<AActor>> OverlappedEnemies = TArray<TObjectPtr<AActor>>();
int CurrentLevel = 0;
private: private:
FTimerHandle WeaponTimerHandle; FTimerHandle WeaponTimerHandle;
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's
AWeapon(); AWeapon();
UFUNCTION(BlueprintNativeEvent)
bool UpgradeWeapon();
virtual bool UpgradeWeapon_Implementation();
FText GetWeaponName() const { return Name; }
FText GetDescription() const { return Description; }
TObjectPtr<UTexture2D> GetIcon() const { return Icon; }
float GetDamage() const { return Damage; }
bool GetFollowPlayer() const { return FollowPlayer; }
TArray<FText> GetUpgradeDescriptions() const { return UpgradeDescriptions; }
int GetWeaponLevel() const { return CurrentLevel; }
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
void ResetWeaponTimer(); void ResetWeaponTimer();
public:
UFUNCTION(BlueprintNativeEvent) UFUNCTION(BlueprintNativeEvent)
void FireWeaponAction(); void FireWeaponAction();
virtual void FireWeaponAction_Implementation(); virtual void FireWeaponAction_Implementation();
UFUNCTION(BlueprintNativeEvent)
bool UpgradeWeapon();
virtual bool UpgradeWeapon_Implementation();
UFUNCTION() UFUNCTION()
virtual void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, virtual void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult); const FHitResult& SweepResult);
UFUNCTION() UFUNCTION()
virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex); int32 OtherBodyIndex);
private: private:
void ResizeBoxComponent(FViewport* Viewport, uint32 Unused);
void ResizeBoxComponent(FViewport* Viewport, uint32 unused);
}; };

View File

@ -42,7 +42,7 @@ void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon
SpawnParameters.Owner = GetOwner(); SpawnParameters.Owner = GetOwner();
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters.Owner->GetTransform(), SpawnParameters); AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParameters.Owner->GetTransform(), SpawnParameters);
if (weapon->FollowPlayer) if (weapon->GetFollowPlayer())
{ {
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepWorldTransform); weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepWorldTransform);
} }

View File

@ -28,12 +28,12 @@ class VAMPIRES_API AGarlicWeapon : public AWeapon
{ {
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Weapon | Garlic")
TObjectPtr<USphereComponent> SphereComponent; TObjectPtr<USphereComponent> SphereComponent;
TArray<FOverlappedEnemy> GarlicOverlappedEnemies; TArray<FOverlappedEnemy> GarlicOverlappedEnemies;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Weapon | Garlic")
TObjectPtr<UStaticMeshComponent> VisualEffectMeshComponent; TObjectPtr<UStaticMeshComponent> VisualEffectMeshComponent;
private: private:

View File

@ -18,13 +18,13 @@ class VAMPIRES_API ALightningRingWeapon : public AWeapon
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring")
int LightningBolts = 1; int LightningBolts = 1;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring")
float LightingBoltRadius = 200.0f; float LightingBoltRadius = 200.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring")
TObjectPtr<UNiagaraSystem> LightningEffectSystem; TObjectPtr<UNiagaraSystem> LightningEffectSystem;
public: public:

View File

@ -18,13 +18,13 @@ class VAMPIRES_API AProjectileWeapon : public AWeapon
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditAnywhere, Category = "Weapon Properties") UPROPERTY(EditAnywhere, Category = "Weapon | Projectiles")
TObjectPtr<UProjectileDataAsset> ProjectileTemplate = nullptr; TObjectPtr<UProjectileDataAsset> ProjectileTemplate = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Projectiles")
int ProjectilesPerActivation = 1; int ProjectilesPerActivation = 1;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Projectiles")
float ProjectileSpawningDelay = 0.25f; float ProjectileSpawningDelay = 0.25f;
protected: protected:

View File

@ -52,7 +52,7 @@ void ASwarmAgent::OnSwarmAgentBeginOverlap(UPrimitiveComponent* OverlappedCompon
if (AVampireCharacter* character = Cast<AVampireCharacter>(ownerWeapon->GetOwner())) if (AVampireCharacter* character = Cast<AVampireCharacter>(ownerWeapon->GetOwner()))
{ {
AController* ownerController = character->GetController(); AController* ownerController = character->GetController();
EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->GetDamage(), nullptr, ownerController, this);
} }
} }
} }

View File

@ -15,19 +15,19 @@ class VAMPIRES_API ASwarmWeapon : public AWeapon
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Timeline") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon | Swarm")
TObjectPtr<UTimelineComponent> TimelineComponent = nullptr; TObjectPtr<UTimelineComponent> TimelineComponent = nullptr;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Swarm")
TObjectPtr<UCurveFloat> SwarmCurve; TObjectPtr<UCurveFloat> SwarmCurve;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Swarm")
float TimelinePlayRate = 1; float TimelinePlayRate = 1;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Swarm")
TSubclassOf<ASwarmAgent> SwarmActor; TSubclassOf<ASwarmAgent> SwarmActor;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Swarm")
float Distance = 250.0f; float Distance = 250.0f;
private: private:

View File

@ -43,7 +43,7 @@ void ULevelUpWidget::NativeConstruct()
TArray<UUpgradeButtonDataObject*> upgradeItems; TArray<UUpgradeButtonDataObject*> upgradeItems;
for (AWeapon* weapon : Inventory) for (AWeapon* weapon : Inventory)
{ {
if (weapon->CurrentLevel < weapon->UpgradeDescriptions.Num()) if (weapon->GetWeaponLevel() < weapon->GetUpgradeDescriptions().Num())
{ {
UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this); UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this);
Temp->SetData(weapon, this); Temp->SetData(weapon, this);

View File

@ -7,23 +7,22 @@
void UStarterWeaponButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) void UStarterWeaponButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent)
{ {
WeaponName = Weapon->Name; WeaponName = Weapon->GetWeaponName();
Description = Weapon->UpgradeDescriptions[Weapon->CurrentLevel]; WeaponIcon = Weapon->GetIcon();
WeaponIcon = Weapon->Icon;
WeaponInstance = Weapon; WeaponInstance = Weapon;
Parent = parent; Parent = parent;
if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel())
{
Description = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()];
}
} }
void UStarterWeaponButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent) void UStarterWeaponButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent)
{ {
AWeapon* temp = NewObject<AWeapon>(this, Weapon); if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon))
if (temp)
{ {
WeaponName = temp->Name; SetData(tempWeapon, parent);
Description = temp->Description;
WeaponIcon = temp->Icon;
WeaponTemplate = Weapon;
Parent = parent;
} }
} }

View File

@ -7,23 +7,22 @@
void UUpgradeButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) void UUpgradeButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent)
{ {
WeaponName = Weapon->Name; WeaponName = Weapon->GetWeaponName();
Description = Weapon->UpgradeDescriptions[Weapon->CurrentLevel]; WeaponIcon = Weapon->GetIcon();
WeaponIcon = Weapon->Icon;
WeaponInstance = Weapon; WeaponInstance = Weapon;
Parent = parent; Parent = parent;
if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel())
{
Description = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()];
}
} }
void UUpgradeButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent) void UUpgradeButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent)
{ {
AWeapon* temp = NewObject<AWeapon>(this, Weapon); if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon))
if (temp)
{ {
WeaponName = temp->Name; SetData(tempWeapon, parent);
Description = temp->Description;
WeaponIcon = temp->Icon;
WeaponTemplate = Weapon;
Parent = parent;
} }
} }