이번엔 게임의 전체적인 관리를 담당하는 factory스크립트를 분석해보자
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // UI관련 코드를 사용하려면 필수
using UnityEngine.SceneManagement; // Scene을 관리할 때 필요한 코드
public class factory : MonoBehaviour
{
public GameObject prefab;
public GameObject prefab2;
IEnumerator t;
public int total = 0;
public int total2 = 0;
public Button qr;
public Button check;
public movePlayer movePlayer;
public movePlayer2 movePlayer2;
public Text Ctext;
public Text Vtext;
public Button spdUP;
public Button Vaccine;
public movePlayer p1;
public movePlayer p2;
public movePlayer p3;
public movePlayer2 t1;
public movePlayer2 t2;
public movePlayer2 t3;
public Image vaccineBar;
public bool flag = true;
IEnumerator g;
public float speed = 1;
int Coin = 0;
float vaccine = 0.0f;
// 각종 변수 설정 너무 많아 쓰일 때 설명 하겠다.
public static factory Instance
{
get
{
if (instance == null) instance = FindObjectOfType<factory>();
return instance;
}
}
private static factory instance;
// factory에 있는 public 변수들을 다른 스크립트에서도 사용 할수있게 하는 명령
void Start()
{
t = Test(); // Test() 함수의 생성자
StartCoroutine(t); // Test() 실행
}
void Update()
{
if (total2 < 4)
{
qr.GetComponent<Button>().interactable = true;
}
else
{
qr.GetComponent<Button>().interactable = false;
}
if(flag==true && Coin >= 50)
{ // Coin이 50이상이면 상점창에 있는 속도를 증가시키는 버튼이 활성화 되고 한번 클릭하면 더이상 쓰지 못하게 설정
spdUP.GetComponent<Button>().interactable = true; // 버튼 활성화
}else
{
spdUP.GetComponent<Button>().interactable = false; // 버튼 비활성화
}
if (Coin >= 200)
{ // Coin이 200 이상일때만 백신을 구입하는 버튼이 활성화
Vaccine.GetComponent<Button>().interactable = true; // 버튼 활성화
}
else
{
Vaccine.GetComponent<Button>().interactable = false; // 버튼 비활성화
}
vaccineBar.fillAmount = vaccine; // 백신바 이미지를 백신 수치에 맞게 점차 채운다
if (vaccine >= 1)
{
SceneManager.LoadScene("GameOver");
} // 백신게이지를 모두 채우면 GameOver신을 불러온다.
}
IEnumerator Test()
{ // 코루틴 함수 Test()
while(true)
{
if(total < 4)
{ // 플레이어 수가 4명 이상 생성되지 않게 설정
Instantiate(prefab, new Vector3(-9, 0.8f, 0), Quaternion.identity); // 지정한 위치에서 플레이어가 복제 되게 한다
total += 1;
yield return new WaitForSeconds(5f);
}
yield return null;
}
}
public void QRActive()
{ // 이 게임에서 가장 중요한 함수 생성!!
qr.gameObject.SetActive(!qr.gameObject.activeSelf); // qr버튼이 보이다가 누르면 안보이게 설정
if (movePlayer != null)
{
Destroy(movePlayer.gameObject); // 버튼을 눌렀을 때 플레이어가 사라지게 설정
movePlayer[] units = GameObject.FindObjectsOfType<movePlayer>();
foreach (movePlayer unit in units)
{
if (unit.gameObject.transform.position.x < -4.6f && unit.gameObject.transform.position.x > -4.8f)
{
p1 = unit;
}
else if (unit.gameObject.transform.position.x < -5.4f && unit.gameObject.transform.position.x > -5.6f)
{
p2 = unit;
}
else if (unit.gameObject.transform.position.x < -6.3f && unit.gameObject.transform.position.x > -6.4f)
{
p3 = unit;
}
} // 플레이어들이 쌓여서 줄이 생길 때 위치에 따른 번호를 받는 코드
g = Go(); // Go()함수 생성자
StartCoroutine(g); // Go()코루틴 함수 시작
total -= 1; // 한명이 Destroy코드로 사라졌기 때문에 총 total에서도 한명을 빼줌
}
}
public void checkActive()
{ // 위에 qr버튼과 비슷하게 설정
check.gameObject.SetActive(!check.gameObject.activeSelf);
if (movePlayer2 != null)
{
Destroy(movePlayer2.gameObject);
movePlayer2[] units = GameObject.FindObjectsOfType<movePlayer2>();
foreach (movePlayer2 unit2 in units)
{
if (unit2.gameObject.transform.position.x < 3.0f && unit2.gameObject.transform.position.x > 2.8f)
{
t1 = unit2;
}
else if (unit2.gameObject.transform.position.x < 2.3f && unit2.gameObject.transform.position.x > 2.1f)
{
t2 = unit2;
}
else if (unit2.gameObject.transform.position.x < 1.6f && unit2.gameObject.transform.position.x > 1.4f)
{
t3 = unit2;
}
}
t = Go2();
StartCoroutine(t);
total2 -= 1;
}
}
IEnumerator Go()
{
yield return new WaitForSeconds(0.1f);
if (p1 != null)
{
p1.stop = speed;
}
yield return new WaitForSeconds(0.1f);
if (p2 != null)
{
p2.stop = speed;
}
yield return new WaitForSeconds(0.1f);
if (p3 != null)
{
p3.stop = speed;
}
yield return null;
} // 플레이어들이 다시 가기 시작했을 때 순차적으로 시간의 텀을 주어서 서로 겹치거나 통과는 버그가 없게 만드는 코루틴함수
IEnumerator Go2()
{
yield return new WaitForSeconds(0.1f);
if (t1 != null)
{
t1.stop = speed;
}
yield return new WaitForSeconds(0.1f);
if (t2 != null)
{
t2.stop = speed;
}
yield return new WaitForSeconds(0.1f);
if (t3 != null)
{
t3.stop = speed;
}
yield return null;
} // Go()함수를 참고하여 만듬
public void PlusSpeed()
{ // 상점창에 있는 속도증가 버튼
speed += 0.5f; // 속도를 1.5배 업함
flag = false; // 더이상 누르지 못하게 설정
Coin -= 50; // Coin50을 지불
Ctext.GetComponent<Text>().text = " " + Coin; // Coin 50을 지불한 것을 게임 화면에 업데이트
}
public void coinUp()
{ // qr버튼을 눌렀을 때
Coin += 10; // Coin 10 상승
Ctext.GetComponent<Text>().text = " " + Coin; // Coin 10 상승한 것을 게임 화면에 업데이트
}
public void coinUp2()
{ // Check버튼을 눌렀을 때
Coin += 20;
Ctext.GetComponent<Text>().text = " " + Coin;
}
public void newPlayer()
{ // qr버튼을 눌렀을 때 온도체크를 끝낸 플레이어가 검사를 하기 위한 줄로 넘어가게 설정
Instantiate(prefab2, new Vector3(0, 0, 0), Quaternion.identity); // 0,0,0 좌표에서 복제
total2 += 1;
}
public void purchaseVaccine()
{ // 백신 구입 버튼을 클릭 했을 때
Coin -= 200;
Ctext.GetComponent<Text>().text = " " + Coin;
vaccine += 0.1f;
Vtext.GetComponent<Text>().text = vaccine * 100 + "%"; // 퍼센트로 보여주기 위한 작업
}
public void OnApplicationQuit()
{ // 게임을 종료하고 싶을 때
Application.Quit();
}
public void Cheat()
{ // 시연을 위한 치트키
Coin += 30000;
Ctext.GetComponent<Text>().text = " " + Coin;
}
}