Create and Add SwarmAgents to SwarmWeapon

This commit is contained in:
baz 2025-04-18 03:05:22 +01:00
parent 5adafd6519
commit 293d0ae8b2
7 changed files with 126 additions and 27 deletions

BIN
Content/Weapons/Swarm/BP_SwarmActor.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Weapons/Swarm/BP_SwarmAgent.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

View File

@ -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<USphereComponent>(TEXT("Sphere Component"));
SetRootComponent(SphereComponent);
SphereComponent->SetSphereRadius(50.0f);
SphereComponent->SetCollisionProfileName(TEXT("Weapon"));
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(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<AEnemyCharacter>(OtherActor))
{
UHealthComponent* EnemyHealthComponent = Enemy->GetHealthComponent();
if (!EnemyHealthComponent->GetIsDead())
{
AController* ownerController = nullptr;
if (AVampireCharacter* character = Cast<AVampireCharacter>(GetOwner()))
{
ownerController = character->GetController();
}
AWeapon* ownerWeapon = Cast<AWeapon>(GetOwner());
EnemyHealthComponent->TakeDamage(Enemy, ownerWeapon->Damage, nullptr, ownerController, this);
}
}
}

View File

@ -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);
};

View File

@ -3,10 +3,7 @@
#include "SwarmWeapon.h" #include "SwarmWeapon.h"
#include "VectorTypes.h" #include "SwarmAgent.h"
#include "Kismet/KismetSystemLibrary.h"
#include "vampires/EnemyCharacter.h"
// Sets default values // Sets default values
ASwarmWeapon::ASwarmWeapon() ASwarmWeapon::ASwarmWeapon()
@ -30,8 +27,8 @@ void ASwarmWeapon::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
// Spawn the swarm actors in // Spawn the swarm actors in
SwarmActors.Add(GetWorld()->SpawnActor<AActor>(SwarmActor)); SpawnSwarmAgent();
SwarmActors.Add(GetWorld()->SpawnActor<AActor>(SwarmActor)); SpawnSwarmAgent();
// Start the timeline // Start the timeline
if (SwarmCurve != nullptr) if (SwarmCurve != nullptr)
@ -41,7 +38,6 @@ void ASwarmWeapon::BeginPlay()
TimelineComponent->SetPlayRate(TimelinePlayRate); TimelineComponent->SetPlayRate(TimelinePlayRate);
TimelineComponent->PlayFromStart(); TimelineComponent->PlayFromStart();
} }
void ASwarmWeapon::TimelineCallback(float val) void ASwarmWeapon::TimelineCallback(float val)
@ -56,18 +52,22 @@ void ASwarmWeapon::TimelineCallback(float val)
FVector Direction = FVector(0.0, 1, 0.0); FVector Direction = FVector(0.0, 1, 0.0);
FVector RotatedDirection = Direction.RotateAngleAxis(val * 360.0f + offset, FVector(0.0f, 0.0f, 1.0f)); FVector RotatedDirection = Direction.RotateAngleAxis(val * 360.0f + offset, FVector(0.0f, 0.0f, 1.0f));
FVector NewLocation = CenterLocation + (RotatedDirection * Distance); FVector NewLocation = CenterLocation + (RotatedDirection * Distance);
NewLocation.Z = 140.0f;
SwarmActors[i]->SetActorLocation(NewLocation); SwarmActors[i]->SetActorLocation(NewLocation);
} }
} }
bool ASwarmWeapon::UpgradeWeapon_Implementation() bool ASwarmWeapon::UpgradeWeapon_Implementation()
{ {
if (!Super::UpgradeWeapon_Implementation()) return false; if (!Super::UpgradeWeapon_Implementation())
{
return false;
}
switch (CurrentLevel) switch (CurrentLevel)
{ {
case 1: case 1:
SwarmActors.Add(GetWorld()->SpawnActor<AActor>(SwarmActor)); SpawnSwarmAgent();
break; break;
case 2: case 2:
Distance *= 1.25f; Distance *= 1.25f;
@ -77,7 +77,7 @@ bool ASwarmWeapon::UpgradeWeapon_Implementation()
Damage += 10; Damage += 10;
break; break;
case 4: case 4:
SwarmActors.Add(GetWorld()->SpawnActor<AActor>(SwarmActor)); SpawnSwarmAgent();
break; break;
case 5: case 5:
Distance *= 1.25f; Distance *= 1.25f;
@ -87,7 +87,7 @@ bool ASwarmWeapon::UpgradeWeapon_Implementation()
Damage += 10; Damage += 10;
break; break;
case 7: case 7:
SwarmActors.Add(GetWorld()->SpawnActor<AActor>(SwarmActor)); SpawnSwarmAgent();
break; break;
default: default:
return false; return false;
@ -97,3 +97,9 @@ bool ASwarmWeapon::UpgradeWeapon_Implementation()
return true; return true;
} }
void ASwarmWeapon::SpawnSwarmAgent()
{
ASwarmAgent* newAgent = GetWorld()->SpawnActor<ASwarmAgent>(SwarmActor, GetActorLocation() / 2, FRotator(0, 0, 0));
newAgent->SetOwner(this);
SwarmActors.Add(newAgent);
}

View File

@ -7,6 +7,8 @@
#include "vampires/Weapon.h" #include "vampires/Weapon.h"
#include "SwarmWeapon.generated.h" #include "SwarmWeapon.generated.h"
class ASwarmAgent;
UCLASS() UCLASS()
class VAMPIRES_API ASwarmWeapon : public AWeapon class VAMPIRES_API ASwarmWeapon : public AWeapon
{ {
@ -23,17 +25,15 @@ public:
float TimelinePlayRate = 1; float TimelinePlayRate = 1;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<class AActor> SwarmActor; TSubclassOf<ASwarmAgent> SwarmActor;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Distance = 250.0f; float Distance = 250.0f;
private: private:
FOnTimelineFloat onTimelineCallback; FOnTimelineFloat onTimelineCallback;
TArray<AActor*> SwarmActors; TArray<ASwarmAgent*> SwarmActors;
public: public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
@ -48,4 +48,7 @@ public:
void TimelineCallback(float val); void TimelineCallback(float val);
virtual bool UpgradeWeapon_Implementation() override; virtual bool UpgradeWeapon_Implementation() override;
private:
void SpawnSwarmAgent();
}; };