using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
#region instance
public static GameManager instance;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
}
#endregion
public float gameSpeed = 1;
}
GroundScroller 이랑 MobBase 의 speed를
GameManager.instance.gameSpeed
로 통일
public void PlayBtn()
{
playBtn.SetActive(false);
isPlay = true;
}
GroundScroller, PlayerController, MobBase에 조건문 넣기(isPlay가 참일때)
public delegate void OnPlay();
public OnPlay onPlay;
private void Start()
{
GameManager.instance.onPlay += PlayGame;
}
void PlayGame()
{
StartCoroutine(CreateMob());
}
IEnumerator CreateMob()
{
while (GameManager.instance.isPlay)
{
MobPool[DeactiveMob()].SetActive(true);
yield return new WaitForSeconds(Random.Range(1f, 3f));
}
}
delegate, Invoke
public delegate void OnPlay(bool isPlay);
delegate bool 인자 넣어주기
public void PlayBtnClick()
{
playBtn.SetActive(false);
isPlay = true;
onPlay.Invoke(isPlay);
}
public void GameOver()
{
playBtn.SetActive(true);
isPlay = false;
onPlay.Invoke(isPlay);
}
void PlayGame(bool isPlay)
{
if (isPlay)
StartCoroutine(CreateMob());
else
StopAllCoroutines();
}
isPlay 인자 추가
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Mob"))
{
GameManager.instance.GameOver();
}
}
IsTrigger 체크
Rigidbody 2D 추가
Body Type : Kinematic
void PlayGame(bool isPlay)
{
if (isPlay)
{
// 시작 전 장애물 비활성화
for (int i = 0; i < MobPool.Count; i++)
{
if (MobPool[i].activeSelf)
MobPool[i].SetActive(false);
}
StartCoroutine(CreateMob());
}
else
StopAllCoroutines();
}
IEnumerator CreateMob()
{
yield return new WaitForSeconds(0.5f);
while (GameManager.instance.isPlay)
{
MobPool[DeactiveMob()].SetActive(true);
yield return new WaitForSeconds(Random.Range(1f, 3f));
}
}
플레이 버튼 누르면 화면에 나와있는 몹 비활성화