1. Day35

  • 알고리즘 코드카타
  • C++과 Unreal Engine으로 3D 게임 개발(6번 과제)
  • CS50x 1주차 마무리

2. CH3 - C++과 Unreal Engine으로 3D 게임 개발(Day35)

  • [6번 과제] 회전 발판과 움직이는 장애물
    • 필수 기능 + 도전 기능 일부분 구현
void AMovingPlatform::BeginPlay()
{
    Super::BeginPlay();

    // 초기 위치 세팅(블루프린트에서 설정) == 초기위치에서 Dist값 계산 후 MaxRange를 넘어가면 방향 전환
    SetActorLocation(StartLocation);
    // 초기 Acotr 각도 세팅
    SetActorRotation(StartRotation);
    // 초기 이동 방향 세팅 == 엑터의 정면
    MoveDirection = GetActorForwardVector();
    // FTimerHandle을 활용한 코드
    GetWorld()->GetTimerManager().SetTimer(
        TimerHandle,
        this,
        &AMovingPlatform::MovePlatform,
        0.016f,
        true
    );
}

void AMovingPlatform::MovePlatform()
{
    // 현재 위치 & 움직일 위치 선정
    FVector CurrentLocation = GetActorLocation();
    FVector NewLocation = CurrentLocation + (MoveDirection * MoveSpeed * 0.016f);
    // Platform 이동
    SetActorLocation(NewLocation);
    // 이동 거리 계산
    float Dist = FVector::Dist(StartLocation, NewLocation);
    // Actor의 정면 방향으로 진행 중 최대 거리를 넘기면 방향 반대로 전환
    if (bMovingForward && Dist >= MaxRange)
    {
        MoveDirection = -MoveDirection;
        bMovingForward = false;
    }
    // 방향을 반대로 전환해 초기 위치에 근접하게 된다면 다시 방향 전환
    else if (bMovingForward == false && Dist <= 1.0f )
    {
        MoveDirection = -MoveDirection;
        bMovingForward = true;
    }

    if (IsValid(GEngine))
    {
        FString DebugMsg = FString::Printf(TEXT("Distance: %f / Dir: %s"), Dist, *MoveDirection.ToString());
        GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Red, DebugMsg);
    }
}
void ARotatingPlatform::BeginPlay()
{
    Super::BeginPlay();
    // 초기 위치 세팅(블루프린트에서 설정)
    SetActorLocation(StartLocation);
    // 초기 Acotr 각도 세팅
    SetActorRotation(StartRotation);

    // FTimerHandle을 활용한 코드
    GetWorld()->GetTimerManager().SetTimer(
        TimerHandle,
        this,
        &ARotatingPlatform::RotatePlatform,
        0.016f,
        true
    );
}

void ARotatingPlatform::RotatePlatform()
{
    // Platform 이동
    AddActorLocalRotation(RotateSpeed * 0.016f);
}

3. 알고리즘 코드카타

  • 수박수박수박수박수박수?
  • 내적(초기 코드)
    • 배열의 길이 만큼 반복문에서 a의 값과 b 의값을 곱한 값을 answer에 더해줌
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> a, vector<int> b) {
    int answer = 0;
    for (int i = 0; i < a.size(); ++i)
    {
        answer += a[i] * b[i];
    }
    return answer;
}
  • 내적(개선 코드)
    • numeric 헤더를 가져와 내적을 구하는 함수인 inner_product를 사용
#include <string>
#include <vector>
#include <numeric>

using namespace std;

int solution(vector<int> a, vector<int> b) {
    int answer = inner_product(a.begin(), a.end(), b.begin(),0);

    return answer;
}

4. 다음주 계획 : CH3 강의 학습, 알고리즘 문제 집중 CS50x 2주차 시작, 과제6 & 과제7 마무리

+ Recent posts