Advertisement
Viraax

Untitled

Jan 25th, 2021
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include "Spawner.h"
  2. #include "Enemy.h"
  3. #include "EnemyAIController.h"
  4. #include "Tower.h"
  5. #include "Blueprint/AIAsyncTaskBlueprintProxy.h"
  6. #include "Blueprint/AIBlueprintHelperLibrary.h"
  7. #include "Components/AudioComponent.h"
  8. #include "GameFramework/CharacterMovementComponent.h"
  9. #include "Kismet/GameplayStatics.h"
  10. #include "Kismet/KismetMathLibrary.h"
  11. #include "Blueprint/AIBlueprintHelperLibrary.h"
  12.  
  13. ASpawner::ASpawner()
  14. {
  15.     srand(time(nullptr));
  16.  
  17.     SpawnRate = 2.0f;
  18.     EnemiesSpawned = 0;
  19.     GoalActor = nullptr;
  20.     CurrentWave = 0;
  21.     EnemiesLeft = 0;
  22.     bIsPaused = false;
  23.     PauseTime = 10.0f;
  24.     Countdown = 0.0f;
  25.     bIsPlayingCountdown = false;
  26.     PrimaryActorTick.bCanEverTick = true;
  27.  
  28.     RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SpawnerSceneComponent"));
  29.  
  30.     // BoxComponent setup
  31.     BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Sphere"));
  32.     BoxComponent->SetBoxExtent(FVector(200.0f, 200.0f, 50.0f));
  33.     BoxComponent->SetHiddenInGame(false);
  34.     BoxComponent->SetupAttachment(RootComponent);
  35.  
  36.     //Find Countdown sound cue
  37.     const ConstructorHelpers::FObjectFinder<USoundCue> Sound(TEXT("/Game/Sounds/General/Countdown"));
  38.     if (Sound.Succeeded())
  39.         CountdownSound = Sound.Object;
  40.  
  41.     const ConstructorHelpers::FObjectFinder<USoundCue> WaveFinished(TEXT("/Game/Sounds/General/SQ_Wave_finished"));
  42.     if (WaveFinished.Succeeded())
  43.         WaveFinishedSound = WaveFinished.Object;
  44.  
  45.     // AudioComponent Setup
  46.     AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComponent"));
  47.     AudioComponent->SetupAttachment(RootComponent);
  48.     AudioComponent->VolumeMultiplier = 0.4f;
  49.     AudioComponent->Activate(true);
  50. }
  51.  
  52. void ASpawner::CreateWave()
  53. {
  54.     ++CurrentWave;
  55.     CurrentWaveEnemies = EnemyCountPerWave->GetFloatValue(static_cast<float>(CurrentWave));
  56.     EnemiesLeft = CurrentWaveEnemies;
  57.     EnemiesLeftToKill = EnemiesLeft;
  58.     EnemiesToSpawn = FMath::FloorToInt(rand() % EnemiesLeft);
  59.  
  60.     CheckEnemiesLeftToSpawn();
  61.     GetWorld()->GetTimerManager().SetTimer(SpawnTimer, this, &ASpawner::CheckEnemiesLeftToSpawn, 3.0f, true);
  62.     bIsPaused = false;
  63. }
  64.  
  65. void ASpawner::SpawnEnemies()
  66. {
  67.     for (int32 i = 0; i < EnemiesToSpawn; ++i)
  68.     {
  69.         FActorSpawnParameters SpawnInfo;
  70.         SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
  71.  
  72.         const auto Point = UKismetMathLibrary::RandomPointInBoundingBox(
  73.             GetActorLocation(), BoxComponent->GetScaledBoxExtent());
  74.         const auto Enemy = GetWorld()->SpawnActor<AEnemy>(Point, FRotator(0.0f, FMath::RandRange(0.0f, 360.0f), 0.0f),
  75.                                                           SpawnInfo);
  76.         Enemy->Spawner = this;
  77.         Enemy->SpawnDefaultController();
  78.  
  79.         ++EnemiesSpawned;
  80.     }
  81.  
  82.     EnemiesLeft -= EnemiesToSpawn;
  83. }
  84.  
  85. void ASpawner::CheckEnemiesLeftToSpawn()
  86. {
  87.     if (EnemiesLeft > 0)
  88.     {
  89.         EnemiesToSpawn = FMath::RandRange(1, EnemiesLeft);
  90.         SpawnEnemies();
  91.     }
  92.  
  93.     if (EnemiesLeft < 0)
  94.     {
  95.         EnemiesLeft = 0;
  96.     }
  97. }
  98.  
  99. void ASpawner::BeginPlay()
  100. {
  101.     Super::BeginPlay();
  102.     CreateWave();
  103. }
  104.  
  105. void ASpawner::Tick(float DeltaTime)
  106. {
  107.     Super::Tick(DeltaTime);
  108.  
  109.     if (EnemiesLeftToKill == 0 && !bIsPaused)
  110.     {
  111.         GetWorld()->GetTimerManager().ClearTimer(SpawnTimer);
  112.         FTimerHandle WavePause;
  113.         Countdown = PauseTime;
  114.         bIsPaused = true;
  115.         bIsPlayingCountdown = false;
  116.         UGameplayStatics::PlaySound2D(GetWorld(), WaveFinishedSound, 1.0f);
  117.  
  118.         GetWorld()->GetTimerManager().SetTimer(WavePause, [this]()
  119.         {
  120.             CreateWave();
  121.         }, PauseTime, false);
  122.     }
  123.  
  124.     if (bIsPaused)
  125.     {
  126.         Countdown -= DeltaTime;
  127.  
  128.         if (FMath::RoundToInt(Countdown) == 3 && !bIsPlayingCountdown)
  129.         {
  130.             UGameplayStatics::PlaySound2D(GetWorld(), CountdownSound, 1.0f);
  131.             bIsPlayingCountdown = true;
  132.         }
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement