// Fill out your copyright notice in the Description page of Project Settings.
#include "DispatcherTest.h"
#include "ActorReferencesUtils.h"
// Sets default values
ADispatcherTest::ADispatcherTest()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = RootComp;
BoxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));
BoxComp->SetupAttachment(RootComponent);
BoxComp->SetRelativeScale3D(FVector(2, 2, 2));
ArrowComp = CreateDefaultSubobject<UArrowComponent>(TEXT("ArrowComp"));
ArrowComp->SetRelativeRotation(FRotator(0, 90, 0));
ArrowComp->SetupAttachment(RootComponent);
BoxMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BoxMesh"));
BoxMesh->SetRelativeScale3D(FVector(0.64, 0.64, 0.64));
BoxMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
BoxMesh->SetupAttachment(BoxComp);
BoxComp->OnComponentBeginOverlap.AddDynamic(this, &ADispatcherTest::OnOverlapBegin);
}
// Called when the game starts or when spawned
void ADispatcherTest::BeginPlay()
{
Super::BeginPlay();
CreateChildActor(ChildCount, MyChildren);
}
// Called every frame
void ADispatcherTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADispatcherTest::CreateChildActor(const int Count, TArray<ADisPatcherChild*>& ChildrenArr) const
{
for (int i = 0; i <= Count; i++)
{
FVector TargetLoc = GetActorLocation() + FVector(0, 100*i, 0);
TargetLoc -= this->GetActorLocation();
TargetLoc = FRotationMatrix(RootComponent->GetComponentTransform().Rotator()).TransformPosition(TargetLoc);
TargetLoc += this->GetActorLocation();
AActor* TempChild;
TempChild = GetWorld()->SpawnActorDeferred<AActor>(ChildClass, FTransform(TargetLoc), nullptr, nullptr, ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
TempChild->FinishSpawning(FTransform(TargetLoc));
if (ADisPatcherChild* Child = Cast<ADisPatcherChild>(TempChild))
{
ADispatcherTest* SelfRef = const_cast<ADispatcherTest*>(this);
Child->InitFunction( SelfRef );
ChildrenArr.Add(Child);
}
}
//ParentActor->TurnLightDelegatePtr.BindSP(this, &ADisPatcherChild::TurnLight);
}
void ADispatcherTest::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Log, TEXT("Overlap Begin"));
TurnLightDelegatePtr.Broadcast();
//TurnLightDelegatePtr.ExecuteIfBound();