From 293d0ae8b218351ad5313ac10030544c92fe517a Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 18 Apr 2025 03:05:22 +0100 Subject: [PATCH] Create and Add SwarmAgents to SwarmWeapon --- Content/Weapons/Swarm/BP_SwarmActor.uasset | 3 -- Content/Weapons/Swarm/BP_SwarmAgent.uasset | 3 ++ Content/Weapons/Swarm/BP_SwarmWeapon.uasset | 4 +- Source/vampires/Weapons/SwarmAgent.cpp | 55 +++++++++++++++++++++ Source/vampires/Weapons/SwarmAgent.h | 35 +++++++++++++ Source/vampires/Weapons/SwarmWeapon.cpp | 32 +++++++----- Source/vampires/Weapons/SwarmWeapon.h | 21 ++++---- 7 files changed, 126 insertions(+), 27 deletions(-) delete mode 100644 Content/Weapons/Swarm/BP_SwarmActor.uasset create mode 100644 Content/Weapons/Swarm/BP_SwarmAgent.uasset create mode 100644 Source/vampires/Weapons/SwarmAgent.cpp create mode 100644 Source/vampires/Weapons/SwarmAgent.h diff --git a/Content/Weapons/Swarm/BP_SwarmActor.uasset b/Content/Weapons/Swarm/BP_SwarmActor.uasset deleted file mode 100644 index 82fe8e9..0000000 --- a/Content/Weapons/Swarm/BP_SwarmActor.uasset +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:acd6b510083a053b08bfafc26e2a96b746e2fd9af490034fa10de6216cf99751 -size 25667 diff --git a/Content/Weapons/Swarm/BP_SwarmAgent.uasset b/Content/Weapons/Swarm/BP_SwarmAgent.uasset new file mode 100644 index 0000000..b400662 --- /dev/null +++ b/Content/Weapons/Swarm/BP_SwarmAgent.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d3d505f4c3315cf068f6ce1648df6e255384fb3f211a713ccae7b8f6001320e +size 23787 diff --git a/Content/Weapons/Swarm/BP_SwarmWeapon.uasset b/Content/Weapons/Swarm/BP_SwarmWeapon.uasset index 46138b4..d799e11 100644 --- a/Content/Weapons/Swarm/BP_SwarmWeapon.uasset +++ b/Content/Weapons/Swarm/BP_SwarmWeapon.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f3d0ef2726f97d3a8a165d47984754523e455433cc2185baa91ced52c0c026fc -size 24287 +oid sha256:b11910cb939058d8132af784c4443dfcee882a4ecd2677b2aabeba685d2f0bbe +size 25189 diff --git a/Source/vampires/Weapons/SwarmAgent.cpp b/Source/vampires/Weapons/SwarmAgent.cpp new file mode 100644 index 0000000..dd344cd --- /dev/null +++ b/Source/vampires/Weapons/SwarmAgent.cpp @@ -0,0 +1,55 @@ +// Louis Hobbs | 2024-2025 + + +#include "SwarmAgent.h" + +#include "Components/SphereComponent.h" +#include "vampires/EnemyCharacter.h" +#include "vampires/HealthComponent.h" +#include "vampires/Weapon.h" + + +// Sets default values +ASwarmAgent::ASwarmAgent() +{ + SphereComponent = CreateDefaultSubobject(TEXT("Sphere Component")); + SetRootComponent(SphereComponent); + SphereComponent->SetSphereRadius(50.0f); + SphereComponent->SetCollisionProfileName(TEXT("Weapon")); + + StaticMeshComponent = CreateDefaultSubobject(TEXT("Static Mesh Component")); + StaticMeshComponent->AttachToComponent(SphereComponent, FAttachmentTransformRules::KeepRelativeTransform); + StaticMeshComponent->SetEnableGravity(false); + StaticMeshComponent->SetGenerateOverlapEvents(false); + StaticMeshComponent->SetCollisionProfileName(TEXT("NoCollision")); +} + +// Called when the game starts or when spawned +void ASwarmAgent::BeginPlay() +{ + Super::BeginPlay(); + + SphereComponent->OnComponentBeginOverlap.AddDynamic(this, &ASwarmAgent::OnSwarmAgentBeginOverlap); +} + +void ASwarmAgent::OnSwarmAgentBeginOverlap(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(); + } + + AWeapon* ownerWeapon = Cast(GetOwner()); + EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this); + } + } +} diff --git a/Source/vampires/Weapons/SwarmAgent.h b/Source/vampires/Weapons/SwarmAgent.h new file mode 100644 index 0000000..2f08e7c --- /dev/null +++ b/Source/vampires/Weapons/SwarmAgent.h @@ -0,0 +1,35 @@ +// Louis Hobbs | 2024-2025 + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "SwarmAgent.generated.h" + +class USphereComponent; + +UCLASS() +class VAMPIRES_API ASwarmAgent : public AActor +{ + GENERATED_BODY() + +public: + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + USphereComponent* SphereComponent = nullptr; + + UPROPERTY(EditAnywhere, BlueprintReadWrite) + UStaticMeshComponent* StaticMeshComponent = nullptr; + + // Sets default values for this actor's properties + ASwarmAgent(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +private: + UFUNCTION() + void OnSwarmAgentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult); +}; diff --git a/Source/vampires/Weapons/SwarmWeapon.cpp b/Source/vampires/Weapons/SwarmWeapon.cpp index 9138718..c29a5b7 100644 --- a/Source/vampires/Weapons/SwarmWeapon.cpp +++ b/Source/vampires/Weapons/SwarmWeapon.cpp @@ -3,10 +3,7 @@ #include "SwarmWeapon.h" -#include "VectorTypes.h" -#include "Kismet/KismetSystemLibrary.h" -#include "vampires/EnemyCharacter.h" - +#include "SwarmAgent.h" // Sets default values ASwarmWeapon::ASwarmWeapon() @@ -30,8 +27,8 @@ void ASwarmWeapon::BeginPlay() Super::BeginPlay(); // Spawn the swarm actors in - SwarmActors.Add(GetWorld()->SpawnActor(SwarmActor)); - SwarmActors.Add(GetWorld()->SpawnActor(SwarmActor)); + SpawnSwarmAgent(); + SpawnSwarmAgent(); // Start the timeline if (SwarmCurve != nullptr) @@ -41,7 +38,6 @@ void ASwarmWeapon::BeginPlay() TimelineComponent->SetPlayRate(TimelinePlayRate); TimelineComponent->PlayFromStart(); - } void ASwarmWeapon::TimelineCallback(float val) @@ -56,38 +52,42 @@ void ASwarmWeapon::TimelineCallback(float val) FVector Direction = FVector(0.0, 1, 0.0); FVector RotatedDirection = Direction.RotateAngleAxis(val * 360.0f + offset, FVector(0.0f, 0.0f, 1.0f)); FVector NewLocation = CenterLocation + (RotatedDirection * Distance); + NewLocation.Z = 140.0f; SwarmActors[i]->SetActorLocation(NewLocation); } } bool ASwarmWeapon::UpgradeWeapon_Implementation() { - if (!Super::UpgradeWeapon_Implementation()) return false; + if (!Super::UpgradeWeapon_Implementation()) + { + return false; + } switch (CurrentLevel) { case 1: - SwarmActors.Add(GetWorld()->SpawnActor(SwarmActor)); + SpawnSwarmAgent(); break; case 2: Distance *= 1.25f; - TimelineComponent->SetPlayRate(TimelineComponent->GetPlayRate() * 1.3f); + TimelineComponent->SetPlayRate(TimelineComponent->GetPlayRate() * 1.3f); break; case 3: Damage += 10; break; case 4: - SwarmActors.Add(GetWorld()->SpawnActor(SwarmActor)); + SpawnSwarmAgent(); break; case 5: Distance *= 1.25f; - TimelineComponent->SetPlayRate(TimelineComponent->GetPlayRate() * 1.3f); + TimelineComponent->SetPlayRate(TimelineComponent->GetPlayRate() * 1.3f); break; case 6: Damage += 10; break; case 7: - SwarmActors.Add(GetWorld()->SpawnActor(SwarmActor)); + SpawnSwarmAgent(); break; default: return false; @@ -97,3 +97,9 @@ bool ASwarmWeapon::UpgradeWeapon_Implementation() return true; } +void ASwarmWeapon::SpawnSwarmAgent() +{ + ASwarmAgent* newAgent = GetWorld()->SpawnActor(SwarmActor, GetActorLocation() / 2, FRotator(0, 0, 0)); + newAgent->SetOwner(this); + SwarmActors.Add(newAgent); +} diff --git a/Source/vampires/Weapons/SwarmWeapon.h b/Source/vampires/Weapons/SwarmWeapon.h index 36fdfd5..8d4bec0 100644 --- a/Source/vampires/Weapons/SwarmWeapon.h +++ b/Source/vampires/Weapons/SwarmWeapon.h @@ -7,6 +7,8 @@ #include "vampires/Weapon.h" #include "SwarmWeapon.generated.h" +class ASwarmAgent; + UCLASS() class VAMPIRES_API ASwarmWeapon : public AWeapon { @@ -15,26 +17,24 @@ class VAMPIRES_API ASwarmWeapon : public AWeapon public: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Timeline") TObjectPtr TimelineComponent = nullptr; - + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TObjectPtr SwarmCurve; UPROPERTY(EditAnywhere, BlueprintReadWrite) float TimelinePlayRate = 1; - + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) - TSubclassOf SwarmActor; + TSubclassOf SwarmActor; UPROPERTY(EditAnywhere, BlueprintReadWrite) float Distance = 250.0f; - - private: FOnTimelineFloat onTimelineCallback; - TArray SwarmActors; - + TArray SwarmActors; + public: // Sets default values for this actor's properties ASwarmWeapon(); @@ -46,6 +46,9 @@ protected: public: UFUNCTION() void TimelineCallback(float val); - - virtual bool UpgradeWeapon_Implementation() override; + + virtual bool UpgradeWeapon_Implementation() override; + +private: + void SpawnSwarmAgent(); };