2Bbear's knowledge workshop

준비물 : UE4 엔진.



1. 간단한 게임 기획


너무 많은 기능을 넣고 싶지 않기에 간단하게 미로 푸는 게임으로 합니다.



- 맵


이정도로 퉁치고


- 목표

저 동그란 걸 모으는 거에요 하와와.


- 캐릭터

간단한게 최고에요. 바꿀 수도 있는데 굳이 지금은 할 필요가 없죠



2. 기능 정의

ㄱ. ui

- 먹은 코인 수 출력

- Retry 횟수 출력

- GameOver 로고 출력

- Game logo Scene 만들기

- Game ending Scene 만들기

ㄴ. 내부기능

- 코인에 닿으면 먹는 기능

- 닿은 후 코인 오브젝트 삭제 기능

- GameManager 기능

- 게임의 시작, 중지, 끝을 관리

- 현재 플레이어가 모은 코인을 관리 (예 최대 코인 수 12/0 현재 먹은 코인수)

- Scene관리


개발 순서는~~~ ui 하고 내부 기능 하고~ 해야하는 거지만 난 내맘데로 만들꺼야!


3.내부 기능을 만들자


- 코인에 닿으면 먹는 기능을 만들자!


먼저 코인의 뼈와 기둥이 되어줄 C++ 코드를 작성합니다.


먼저 코인!


 //ACoin.h

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


#pragma once

#include "CoreMinimal.h"

#include "GameFramework/Actor.h"


#include "Coin.generated.h"


UCLASS()

class HOWTOMAKEHTML5_API ACoin : public AActor

{

GENERATED_BODY()

private:

//Sets Componetns

UPROPERTY(VisibleAnywhere, Category = "Config")

class UBoxComponent * pBoxCollision;

UPROPERTY(VisibleAnywhere, Category = "Config")

class UStaticMeshComponent * pStaticMesh;


public:

// Sets default values for this actor's properties

ACoin();


protected:

// Called when the game starts or when spawned

virtual void BeginPlay() override;

public:

UFUNCTION()

void OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

};


 //ACoin.cpp

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


#include "Coin.h"

#include "Runtime/Engine/Classes/Components/BoxComponent.h"

#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"

#include "Kismet/GameplayStatics.h"

#include "HowToMakeHTML5GameMode.h"

#include <string>


// Sets default values

ACoin::ACoin()

{

  // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick = false;

pBoxCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("Boxcollision"));

pBoxCollision->SetGenerateOverlapEvents(true);


pStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));


}


// Called when the game starts or when spawned

void ACoin::BeginPlay()

{

Super::BeginPlay();

UE_LOG(LogTemp, Warning, TEXT("ACoin::BeginPlay"));


FScriptDelegate DelegateBegin;

DelegateBegin.BindUFunction(this, "OnOverlapBegin");

this->OnActorBeginOverlap.Add(DelegateBegin);



pBoxCollision->SetupAttachment(RootComponent);

pStaticMesh->SetupAttachment(pBoxCollision);

}


void ACoin::OnOverlapBegin(AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)

{

UE_LOG(LogTemp, Warning, TEXT("ACoint::OnOverlapBegin"));

//Processing Score

AHowToMakeHTML5GameMode* temp=(AHowToMakeHTML5GameMode*)GetWorld()->GetAuthGameMode();

temp->addPlayerScore();

std::string str= std::to_string(temp->getPlayerScore());


TCHAR ch[20];

const char* all = str.c_str();

int len = 1 + strlen(all);

wchar_t* t = new wchar_t[len];

if (NULL == t) throw std::bad_alloc();

mbstowcs(t, all, len);

_tcscpy_s(ch,t);


UE_LOG(LogTemp, Warning, ch);

//Delete this object

Destroy(this);


}


그 다음에는 score를 관리해주고 게임을 관리해주는 GameMode 찡!

//AHowToMakeHTML5GameMode.h

// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.


#pragma once


#include "CoreMinimal.h"

#include "GameFramework/GameModeBase.h"

#include "HowToMakeHTML5GameMode.generated.h"


UCLASS(minimalapi)

class AHowToMakeHTML5GameMode : public AGameModeBase

{

GENERATED_BODY()


public:

AHowToMakeHTML5GameMode();


private:

int playerScore;

public:

void setPlayerScore(int param);

int getPlayerScore();

void addPlayerScore();


};




 //AHowToMakeHTML5GameMode.cpp

// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.


#include "HowToMakeHTML5GameMode.h"

#include "HowToMakeHTML5Character.h"

#include "UObject/ConstructorHelpers.h"


AHowToMakeHTML5GameMode::AHowToMakeHTML5GameMode()

{

// set default pawn class to our Blueprinted character

static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));

if (PlayerPawnBPClass.Class != NULL)

{

DefaultPawnClass = PlayerPawnBPClass.Class;

}

}


void AHowToMakeHTML5GameMode::setPlayerScore(int param)

{

if(param<0)

{

throw param;

}

playerScore = param;

}


int AHowToMakeHTML5GameMode::getPlayerScore()

{

return playerScore;

}


void AHowToMakeHTML5GameMode::addPlayerScore()

{

playerScore += 1;

}




간단하게 Coin Actor는 뭔가와 부딪히면 그대로 현재 게임 모드를 불러와서 거기에 score를 수정합니다. 


간단하죠?


-GameMode의 동전관리

총 동전 수를 알아와야 하고 현재 동전수를 관리합니다.

// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.


#pragma once


#include "CoreMinimal.h"

#include "GameFramework/GameModeBase.h"

#include "HowToMakeHTML5GameMode.generated.h"


UCLASS(minimalapi)

class AHowToMakeHTML5GameMode : public AGameModeBase

{

GENERATED_BODY()


public:

AHowToMakeHTML5GameMode();


private:

int playerScore;

public:

void setPlayerScore(int param);

int getPlayerScore();

void addPlayerScore();


protected:

virtual void BeginPlay() override;

};




 // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.


#include "HowToMakeHTML5GameMode.h"

#include "HowToMakeHTML5Character.h"

#include "UObject/ConstructorHelpers.h"

#include "Runtime/Engine/Public/EngineUtils.h"

#include "Coin.h"

#include <string>


AHowToMakeHTML5GameMode::AHowToMakeHTML5GameMode()

{


// set default pawn class to our Blueprinted character

static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));

if (PlayerPawnBPClass.Class != NULL)

{

DefaultPawnClass = PlayerPawnBPClass.Class;

}


}


void AHowToMakeHTML5GameMode::setPlayerScore(int param)

{

if(param<0)

{

throw param;

}

playerScore = param;

}


int AHowToMakeHTML5GameMode::getPlayerScore()

{

return playerScore;

}


void AHowToMakeHTML5GameMode::addPlayerScore()

{

playerScore += 1;

}


void AHowToMakeHTML5GameMode::BeginPlay()

{

Super::BeginPlay();

TActorIterator< ACoin > ActorItr =TActorIterator< ACoin >(GetWorld());

int currentWorldCoinCount=ActorItr.GetProgressNumerator();

currentWorldCoinCount -=2;

if(currentWorldCoinCount <0)

{

currentWorldCoinCount = 0;

}


//for debug===========================================================

UE_LOG(LogTemp, Warning, TEXT("AHowToMakeHTML5GameMode::BeginPlay()"));


std::string str = std::to_string(currentWorldCoinCount);

TCHAR ch[20];

const char* all = str.c_str();

int len = 1 + strlen(all);

wchar_t* t = new wchar_t[len];

if (NULL == t) throw std::bad_alloc();

mbstowcs(t, all, len);

_tcscpy_s(ch, t);


UE_LOG(LogTemp, Warning, ch);


//==============================================================

}


로그로 현재 동전의 수를 나타내 줍니다.


그런데 월드에서 찾는 Coin이 2개더 잡히는데...어디서 잡히는거지?...이거 의문이네..


아무튼 이렇게 해서 Coin편 끝.






Comment +1