Skip to content

Commit d459faa

Browse files
committed
Added spawning state to 'SpawnVolume' class
1 parent 65334bc commit d459faa

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

Source/BatteryCollector/BatteryCollectorGameMode.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "BatteryCollectorCharacter.h"
66
#include "Kismet/GameplayStatics.h"
77
#include "Blueprint/UserWidget.h"
8+
#include "SpawnVolume.h"
89

910
ABatteryCollectorGameMode::ABatteryCollectorGameMode()
1011
{
@@ -22,6 +23,20 @@ ABatteryCollectorGameMode::ABatteryCollectorGameMode()
2223
void ABatteryCollectorGameMode::BeginPlay()
2324
{
2425
Super::BeginPlay();
26+
27+
// find all spawn volume Actors
28+
TArray<AActor*> FoundActors;
29+
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnVolume::StaticClass(), FoundActors);
30+
31+
for (auto Actor : FoundActors)
32+
{
33+
ASpawnVolume* SpawnVolumeActor = Cast<ASpawnVolume>(Actor);
34+
if (SpawnVolumeActor)
35+
{
36+
SpawnVolumeActors.AddUnique(SpawnVolumeActor);
37+
}
38+
}
39+
2540
SetCurrentState(EBatteryPlayState::EPlaying);
2641

2742
ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));

Source/BatteryCollector/BatteryCollectorGameMode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ABatteryCollectorGameMode : public AGameMode
5757
private:
5858
/**Keeps track of the current playing state */
5959
EBatteryPlayState CurrentState;
60+
61+
TArray<class ASpawnVolume*> SpawnVolumeActors;
6062
};
6163

6264

Source/BatteryCollector/SpawnVolume.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ ASpawnVolume::ASpawnVolume()
1919
//Set the spawn delay range
2020
SpawnDelayRangeLow = 1.0f;
2121
SpawnDelayRangeHigh = 4.5f;
22+
2223
}
2324

2425
// Called when the game starts or when spawned
2526
void ASpawnVolume::BeginPlay()
2627
{
2728
Super::BeginPlay();
29+
30+
2831

29-
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
30-
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
3132
}
3233

3334
// Called every frame
@@ -46,6 +47,21 @@ FVector ASpawnVolume::GetRandomPointInVolume()
4647

4748
}
4849

50+
void ASpawnVolume::SetSpawningActive(bool bShouldSpawn)
51+
{
52+
if (bShouldSpawn)
53+
{
54+
// Set the timer on Spawn Pickup
55+
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
56+
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
57+
}
58+
else
59+
{
60+
// clear the timer on Spawn Pickup
61+
GetWorldTimerManager().ClearTimer(SpawnTimer);
62+
}
63+
}
64+
4965
void ASpawnVolume::SpawnPickup()
5066
{
5167
// If we have set something to spawn:
@@ -71,11 +87,11 @@ void ASpawnVolume::SpawnPickup()
7187

7288
// spawn the pickup
7389
APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
74-
90+
7591
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
7692
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
93+
7794
}
7895
}
7996

8097
}
81-

Source/BatteryCollector/SpawnVolume.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
2727
UFUNCTION(BlueprintPure, Category = "Spawning")
2828
FVector GetRandomPointInVolume();
2929

30+
/**This function toggles whether or not the spawn volume spawns pickups */
31+
UFUNCTION(BlueprintCallable, Category = "Spawning")
32+
void SetSpawningActive(bool bShouldSpawn);
33+
3034
protected:
3135
/** The pickup to spawn*/
3236
UPROPERTY(EditAnywhere, Category = "Spawning")
@@ -49,7 +53,9 @@ class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
4953

5054
/** Handle spawning a new pickup */
5155
void SpawnPickup();
52-
56+
5357
/** The current spawn delay */
5458
float SpawnDelay;
59+
60+
5561
};

0 commit comments

Comments
 (0)