1. 필수 기능 구현
▸ 문제


▸ 완성 코드
#include "MyActor.h"
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = true;
}
void AMyActor::BeginPlay()
{
Super::BeginPlay();
start = { 0, 0 };
FVector Location = { start.X, start.Y, 100.f };
SetActorLocation(Location);
UE_LOG(LogTemp, Display, TEXT("{ %d, %d }"), (int32) start.X, (int32)start.Y);
for (int i = 0; i < 10; i++)
{
move();
}
}
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor::move()
{
FVector2D NextLocation = { start.X + step(), start.Y + step() };
start = NextLocation;
FVector Location = { NextLocation.X, NextLocation.Y, 100.f };
SetActorLocation(Location);
UE_LOG(LogTemp, Display, TEXT("{ %d, %d }"), (int32)NextLocation.X, (int32)NextLocation.Y);
}
int32_t AMyActor::step()
{
return FMath::RandRange(0,1);
}
2. 도전 기능 구현
▸ 문제


▸ 완성 코드
#include "MyActor.h"
AMyActor::AMyActor()
{
PrimaryActorTick.bCanEverTick = true;
}
void AMyActor::BeginPlay()
{
Super::BeginPlay();
start = { 0, 0 };
FVector Location = { start.X, start.Y, 100.f };
SetActorLocation(Location);
UE_LOG(LogTemp, Display, TEXT("Initial location : { %d, %d }"), (int32)start.X, (int32)start.Y);
for (int i = 0; i < 10; i++)
{
move();
}
UE_LOG(LogTemp, Display, TEXT("event cnt : %d, total distance : %.3f"), evCnt, totDist);
}
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActor::move()
{
if (createEvent())
{
evCnt++;
UE_LOG(LogTemp, Warning, TEXT("event occurred!!"));
FVector2D NextLocation = { start.X + step(), start.Y + step() };
UE_LOG(LogTemp, Display, TEXT("move { %d, %d }"), (int32)NextLocation.X, (int32)NextLocation.Y);
float dist = distance(start, NextLocation);
UE_LOG(LogTemp, Display, TEXT("move distance: %.2f"), dist);
totDist += dist;
FVector Location = { NextLocation.X, NextLocation.Y, 100.f };
SetActorLocation(Location);
start = NextLocation;
}
else
UE_LOG(LogTemp, Warning, TEXT("event fail!!"));
}
int32_t AMyActor::step()
{
return FMath::RandRange(0, 1);
}
float AMyActor::distance(FVector2D first, FVector2D second)
{
float dx = fabs(first.X - second.X);
float dy = fabs(first.Y - second.Y);
return sqrt(dx * dx + dy * dy);
}
int32 AMyActor::createEvent()
{
return FMath::RandRange(0, 1);
}