GameManager의 관리
르탄 만들기에서 배운 내용은 기본적으로 설정을 적용하고 이를 각 스크립트에 적용하게 하기 위한 방법을 배웠다.
백그라운드와 그라운드는 기본적으로 고정형으로 스크립트를 짤 필요가 없었고
enemy 역할인 rain의 경우 prefabs를 만들어 관리 한다.
또한 player 역할인 르탄과 다시 게임을 초기화 시켜주기 위한 패널을 제외하고 모든 게임 설정을 GameManager에서 싱글톤 작성하고 관리했다.
다만 르탄과 rain의 경우 rigidbody 2D를 이용하여 물리력을 적용하였다.
player인 르탄의 함수는
float direction = 1f;
float toward = 1.0f;
public float moveSpeed = 1f;
방향과 속도를 변수로 설정해 주고 업데이트 함수를 통하여 버튼을 누르면 방향을 바꾸도록 설정하였다.
void Update()
{
if ( Input.GetMouseButtonDown(0))
{
toward *= -1;
direction *= -1;
}
if (transform.position.x > 2.8f)
{
direction = -1f;
toward = -1.0f;
} else if (transform.position.x < -2.8f)
{
direction += 1f;
toward = 1.0f;
}
transform.localScale = new Vector3(toward, 1, 1);
transform.position += new Vector3(direction, 0, 0) * moveSpeed * Time.deltaTime;
}
포지션을 변경하여 위치를 변경시키게 하였고 로컬 스케일을 통해 방향을 변하게 만들고 크기를 고정시켰다.
게임 매니저에서 모든걸 관리하는데
public static GameManager I;
public Text scoreText;
int totalScore;
public float limit = 60f;
public Text timeText;
void Awake()
{
I = this;
}
public GameObject Rain;
public GameObject Panel;
public float CreateRain = 0.5f;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("makeRain", 0, CreateRain);
initGame();
}
void makeRain()
{
Instantiate(Rain);
}
public void addScore(int score)
{
totalScore += score;
scoreText.text = totalScore.ToString();
}
// Update is called once per frame
void Update()
{
limit -= Time.deltaTime;
if (limit < 0) {
Time.timeScale = 0.0f;
Panel.SetActive(true);
limit = 0f;
}
timeText.text = limit.ToString("N2");
}
public void retry() {
SceneManager.LoadScene("MainScene");
}
void initGame()
{
Time.timeScale = 1.0f;
totalScore = 0;
limit = 30.0f;
}
싱글톤을 이용하여 유일한 게임매니저로 작성하였고 택스트와 시간을 관리하였다
게임오브젝트를 직접 불러오는것도 게임매니저를 통해 완성하였다.
Awake 함수로 싱글톤 게임 매니저를 초기화 시키고
Start 함수에서 invokeRepeating 함수를 통해 적의 탄생을 관리하고 재시작시 초기화도 진행한다.
적 함수를 가져오고 그걸 함수로 만들어 invokeRepeating을 이용하였다.