Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "SpawnerProject.h"
  4. #include "Spawner.h"
  5.  
  6. // Sets default values
  7. ASpawner::ASpawner()
  8. {
  9.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  10.     PrimaryActorTick.bCanEverTick = false;
  11.     sceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  12.     RootComponent = sceneComponent;
  13.     OurRoot = sceneComponent;
  14.    
  15.     boxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Collider"));
  16.  
  17.     boxComponent->InitBoxExtent(boxExtent);
  18.     boxComponent->AttachTo(RootComponent);
  19.  
  20. }
  21.  
  22. // Called when the game starts or when spawned
  23. void ASpawner::BeginPlay()
  24. {
  25.     Super::BeginPlay();
  26.  
  27.     for (int i = 0; i < spawnAmount; i++) {
  28.         FActorSpawnParameters spawnParams;                      //create spawn params
  29.         spawnParams.Owner = this;                               //who did the spawn? THIS controller did it
  30.         spawnParams.Instigator = Instigator;                    //instigator to default
  31.         spawnLoc = this->GetActorLocation();                    //get actor location
  32.         RandomSpawnNVelocity();                                 //spawn random position of box extent & velocity
  33.         ourNewObject = GetWorld()->SpawnActor<AActor>(ourSpawnObject, objectLocation, FRotator::ZeroRotator, spawnParams);   //spawn actor
  34.         UStaticMeshComponent* SM = Cast<UStaticMeshComponent>(ourNewObject->GetRootComponent());
  35.         if (SM && firework)
  36.         {
  37.             SM->AddImpulse(objectVelocity*SM->GetMass());
  38.         }
  39.  
  40.         ourNewObject->SetLifeSpan(FMath::RandRange(objectLifeTimeMin, objectLifeTimeMax));                      // set life span
  41.         GetWorld()->GetTimerManager().SetTimer(timerHandle, this, &ASpawner::SpawnDespawn, spawnRate, true);   //spawn rate of object spawn
  42.         //could get better frames if disable active of object and renable back
  43.     }
  44. }
  45.  
  46. // Called every frame
  47. void ASpawner::Tick( float DeltaTime )
  48. {
  49.     Super::Tick( DeltaTime);
  50. }
  51.  
  52. /*
  53.     Spawn and Despawn object
  54. */
  55. void ASpawner::SpawnDespawn()
  56. {
  57.     for (int i = 0; i < spawnAmount; i++) {
  58.  
  59.         FActorSpawnParameters spawnParams;                      //create spawn params
  60.         spawnParams.Owner = this;                               //who did the spawn? THIS controller did it
  61.         spawnParams.Instigator = Instigator;                    //instigator to default
  62.         spawnLoc = this->GetActorLocation();                    //get actor location
  63.         RandomSpawnNVelocity();
  64.         ourNewObject = GetWorld()->SpawnActor<AActor>(ourSpawnObject, objectLocation, FRotator::ZeroRotator, spawnParams);
  65.         UStaticMeshComponent* SM = Cast<UStaticMeshComponent>(ourNewObject->GetRootComponent());
  66.  
  67.         if (SM && firework)
  68.         {
  69.             SM->AddImpulse(objectVelocity*SM->GetMass());
  70.         }
  71.  
  72.         ourNewObject->SetLifeSpan(FMath::RandRange(objectLifeTimeMin, objectLifeTimeMax));
  73.     }
  74. }
  75.  
  76. /*
  77.     Get Random position within box extent & Simulate firework physics
  78. */
  79. void ASpawner::RandomSpawnNVelocity()
  80. {
  81.     float x = boxExtent.X;
  82.     float y = boxExtent.Y;
  83.     float z = boxExtent.Z;
  84.     x = FMath::RandRange(spawnLoc.X - x, spawnLoc.X + x);
  85.     y = FMath::RandRange(spawnLoc.Y - y, spawnLoc.Y + y);
  86.     z = FMath::RandRange(spawnLoc.Z - z, spawnLoc.Z + z);
  87.  
  88.     if (!firework) {
  89.         objectLocation = FVector(x, y, z);
  90.     }
  91.     else {
  92.         objectLocation = FVector(x, y, boxExtent.Z);
  93.         objectVelocity = FVector(FMath::RandRange(-200.0f, 200.0f), FMath::RandRange(-200.0f, 200.0f), FMath::RandRange(minLaunchVelocity, maxLaunchVelocity));
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement