최종 프로젝트 개발 일지 #2
GameScene에서 위 그림처럼 [ Round (숫자) : (초) s ] 형식의 UI를 좌측 상단에 띄웠다.
우리 게임의 GameScene 흐름은 다음과 같다.
Start => 1라운드 파밍페이즈 - 1라운드 배틀 페이즈
if 누군가의 점수가 1이라면 => 2라운드 파밍페이즈 - 2라운드 배틀 페이즈
if 누군가의 점수가 2라면 => Game Over
else if 1대1이라면 => 3라운드 파밍페이즈 - 3라운드 배틀 페이즈 => Game Over
점수에 따라서 라운드가 다음으로 진행되고, 파밍페이즈와 배틀 페이즈의 타이머가 따로 분리되어 있는 형태이다.
// RoundTimer.cs
IEnumerator FarmingTimer()
{
//yield return new WaitForSeconds(1.0f); 화면 전환을 위해 잠깐 기다림
farmingTime = 6;
curTime = farmingTime;
while (curTime > 0)
{
curTime -= Time.deltaTime;
second = (int)curTime % 60;
text.text = second.ToString("0");
yield return null;
if (curTime <= 0)
{
curTime = battleTime;
StartBattleTimer();
yield break;
}
}
}
IEnumerator BattleTimer()
{
//yield return new WaitForSeconds(1.0f);
battleTime = 4;
curTime = battleTime;
while (curTime > 0)
{
curTime -= Time.deltaTime;
second = (int)curTime % 60;
text.text = second.ToString("0");
yield return null;
if (curTime <= 0)
{
farmingTime = 8;
curTime = farmingTime;
StartFarmingTimer();
yield break;
}
}
}
타이머는 두 개의 코루틴으로 분리시킬 뿐, 내부 코드는 같다.
타이머는 달라도 흐르는 시간 (시간 그 자체값)은 같기 때문에 curTime이라는 변수를 공통으로 사용하지만, 코루틴 내부에서 farmingTime과 battleTime으로 명확히 구분해야한다.
타이머를 작동시키기 위해 바깥에 메서드를 만들어주었다.
private void Awake()
{
roundLogic.RoundIndex();
curTime = farmingTime;
StartCoroutine (FarmingTimer());
}
void StartFarmingTimer()
{
int scoreRed;
int scoreBlue;
int.TryParse(TeamRedScore.text, out scoreRed);
int.TryParse(TeamBlueScore.text, out scoreBlue);
if (scoreRed == 1 || scoreBlue == 1)
{
roundLogic.RoundIndex();
StopCoroutine(BattleTimer());
StartCoroutine(FarmingTimer());
}
else if (scoreRed == 1 && scoreBlue == 1)
{
roundLogic.RoundIndex();
StopCoroutine(BattleTimer());
StartCoroutine(FarmingTimer());
}
if ((scoreRed == 2) || (scoreBlue == 2))
{
roundLogic.RoundIndex();
roundLogic.GameOver();
StopAllCoroutines();
}
}
void StartBattleTimer()
{
if (curTime == battleTime)
{
StopCoroutine(FarmingTimer());
StartCoroutine(BattleTimer());
}
}
우선 Scene이 시작될 때 FarmingTimer로 시작할 수 있도록 설정했다.
이후 FarmingTimer의 시간이 다 되면 StartBattleTimer를 실행시킨 뒤 break하고, BattleTimer가 시작된다.
마찬가지로 BattlerTimer의 시간이 다 되면 StartFarmingTimer를 실행시킨 뒤 break하고 FarmingTimer가 시작된다.
'게임 개발 일지 > 내일배움캠프 TIL' 카테고리의 다른 글
[C#] 델리게이트와 이벤트, action과 func (1) | 2024.02.01 |
---|---|
[Unity] 게임 진행에 따라 round 표시하기 (1) | 2024.01.31 |
[C#] 인터페이스와 추상클래스 / 스택과 큐의 차이 (1) | 2024.01.26 |
[Unity] 같은 scene 에서 오브젝트 간 생성 순서 정하기 (1) | 2024.01.25 |
[C#] 자료구조의 차이점 / 제네릭 (1) | 2024.01.24 |
댓글