From e0dd825be9a13c2861d335a480ed6dbae86664f2 Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 25 Jul 2025 23:03:58 +0100 Subject: [PATCH] Minor refactor to base weapon class --- Source/vampires/Projectile.cpp | 2 +- Source/vampires/Weapon.cpp | 25 +++--- Source/vampires/Weapon.h | 78 ++++++++++--------- Source/vampires/WeaponInventoryComponent.cpp | 2 +- Source/vampires/Weapons/GarlicWeapon.h | 4 +- Source/vampires/Weapons/LightningRingWeapon.h | 6 +- Source/vampires/Weapons/ProjectileWeapon.h | 6 +- Source/vampires/Weapons/SwarmAgent.cpp | 2 +- Source/vampires/Weapons/SwarmWeapon.h | 10 +-- Source/vampires/Widgets/LevelUpWidget.cpp | 2 +- .../Widgets/StarterWeaponButtonDataObject.cpp | 19 +++-- .../Widgets/UpgradeButtonDataObject.cpp | 19 +++-- 12 files changed, 92 insertions(+), 83 deletions(-) diff --git a/Source/vampires/Projectile.cpp b/Source/vampires/Projectile.cpp index 8e6c98a..63eb94f 100644 --- a/Source/vampires/Projectile.cpp +++ b/Source/vampires/Projectile.cpp @@ -110,7 +110,7 @@ void AProjectile::OnProjectileBeginOverlap(UPrimitiveComponent* OverlappedCompon } AProjectileWeapon* ownerWeapon = Cast(GetOwner()); - EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); + EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->GetDamage(), nullptr, ownerController, this); RemainingDamagableEnemies--; diff --git a/Source/vampires/Weapon.cpp b/Source/vampires/Weapon.cpp index 500c15f..7e60bd6 100644 --- a/Source/vampires/Weapon.cpp +++ b/Source/vampires/Weapon.cpp @@ -12,7 +12,7 @@ // Sets default values 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; BoxComponent = CreateDefaultSubobject(TEXT("Box Component")); @@ -30,9 +30,9 @@ void AWeapon::BeginPlay() BoxComponent->OnComponentEndOverlap.AddDynamic(this, &AWeapon::OnWeaponEndOverlap); FViewport::ViewportResizedEvent.AddUObject(this, &AWeapon::ResizeBoxComponent); - + ResizeBoxComponent(GEngine->GameViewport->Viewport, -2); - + GetWorldTimerManager().SetTimer(WeaponTimerHandle, this, &AWeapon::FireWeaponAction, WeaponCooldown, true); } @@ -59,12 +59,13 @@ bool AWeapon::UpgradeWeapon_Implementation() CurrentLevel++; return true; } - + return false; } 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(OtherActor)) { @@ -73,7 +74,7 @@ void AWeapon::OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AAc } void AWeapon::OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) { if (AEnemyCharacter* Enemy = Cast(OtherActor)) { @@ -81,11 +82,13 @@ 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) + { + return; + } - if (!GEngine->GameViewport) return; - FVector TopLeft, TopLeftDir; FVector TopRight, TopRightDir; FVector BottomLeft, BottomLeftDir; @@ -108,11 +111,11 @@ 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 // 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; height *= 266.666; } - + BoxComponent->SetBoxExtent(FVector(height, width, 200.0f)); } diff --git a/Source/vampires/Weapon.h b/Source/vampires/Weapon.h index fd772e4..1ef00d6 100644 --- a/Source/vampires/Weapon.h +++ b/Source/vampires/Weapon.h @@ -16,78 +16,86 @@ class VAMPIRES_API AWeapon : public AActor { GENERATED_BODY() -public: - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") +protected: + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information") FText Name; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information") FText Description; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") + + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Information") TObjectPtr Icon; - UPROPERTY(EditDefaultsOnly, Category = "Weapon Properties") + UPROPERTY(EditDefaultsOnly, Category = "Weapon | Properties") TObjectPtr WeaponActivatedSoundBase = nullptr; - UPROPERTY(EditDefaultsOnly, Category = "Weapon Properties") - TObjectPtr WeaponSprite = nullptr; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties") float WeaponCooldown = 1.0f; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties") float Damage; - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") - TArray UpgradeDescriptions; - - int CurrentLevel = 0; - - UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties") + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Properties") bool FollowPlayer = true; - UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Upgrades") + TArray UpgradeDescriptions; + + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon | Upgrades") int MaxLevel = 0; -protected: - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UPROPERTY() TObjectPtr BoxComponent = nullptr; TArray> OverlappedEnemies = TArray>(); - + + int CurrentLevel = 0; + private: FTimerHandle WeaponTimerHandle; - public: - // Sets default values for this actor's properties + // Sets default values for this actor's AWeapon(); + UFUNCTION(BlueprintNativeEvent) + bool UpgradeWeapon(); + virtual bool UpgradeWeapon_Implementation(); + + FText GetWeaponName() const { return Name; } + + FText GetDescription() const { return Description; } + + TObjectPtr GetIcon() const { return Icon; } + + float GetDamage() const { return Damage; } + + bool GetFollowPlayer() const { return FollowPlayer; } + + TArray GetUpgradeDescriptions() const { return UpgradeDescriptions; } + + int GetWeaponLevel() const { return CurrentLevel; } + protected: // Called when the game starts or when spawned virtual void BeginPlay() override; void ResetWeaponTimer(); - -public: + UFUNCTION(BlueprintNativeEvent) void FireWeaponAction(); virtual void FireWeaponAction_Implementation(); - UFUNCTION(BlueprintNativeEvent) - bool UpgradeWeapon(); - virtual bool UpgradeWeapon_Implementation(); - UFUNCTION() virtual void OnWeaponBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, - const FHitResult& SweepResult); + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult); UFUNCTION() - virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, - int32 OtherBodyIndex); + virtual void OnWeaponEndOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, + UPrimitiveComponent* OtherComp, + int32 OtherBodyIndex); private: - - void ResizeBoxComponent(FViewport* Viewport, uint32 unused); + void ResizeBoxComponent(FViewport* Viewport, uint32 Unused); }; diff --git a/Source/vampires/WeaponInventoryComponent.cpp b/Source/vampires/WeaponInventoryComponent.cpp index daa1a34..2409dad 100644 --- a/Source/vampires/WeaponInventoryComponent.cpp +++ b/Source/vampires/WeaponInventoryComponent.cpp @@ -42,7 +42,7 @@ void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf Weapon SpawnParameters.Owner = GetOwner(); AWeapon* weapon = GetWorld()->SpawnActor(Weapon, SpawnParameters.Owner->GetTransform(), SpawnParameters); - if (weapon->FollowPlayer) + if (weapon->GetFollowPlayer()) { weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepWorldTransform); } diff --git a/Source/vampires/Weapons/GarlicWeapon.h b/Source/vampires/Weapons/GarlicWeapon.h index 34b55e5..9747305 100644 --- a/Source/vampires/Weapons/GarlicWeapon.h +++ b/Source/vampires/Weapons/GarlicWeapon.h @@ -28,12 +28,12 @@ class VAMPIRES_API AGarlicWeapon : public AWeapon { GENERATED_BODY() public: - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Weapon | Garlic") TObjectPtr SphereComponent; TArray GarlicOverlappedEnemies; - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"), Category = "Weapon | Garlic") TObjectPtr VisualEffectMeshComponent; private: diff --git a/Source/vampires/Weapons/LightningRingWeapon.h b/Source/vampires/Weapons/LightningRingWeapon.h index 4197cd3..baf6f88 100644 --- a/Source/vampires/Weapons/LightningRingWeapon.h +++ b/Source/vampires/Weapons/LightningRingWeapon.h @@ -18,13 +18,13 @@ class VAMPIRES_API ALightningRingWeapon : public AWeapon GENERATED_BODY() public: - UPROPERTY(EditAnywhere, BlueprintReadWrite) + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring") int LightningBolts = 1; - UPROPERTY(EditAnywhere, BlueprintReadWrite) + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring") float LightingBoltRadius = 200.0f; - UPROPERTY(EditAnywhere, BlueprintReadWrite) + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Lightning Ring") TObjectPtr LightningEffectSystem; public: diff --git a/Source/vampires/Weapons/ProjectileWeapon.h b/Source/vampires/Weapons/ProjectileWeapon.h index 55ec964..6d4749f 100644 --- a/Source/vampires/Weapons/ProjectileWeapon.h +++ b/Source/vampires/Weapons/ProjectileWeapon.h @@ -18,13 +18,13 @@ class VAMPIRES_API AProjectileWeapon : public AWeapon GENERATED_BODY() public: - UPROPERTY(EditAnywhere, Category = "Weapon Properties") + UPROPERTY(EditAnywhere, Category = "Weapon | Projectiles") TObjectPtr ProjectileTemplate = nullptr; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Projectiles") int ProjectilesPerActivation = 1; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Projectiles") float ProjectileSpawningDelay = 0.25f; protected: diff --git a/Source/vampires/Weapons/SwarmAgent.cpp b/Source/vampires/Weapons/SwarmAgent.cpp index dcb2fa6..e0a7229 100644 --- a/Source/vampires/Weapons/SwarmAgent.cpp +++ b/Source/vampires/Weapons/SwarmAgent.cpp @@ -52,7 +52,7 @@ void ASwarmAgent::OnSwarmAgentBeginOverlap(UPrimitiveComponent* OverlappedCompon if (AVampireCharacter* character = Cast(ownerWeapon->GetOwner())) { AController* ownerController = character->GetController(); - EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); + EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->GetDamage(), nullptr, ownerController, this); } } } diff --git a/Source/vampires/Weapons/SwarmWeapon.h b/Source/vampires/Weapons/SwarmWeapon.h index 953ff72..13cd350 100644 --- a/Source/vampires/Weapons/SwarmWeapon.h +++ b/Source/vampires/Weapons/SwarmWeapon.h @@ -15,19 +15,19 @@ class VAMPIRES_API ASwarmWeapon : public AWeapon GENERATED_BODY() public: - UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Timeline") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon | Swarm") TObjectPtr TimelineComponent = nullptr; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Swarm") TObjectPtr SwarmCurve; - UPROPERTY(EditAnywhere, BlueprintReadWrite) + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Swarm") float TimelinePlayRate = 1; - UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon | Swarm") TSubclassOf SwarmActor; - UPROPERTY(EditAnywhere, BlueprintReadWrite) + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon | Swarm") float Distance = 250.0f; private: diff --git a/Source/vampires/Widgets/LevelUpWidget.cpp b/Source/vampires/Widgets/LevelUpWidget.cpp index 7ea61f7..cf729b0 100644 --- a/Source/vampires/Widgets/LevelUpWidget.cpp +++ b/Source/vampires/Widgets/LevelUpWidget.cpp @@ -43,7 +43,7 @@ void ULevelUpWidget::NativeConstruct() TArray upgradeItems; for (AWeapon* weapon : Inventory) { - if (weapon->CurrentLevel < weapon->UpgradeDescriptions.Num()) + if (weapon->GetWeaponLevel() < weapon->GetUpgradeDescriptions().Num()) { UUpgradeButtonDataObject* Temp = NewObject(this); Temp->SetData(weapon, this); diff --git a/Source/vampires/Widgets/StarterWeaponButtonDataObject.cpp b/Source/vampires/Widgets/StarterWeaponButtonDataObject.cpp index b05d916..067a01a 100644 --- a/Source/vampires/Widgets/StarterWeaponButtonDataObject.cpp +++ b/Source/vampires/Widgets/StarterWeaponButtonDataObject.cpp @@ -7,23 +7,22 @@ void UStarterWeaponButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) { - WeaponName = Weapon->Name; - Description = Weapon->UpgradeDescriptions[Weapon->CurrentLevel]; - WeaponIcon = Weapon->Icon; + WeaponName = Weapon->GetWeaponName(); + WeaponIcon = Weapon->GetIcon(); WeaponInstance = Weapon; Parent = parent; + + if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel()) + { + Description = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()]; + } } void UStarterWeaponButtonDataObject::SetData(TSubclassOf Weapon, UUserWidget* parent) { - AWeapon* temp = NewObject(this, Weapon); - if (temp) + if (AWeapon* tempWeapon = NewObject(this, Weapon)) { - WeaponName = temp->Name; - Description = temp->Description; - WeaponIcon = temp->Icon; - WeaponTemplate = Weapon; - Parent = parent; + SetData(tempWeapon, parent); } } diff --git a/Source/vampires/Widgets/UpgradeButtonDataObject.cpp b/Source/vampires/Widgets/UpgradeButtonDataObject.cpp index 7be6839..646a553 100644 --- a/Source/vampires/Widgets/UpgradeButtonDataObject.cpp +++ b/Source/vampires/Widgets/UpgradeButtonDataObject.cpp @@ -7,23 +7,22 @@ void UUpgradeButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) { - WeaponName = Weapon->Name; - Description = Weapon->UpgradeDescriptions[Weapon->CurrentLevel]; - WeaponIcon = Weapon->Icon; + WeaponName = Weapon->GetWeaponName(); + WeaponIcon = Weapon->GetIcon(); WeaponInstance = Weapon; Parent = parent; + + if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel()) + { + Description = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()]; + } } void UUpgradeButtonDataObject::SetData(TSubclassOf Weapon, UUserWidget* parent) { - AWeapon* temp = NewObject(this, Weapon); - if (temp) + if (AWeapon* tempWeapon = NewObject(this, Weapon)) { - WeaponName = temp->Name; - Description = temp->Description; - WeaponIcon = temp->Icon; - WeaponTemplate = Weapon; - Parent = parent; + SetData(tempWeapon, parent); } }