Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "SpawnerProject.h"
- #include "Spawner.h"
- // Sets default values
- ASpawner::ASpawner()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = false;
- sceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
- RootComponent = sceneComponent;
- OurRoot = sceneComponent;
- boxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Collider"));
- boxComponent->InitBoxExtent(boxExtent);
- boxComponent->AttachTo(RootComponent);
- }
- // Called when the game starts or when spawned
- void ASpawner::BeginPlay()
- {
- Super::BeginPlay();
- for (int i = 0; i < spawnAmount; i++) {
- FActorSpawnParameters spawnParams; //create spawn params
- spawnParams.Owner = this; //who did the spawn? THIS controller did it
- spawnParams.Instigator = Instigator; //instigator to default
- spawnLoc = this->GetActorLocation(); //get actor location
- RandomSpawnNVelocity(); //spawn random position of box extent & velocity
- ourNewObject = GetWorld()->SpawnActor<AActor>(ourSpawnObject, objectLocation, FRotator::ZeroRotator, spawnParams); //spawn actor
- UStaticMeshComponent* SM = Cast<UStaticMeshComponent>(ourNewObject->GetRootComponent());
- if (SM && firework)
- {
- SM->AddImpulse(objectVelocity*SM->GetMass());
- }
- ourNewObject->SetLifeSpan(FMath::RandRange(objectLifeTimeMin, objectLifeTimeMax)); // set life span
- GetWorld()->GetTimerManager().SetTimer(timerHandle, this, &ASpawner::SpawnDespawn, spawnRate, true); //spawn rate of object spawn
- //could get better frames if disable active of object and renable back
- }
- }
- // Called every frame
- void ASpawner::Tick( float DeltaTime )
- {
- Super::Tick( DeltaTime);
- }
- /*
- Spawn and Despawn object
- */
- void ASpawner::SpawnDespawn()
- {
- for (int i = 0; i < spawnAmount; i++) {
- FActorSpawnParameters spawnParams; //create spawn params
- spawnParams.Owner = this; //who did the spawn? THIS controller did it
- spawnParams.Instigator = Instigator; //instigator to default
- spawnLoc = this->GetActorLocation(); //get actor location
- RandomSpawnNVelocity();
- ourNewObject = GetWorld()->SpawnActor<AActor>(ourSpawnObject, objectLocation, FRotator::ZeroRotator, spawnParams);
- UStaticMeshComponent* SM = Cast<UStaticMeshComponent>(ourNewObject->GetRootComponent());
- if (SM && firework)
- {
- SM->AddImpulse(objectVelocity*SM->GetMass());
- }
- ourNewObject->SetLifeSpan(FMath::RandRange(objectLifeTimeMin, objectLifeTimeMax));
- }
- }
- /*
- Get Random position within box extent & Simulate firework physics
- */
- void ASpawner::RandomSpawnNVelocity()
- {
- float x = boxExtent.X;
- float y = boxExtent.Y;
- float z = boxExtent.Z;
- x = FMath::RandRange(spawnLoc.X - x, spawnLoc.X + x);
- y = FMath::RandRange(spawnLoc.Y - y, spawnLoc.Y + y);
- z = FMath::RandRange(spawnLoc.Z - z, spawnLoc.Z + z);
- if (!firework) {
- objectLocation = FVector(x, y, z);
- }
- else {
- objectLocation = FVector(x, y, boxExtent.Z);
- objectVelocity = FVector(FMath::RandRange(-200.0f, 200.0f), FMath::RandRange(-200.0f, 200.0f), FMath::RandRange(minLaunchVelocity, maxLaunchVelocity));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement