1. Day20

  • C++ 문법 학습
  • 알고리즘 코드카타
  • C++ 레벨 테스트
  • CH2 학습 가이드 - 12/24
  • 과제 5 : Unreal Engine C++ 활용 프로그램 제작

2. C++ 문법 학습(Day20)

  • 게임 개발자를 위한 C++ 문법(3-2)
    • 언리얼 엔진 C++ 기본개념
  • 알고리즘 코드카타
    • 나이 출력, 숫자 비교하기, 두 수의 합
  • C++ 레벨 테스트
  • 과제 5 : Unreal Engine C++ 활용 프로그램 제작(도전 기능 완성)
    • MyActor.h/cpp
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class NBC_05_API AMyActor : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    AMyActor();
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;
    UFUNCTION()
    void RandMove();
    UFUNCTION()
    void RandRotate();
    UFUNCTION()
    int TriggerEventWithProbability(float Probability);
private:
    UPROPERTY()
    // 엑터의 회전
    FRotator ActorRotation;
    UPROPERTY()
    // 엑터의 위치
    FVector ActorLocation;

};

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"
#include "Engine/Engine.h"

// Sets default values
AMyActor::AMyActor()
{
     // 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;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
    Super::BeginPlay();

    int32 RandomValue = FMath::RandRange(1, 100);
    int32 Probability = 50;
    float TotalDistance = 0.f;
    int32 EventCount = 0;

    if (IsValid(GEngine))
    {
        GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Black, 
            FString::Printf(TEXT("시작 위치 X = %f, Y = %f"), GetActorLocation().X, GetActorLocation().Y));
    }

    for (int i = 0; i < 10; i++)
    {
        if (RandomValue <= Probability)
        {
            FVector PrevLocation = GetActorLocation();
            RandMove();
            TotalDistance += FVector::Dist(PrevLocation, GetActorLocation());

            if (IsValid(GEngine))
            {
                GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Black, 
                    FString::Printf(TEXT("%d 번째, 위치 X = %f, Y = %f"), i + 1, GetActorLocation().X, GetActorLocation().Y));
            }
        }
        else
        {
            RandRotate();
            if (IsValid(GEngine))
            {
                GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Black, 
                    FString::Printf(TEXT("%d 번째, Yaw = %f°, Pitch = %f°"), i + 1, GetActorRotation().Yaw, GetActorRotation().Pitch));
            }
        }

        EventCount += TriggerEventWithProbability(50);
        RandomValue = FMath::RandRange(1, 100);

    }

    if (IsValid(GEngine))
    {
        GEngine->AddOnScreenDebugMessage(-1, 50.0f, FColor::Green, 
            FString::Printf(TEXT("총 이동거리: %f, 이벤트 발생: %d회"), TotalDistance, EventCount));
    }

}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

void AMyActor::RandMove()
{
    FVector RandomLocation;
    RandomLocation.X = FMath::RandRange(-100.f, 100.f);
    RandomLocation.Y = FMath::RandRange(-100.f, 100.f);

    SetActorLocation(GetActorLocation() + RandomLocation);
}


void AMyActor::RandRotate()
{
    FRotator RandomRotation;
    RandomRotation.Yaw = FMath::RandRange(0.f, 360.f);
    RandomRotation.Pitch = FMath::RandRange(0.f, 360.f);

    SetActorRotation(GetActorRotation() + RandomRotation);
}

int AMyActor::TriggerEventWithProbability(float Probability)
{
    int32 RandomValue = FMath::RandRange(1, 100);

    if (RandomValue <= Probability)
    {
        if (IsValid(GEngine))
        {
            GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, 
                TEXT("Event Triggered!"));
        }
        return 1;
    }
    else
    {
        if (IsValid(GEngine))
        {
            GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, 
                TEXT("Event Not Triggered"));
        }
        return 0;
    }
}

3. 내일 계획 : 크리스마스 휴일

+ Recent posts