1. Day38
- 알고리즘 코드카타
- C++과 Unreal Engine으로 3D 게임 개발
- 스탠다드 클래스
- Collision & Trace 세션
2. CH3 - C++과 Unreal Engine으로 3D 게임 개발(Day38)
- Chapter 3 C++과 Unreal Engine으로 3D 게임 개발(2-3)
- [7번 과제] Pawn 클래스로 3D 캐릭터 만들기
- 코드 정리 및 최적화
- SetSimulatePhysics 생성자에서 한번만 처리, Input값 const 활용(수정방지)
// Sets default values
AMyCharacter::AMyCharacter()
{
// Set this pawn to call Tick() every frame.
PrimaryActorTick.bCanEverTick = true;
// 캡슐(루트 컴포넌트)
CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComp"));
RootComponent = CapsuleComp;
CapsuleComp->SetSimulatePhysics(false);
// 메시
MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
MeshComp->SetupAttachment(RootComponent);
MeshComp->SetSimulatePhysics(false);
// 스프링 암
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
SpringArmComp->SetupAttachment(RootComponent);
// 카메라
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
CameraComp->SetupAttachment(SpringArmComp);
}
void AMyCharacter::Move(const FInputActionValue& Value)
{
// 키보드 입력값 X, Y 2D 벡터값으로 저장
const FVector2D MoveInput = Value.Get<FVector2D>();
// Tick밖에서 DeltaTime을 쓰는 방식
const float DeltaTime = GetWorld()->GetDeltaSeconds();
// 로그 출력
if (IsValid(GEngine))
{
GEngine->AddOnScreenDebugMessage(-1, 0.f, FColor::Red, FString::Printf(TEXT("X: %f, Y: %f"), MoveInput.X, MoveInput.Y));
}
// Move 구현
const FVector Movement = FVector(MoveInput.X, MoveInput.Y, 0.f) * MoveSpeed * DeltaTime;
AddActorLocalOffset(Movement);
}
void AMyCharacter::Look(const FInputActionValue& Value)
{
// 마우스 입력값 X, Y 2D 벡터값으로 저장
const FVector2D MouseInput = Value.Get<FVector2D>();
// Tick밖에서 DeltaTime을 쓰는 방식
const float DeltaTime = GetWorld()->GetDeltaSeconds();
// Look 구현
// YawRotation 적용
const FRotator YawRotation = FRotator(0.f, MouseInput.X * LookSensitivity * DeltaTime, 0.f);
AddActorLocalRotation(YawRotation);
//PitchRotation 적용 Pawn은 그대로 SpringArmComp의 Pitch값만 회전
FRotator ArmRotation = SpringArmComp->GetRelativeRotation();
ArmRotation.Pitch = FMath::Clamp(ArmRotation.Pitch - MouseInput.Y * LookSensitivity * DeltaTime, -80.f, 80.f);
SpringArmComp->SetRelativeRotation(ArmRotation);
// 로그 출력
if (IsValid(GEngine))
{
GEngine->AddOnScreenDebugMessage(-1, 0.f, FColor::Red, FString::Printf(TEXT("X: %f, Y: %f"), MouseInput.X, MouseInput.Y));
}
}
3. 알고리즘 코드카타
- 3진법 뒤집기(초기 코드)
- 입력 갑 n을 3진법으로 바꾸는 과정에 sting으로 변환(자동으로 뒤집힘) 다시 for문을 이용해 10진법으로 변환
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
string s;
int answer;
while (n != 0)
{
int temp = n % 3;
s.push_back(temp + '0');
n /= 3;
}
int base = 1;
for (int i = s.size() - 1; i >= 0; --i)
{
answer += (s[i] - '0') * base;
base *= 3;
}
return answer;
}
- 3진법 뒤집기(개선 코드)
- string을 stoi로 다시 10진법으로 int값으로 바꾸는 과정에서 stoi함수의 변환 기능이용
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
string s;
int answer;
while (n != 0)
{
int temp = n % 3;
s.push_back(temp + '0');
n /= 3;
}
answer = stoi(s, nullptr, 3);
return answer;
}
- 이상한 문자 만들기(초기 코드)
- count가 문자열의 인덱스로 1부터 시작해 count % 2 ≠ 0(홀수) 이면 대문자 짝수이면 소문자로 변환 공백을 만나면 1로 초기화
- ctype.h 헤더를 가져와 홀수면 toupper() 함수로 대문자 변환 짝수면 tolower()함수로 소문자 변환
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
string solution(string s) {
string answer = "";
int count = 1;
for (auto& c : s)
{
if (count % 2 != 0)
{
answer.push_back(toupper(c));
}
else
{
answer.push_back(tolower(c));
}
count++;
if (c == ' ')
{
count = 1;
}
}
return answer;
}
- 이상한 문자 만들기(개선 코드)
- string answer 를 따로 두지않고 입력받은 s 문자열을 수정한 다음 s 를 반환
- 기존의 공백 체크, 문자열 수정 입력, count 수정 등을 매번 다 하지않고 공백이라면 count = 0만 수행 나머지는 else안에 구현해 넘어감
- else 부분의 문자열의 홀수 짝수 문자를 대소문자 변환하여 다시 입력해주는 코드를 삼항연산자 c = (count % 2 == 0) ? toupper(c) : tolower(c)로 수정해도 코드 자체는 줄어드나 명시적으로 표현해주는 것이 좋을 것 같아 유지함
- 문자에서 문자열의 0번째는 짝수 취급이라 대문자 변환이라 했으니 조건을 다시 수정하고 count = 0 부터 시작하도록 수정
#include <string>
#include <vector>
#include <cctype>
using namespace std;
string solution(string s) {
int count = 0;
for (auto& c : s)
{
if (c == ' ')
{
count = 0;
}
else
{
if (count % 2 == 0)
{
c = toupper(c);
}
else
{
c = tolower(c);
}
count++;
}
}
return s;
}
4. Collision & Trace 세션
- Collision & Trace 기초
- 라인트레이스(발사), 한 점 판정(아이템 인터랙트) 구현
5. 스탠다드 클래스
6. 내일 계획 : CH3 강의 학습, 알고리즘 문제 집중 CS50x 2주차 진행, 과제6 & 과제7 제출