[UE5 / C++] ๐Ÿ‘ฟ ์  ๋งŒ๋“ค๊ธฐ (Feat. FSM) (3)

Singery00ยท2024๋…„ 4์›” 29์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
13/20
post-thumbnail

๊ฐœ์š”

๐Ÿ’ก C++๋กœ ์ ์„ ๋งŒ๋“ค์–ด๋ณด์ž. ๋˜ํ•œ ์œ ํ•œ์ƒํƒœ๊ธฐ๊ณ„๋ฅผ ํฌํ•จํ•˜์—ฌ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.

์ด๋ฒˆ์—๋Š” ๋งŒ๋“ค์–ด์ง„ ์ ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ฐฐ์น˜ํ•˜๋Š”๊ฒŒ ์•„๋‹Œ, ์ž๋™์ƒ์„ฑ์ด ๋˜๋Š” Enemy Manager๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๋ณธ๋ก 


EnemyManager ํด๋ž˜์Šค ์ƒ์„ฑ

Enemy Manger๋ฅผ ์œ„ํ•œ C++ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

C++ํด๋ž˜์Šค ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๊ณ ,

์•กํ„ฐ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.

์ด๋ฆ„์€ EnemyManager๋กœ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

SpawnSettings ๋ณ€์ˆ˜ ์ถ”๊ฐ€

์  ์ƒ์„ฑ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋“ค์„ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

  1. ์ƒ์„ฑ ์ตœ์†Œ ์‹œ๊ฐ„
  2. ์ƒ์„ฑ ์ตœ๋Œ€ ์‹œ๊ฐ„
  3. ์ƒ์„ฑ ์ง€์ 
  4. ์ƒ์„ฑ ์•กํ„ฐ
public:
	UPROPERTY(EditAnywhere,Category=SpawnSettings)
	float minTime = 1;
	
	UPROPERTY(EditAnywhere, Category = SpawnSettings)
	float maxTime = 5;

	UPROPERTY(EditAnywhere, Category = SpawnSettings)
	TArray<class AActor*> spawnPoints;

	UPROPERTY(EditAnywhere, Category = SpawnSettings)
	TSubclassOf<class AEnemy> enemyFactory;

ํƒ€์ด๋จธ ํ™œ์šฉ

์ง€๊ธˆ๊นŒ์ง€ currentTime์ด๋ž€ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ DeltaTime์„ ๋ˆ„์ ํ•ฉํ•˜์—ฌ ์‹œ๊ฐ„์„ ์ฒดํฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ด๋ฒˆ์—๋Š” ์–ธ๋ฆฌ์–ผ์—์„œ ์ œ๊ณตํ•˜๋Š” ํƒ€์ด๋จธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ณ€์ˆ˜ ์„ ์–ธ

์›”๋“œ์— ํ•˜๋‚˜ ์žˆ๋Š” ๊ธ€๋กœ๋ฒŒ ํƒ€์ด๋จธ ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๊ธ€๋กœ๋ฒŒ ํƒ€์ด๋จธ ๋งค๋‹ˆ์ €์—๊ฒŒ ์•Œ๋žŒ์‹œ๊ณ„(FTimerHandle)์„ ๋„˜๊ฒจ ์ค€ ํ›„,
์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ํ•จ์ˆ˜๋‚˜ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ํ†ตํ•ด ์•Œ๋ฆผ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €ํฌ๋Š” CreateEnemy()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public:
	// ์ƒ๋žต

	FTimerHandle spawnTimerHandle;
	void CreateAlarm();
	void CreateEnemy();

์ƒ์„ฑ์ž Tick ์„ค์ • ๋ณ€๊ฒฝ

Tick()์œผ๋กœ ์‹œ๊ฐ„์„ ๋ฐ›์ง€ ์•Š๊ณ , ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ๋„ฃ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์•„๋ฌด ๋™์ž‘์„ ํ•˜์ง€ ์•Š๋Š”๋ฐ Tick()์ด ๊ณ„์†ํ•ด์„œ ํ˜ธ์ถœ๋˜๋Š”๊ฑด ์ž์› ๋‚ญ๋น„์ž…๋‹ˆ๋‹ค.

Tick()์ด ํ˜ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

AEnemyManager::AEnemyManager()
{
	// false๋กœ ๋ณ€๊ฒฝ
	PrimaryActorTick.bCanEverTick = false;
}

CreateAlarm()

minTime๊ณผ maxTime ์‚ฌ์ด์˜ ๋žœ๋ค ๊ฐ’์„ ์ƒ์„ฑ ์‹œ๊ฐ„์œผ๋กœ ์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

void AEnemyManager::CreateAlarm()
{
	float createTime = FMath::RandRange(minTime, maxTime);

	GetWorld()->GetTimerManager().SetTimer(spawnTimerHandle, this, &AEnemyManager::CreateEnemy, createTime);
}

BeginPlay()์—์„œ ํ˜ธ์ถœ

์ตœ์ดˆ๋กœ ๊ฒŒ์ž„์ด ์‹œ์ž‘ํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ BeginPlay()์—์„œ CreateAlarm()์„ ํ•œ๋ฒˆ ํ˜ธ์ถœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

void AEnemyManager::BeginPlay()
{
	Super::BeginPlay();
	
	CreateAlarm();
}

CreateEnemy()

Enemy.h ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.
๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ ์ง€์ ์„ ๋žœ๋ค์œผ๋กœ ์„ ํƒํ•œ ํ›„,
ํ•ด๋‹น ์œ„์น˜์— ์ƒ์„ฑํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Enemy๋ฅผ ๋ชป์ฐพ์€ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋„ ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ๋œ Enemy์—๊ฒŒ Controller๋ฅผ ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

void AEnemyManager::CreateEnemy()
{
	int index = FMath::RandRange(0, spawnPoints.Num() - 1);

	AEnemy* newEnemy = GetWorld()->SpawnActor<AEnemy>(enemyFactory, spawnPoints[index]->GetActorLocation(), FRotator(0));
	if (newEnemy != nullptr)
	{
		newEnemy->SpawnDefaultController();
		CreateAlarm();
	}
}

BP_EnemySpawnPoint

์Šคํฐ ์ง€์  ์•กํ„ฐ๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

์•กํ„ฐ๋กœ BP๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.

๋ ˆ๋ฒจ์—์„œ ๋ฐฐ์น˜ํ•  ๋•Œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก Sphere๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ธ๊ฒŒ์ž„ ํ”Œ๋ ˆ์ด ์‹œ์—๋Š” ๋ณด์ด์ง€ ์•Š๋„๋ก Hidden in Game์€ True๋กœ ํ•ด์ฃผ์„ธ์š”.

๋˜ํ•œ Collision์€ NoCollision์œผ๋กœ ์ถฉ๋Œ์„ ๊บผ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.
๋‹จ์ˆœํžˆ ์œ„์น˜๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•˜๋‹ˆ๊นŒ์š”.

๋ ˆ๋ฒจ์— ์ ๋‹นํžˆ ๋ฐฐ์น˜๋ฅผ ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

EnemyManager ์„ค์ •

๋ ˆ๋ฒจ์— EnemyManager Class๋ฅผ ๋Œ์–ด์„œ ๋ฐฐ์น˜ํ•ด์ฃผ์„ธ์š”.

์•„์›ƒ๋ผ์ด๋„ˆ์—์„œ ๋ฐฐ์น˜๋œ ๊ฒƒ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.

๊ทธ๋ฆฌ๊ณ  Details ์ฐฝ์—์„œ Spawn Points๋ฅผ ์ถ”๊ฐ€ํ•ด์ค์‹œ๋‹ค.

โ—๊ธฐ์กด ์ง์ ‘ ๋ฐฐ์น˜ํ•œ BP_Enemy๋Š” ์‚ญ์ œํ•ด์ฃผ์„ธ์š”โ—

โ—์ปดํŒŒ์ผ ์ €์žฅโ—ํ›„ ํ”Œ๋ ˆ์ดํ•ด๋ณด๋ฉด

์Šคํฐ์ง€์ ์—์„œ ๋ฟ… ๋ฟ… ํ•˜๋‚˜์”ฉ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

๊ทธ๋Ÿฐ๋ฐ ์Šคํฐ ์ง€์ ์„ ํ•˜๋‚˜์”ฉ ์ง€์ •ํ•ด์ฃผ๋Š”๊ฑด ์—ฌ๊ฐ„ ๊ท€์ฐฎ์€ ์ž‘์—…์ด ์•„๋‹™๋‹ˆ๋‹ค.

๋‹ค์Œ ํฌ์ŠคํŒ…์—๋Š” EnemyManager๊ฐ€ ์ง์ ‘ ๋ ˆ๋ฒจ์—์„œ Spawn Point๋“ค์„ ์ฐพ์•„์„œ ํ• ๋‹นํ•˜๋Š” ์ž๋™ํ™” ์ž‘์—…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€