ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • GameManager - 캡스톤 개발일지
    UNITY 시행착오들 2023. 6. 19. 09:24
    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Pool;
    using System.Runtime.CompilerServices;
    using UnityEngine.SceneManagement;
    
    public class UI : MonoBehaviour
    {
        [Header("UI")]
        public GameObject UIP;
        bool isActive = false;
    
    
        public float maxtime = 10f;  //�Ʒ��� Ÿ�̸Ӹ� ���� ������
        public float timer;
    
        TextMeshProUGUI Timer;
        float power = 1;
    
    
        public Transform plane; 
    
        //[SerializeField]
        public int waveCount;
    
    
        [Header("���� ��ȯ")]
        public float radius = 1f;
        public float camWidth;
        public float camHeight;
    
    
        [SerializeField]
        private float spawn = 3.0f;  //���� Ÿ�̹�
        [SerializeField]
        private float bossSpawn = 30.0f; //������ ��
        public bool isBossSpawned = false; //������ �� �Ǿ��°�?
    
    
    
        public int NumberOfMonsters = 3; //��ȯ�� ���� ��
        private float maxDistance;  //��ȯ�Ǵ� �ִ� ����
        public float max;//�ִ� ������ ������ ī�޶� �ۿ��� �����ǰ� �ϱ� ���� ����
        public int maxOfMonsters = 100;
    
        public GameObject monster;
        public GameObject monster1;
        public GameObject monster2;
    
        public GameObject Boss; //������
        public GameObject BossCut;//������ �ƽ�
    
        public GameObject Golem; //��
        public GameObject Gazer; //������
        public GameObject Anubis; //�ƴ���
        public GameObject Dragon; //�巡��
    
        private int bossCount; //�� ��° �������ΰ���
    
    
    
        private IObjectPool<MonsterCtrl> _Pool;  //Ǯ�� ������ ����
        private IObjectPool<MonsterCtrl> Pool1;  //Ǯ�� ������ ����
    
    
    
        public GameObject player;
    
        Transform trans;
        public float timescale = 0; //����
        public bool isBoss;
    
    
        public GameObject stage;
        private float xRotateMove, yRotateMove;
        public float rotateSpeed = 500.0f;
        public float scrollSpeed = 2000.0f;
    
    
        public GameObject LevelUpUI;
        public Text KillLog;
        public Text LevelText_Test;
        public static UI instance;
        public int killcount=0;
    
        
        [System.Serializable]   
        public struct BgmType
        {
            public string name;
            public AudioClip audio;
        }
    
        // Inspector 에표시할 배경음악 목록
        public BgmType[] BGMList;
    
        private AudioSource BGM;
        private string NowBGMname = "";
    
        public bool isPlayer; //플레이어가 살아 있을 때만 BGM이 나오도록
    
     
        void Start()
        {
            UIP.SetActive(isActive);
            //Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<Image>(); //�� ������Ʈ �޾ƿ�
            Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<TextMeshProUGUI>(); //�� ������Ʈ �޾ƿ�
            BossCut.SetActive(false);
            StartCoroutine(MonsterSpawn()); //���͸� �����ϴ� �ڷ�ƾ
            StartCoroutine(bossMobSpawn());  //���������� ����
            waveCount = 0;
            bossCount = 0;
            maxtime = 10;
            timer = 900;
            isBossSpawned = false;
     
        }
      
        private void Awake()
        {
            UIP.SetActive(isActive);
            //Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<Image>(); //�� ������Ʈ �޾ƿ�
            Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<TextMeshProUGUI>(); //�� ������Ʈ �޾ƿ�
            BossCut.SetActive(false);
           // StartCoroutine(MonsterSpawn()); 
           //StartCoroutine(bossMobSpawn());  
            waveCount = 0;
            bossCount = 0;
            maxtime = 10;
            timer = 900;
            isBossSpawned = false;
    
    
            camHeight = Camera.main.orthographicSize; //ī�޶� ����
            camWidth = camHeight * Camera.main.aspect;
    
    
    
            _Pool = new ObjectPool<MonsterCtrl>(SpawnMonster, GetMob, ReleaseMob, OnDestroyMob, maxSize: maxOfMonsters);
            Pool1 = new ObjectPool<MonsterCtrl>(SpawnMonster2, GetMob, ReleaseMob, OnDestroyMob, maxSize: maxOfMonsters);
            instance=this;
    
            BGM = gameObject.AddComponent<AudioSource>();
            BGM.loop = true;
            if (BGMList.Length > 0) PlayBGM(BGMList[0].name);
    
    
            isPlayer = true;
        }
    
        public void PlayBGM(string name)  //음악 재생용 함수. 이름을 넣어서 사용한다.
        {
            if (NowBGMname.Equals(name)) return;
    
            for (int i = 0; i < BGMList.Length; ++i)
                if (BGMList[i].name.Equals(name))
                {
                    BGM.clip = BGMList[i].audio;
                    BGM.Play();
                    NowBGMname = name;
                }
        }
    
    
        public void SelectWeapon()
        {
            LevelUpUI.SetActive(false);
            Time.timeScale=1f;
    
        }
        // Update is called once per frame
        void Update()
        {
            timer -= Time.deltaTime;  //Ÿ�̸ӿ� ���������� ���� �ð��� ����
            float minutes = Mathf.Floor(timer / 60);
            float seconds = Mathf.RoundToInt(timer % 60);
            Timer.text = minutes.ToString() + " : " + seconds.ToString();
            KillLog.text="처치한 적의 수 : " + killcount;
          
    
            maxDistance = camWidth - radius + max;
    
    
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
    
    
    
            if (GameObject.FindWithTag("Boss") == null)  // Boss 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Slime") == null)  // Slime 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Gazer") == null)  // Gazer 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Golem") == null)  // Golem 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Anubis") == null)  // Anubis 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
    
            else
            {
                isBoss = false; //isboss�� true�� ���� ���� �����ǰ� ��
                isBossSpawned = true;
            }
    
            if (Input.GetKeyUp(KeyCode.R))
            {
                //SceneManager.LoadScene(0);
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
            }
    
            if (Input.GetMouseButton(0))
            {
                xRotateMove = Input.GetAxis("Mouse X") * Time.deltaTime * rotateSpeed;
                yRotateMove = Input.GetAxis("Mouse Y") * Time.deltaTime * rotateSpeed;
    
                Vector3 stagePosition = stage.transform.position;
    
                transform.RotateAround(stagePosition, Vector3.right, -yRotateMove);
                transform.RotateAround(stagePosition, Vector3.up, xRotateMove);
    
                transform.LookAt(stagePosition);
            }
            else
            {
                float scroollWheel = Input.GetAxis("Mouse ScrollWheel");
    
                Camera.main.fieldOfView -= scroollWheel * Time.deltaTime * scrollSpeed;
            }
    
            if (!isPlayer)
                BGM.mute = true;
            if (!isBoss)
            {
                PlayBGM(BGMList[1].name);
            }
            else
            {
                PlayBGM(BGMList[0].name);
            }
    
            Debug.Log(isBoss);
        }
    
        public void Enhance()
        {
            Time.timeScale = 1;
            power += 1;
            Debug.Log("SecondButton");
            isActive = !isActive;
            UIP.SetActive(isActive);
        }
        public void gain()
        {
            Time.timeScale = 1;
            power += 1;
            Debug.Log("FirstButton");
            isActive = !isActive;
            UIP.SetActive(isActive);
    
        }
    
        public void sEnhance()
        {
            Time.timeScale = 1;
            power += 1;
            Debug.Log("Thirdbutton");
            isActive = !isActive;
            UIP.SetActive(isActive);
    
        }
        //int k = Random.Range(0, 2);
       // int j = Random.Range(0, 2);
    
        private MonsterCtrl SpawnMonster()
        {
    
    
            MonsterCtrl mob = Instantiate(monster).GetComponent<MonsterCtrl>();
            mob.SetManagedPool(_Pool);
            return mob;
        }
    
        private MonsterCtrl SpawnMonster2()
        {
            MonsterCtrl mob = Instantiate(monster1).GetComponent<MonsterCtrl>();
            mob.SetManagedPool(_Pool);
            return mob;
        }
    
        private void GetMob(MonsterCtrl mob)
        {
            mob.gameObject.SetActive(true);
        }
    
        private void ReleaseMob(MonsterCtrl mob)
        {
            mob.gameObject.SetActive(false);
        }
    
        private void OnDestroyMob(MonsterCtrl mob)
        {
            Destroy(mob.gameObject);
        }
    
    
        IEnumerator MonsterSpawn()
        {
    
            while (true)
            {
                GameObject[] LenMob;
                LenMob = GameObject.FindGameObjectsWithTag("Monster");
                 if (LenMob.Length <= max - 3) {
                if (waveCount < 1  &&  isBossSpawned == false)
                {
                    for (int i = 0; i < NumberOfMonsters; i++)
                    {
                      // float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                        // float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                        float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                        float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
                        int k = Random.Range(0, 2);
                        int j = Random.Range(0, 2);
    
                        if (k == 1)
                        {
                            x *= -1;
                        }
                        if (j == 1)
                        {
                            z *= -1;
                        }
    
                        Vector3 pos = new Vector3(x, 0, z);
    
                            //var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                           var mob = _Pool.Get();
    
                        mob.transform.position = pos;
                        //Instantiate(mob, pos, Quaternion.identity);  //����
                    }
    
    
                }
    
                    //2���� ����ȯ
                    //if (waveCount == 1) && LenMob.Length <= max - 3
                    if (isBossSpawned == false && waveCount >= 1)
                    {
    
                        for (int i = 0; i < NumberOfMonsters / 2; i++)
                        {
    
    
                            float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                            float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                            int k = Random.Range(0, 2);
                            int j = Random.Range(0, 2);
    
                            if (k == 0)
                            {
                                x *= -1;
                            }
                            if (j == 0)
                            {
                                z *= -1;
                            }
    
                            Vector3 pos = new Vector3(x, 0, z);
                            // var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                            var mob = _Pool.Get();
                            Instantiate(mob, pos, Quaternion.identity);  //����
                        }
    
                        for (int i = 0; i < NumberOfMonsters / 2; i++)
                        {
    
    
                            //  float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                            //float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                            float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                            float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
    
                            int k = Random.Range(0, 2);
                            int j = Random.Range(0, 2);
    
                            if (k == 0)
                            {
                                x *= -1;
                            }
                            if (j == 0)
                            {
                                z *= -1;
                            }
    
    
                           // Debug.Log("거미왜나옴");
                            Vector3 pos = new Vector3(x, plane.position.y, z);
                            //var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                            // Instantiate(mob, pos, Quaternion.identity);  //����
    
                            var mob2 = Pool1.Get();
    
                            mob2.transform.position = pos;
    
                        }
                    }
                    /*
    
                    for (int i = 0; i < NumberOfMonsters / 2; i++)
                    {
    
    
                        float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                        float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                              Vector3 pos = new Vector3(x, 0, z);
                              var mob = pulling.GetObject(pulling.Instance.QueueDic["SpiderMonster"], "SpiderMonster");
                              //var mob = pulling.GetObject();
                              Instantiate(mob, pos, Quaternion.identity);  //����
                          }
    
                          for (int i = 0; i < NumberOfMonsters / 3; i++)
                          {
    
    
                              float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                              float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                              int k = Random.Range(0, 2);
                              int j = Random.Range(0, 2);
    
                              if (k == 0)
                              {
                                  x *= -1;
                              }
                              if (j == 0)
                              {
                                  z *= -1;
                              }
    
    
    
                              Vector3 pos = new Vector3(x, 0, z);
                              var mob = pulling.GetObject(pulling.Instance.QueueDic["MushroomMonster"], "MushroomMonster");
                              //var mob = pulling.GetObject();
                              Instantiate(mob, pos, Quaternion.identity);  //����
                          }
    
                      */
    
                }
                    yield return new WaitForSeconds(spawn);
    
            }
        }
    
    
        IEnumerator bossMobSpawn()
        {
            yield return new WaitForSeconds(bossSpawn);
    
            if (!isBossSpawned)
            {     
    
    
                //float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                // float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
    
                int k = Random.Range(0, 2);
                int j = Random.Range(0, 2);
    
                if (k == 0)
                {
                    x *= -1;
                }
                if (j == 0)
                {
                    z *= -1;
                }
    
    
    
                // Vector3 pos = new Vector3(0, 0, 0);
                Vector3 pos = new Vector3(x, plane.position.y + 1.0f, z);
                if (bossCount < 4)
                {
                   // isBoss = false;
                    //int WhatBoss = UnityEngine.Random.Range(0, 1); //4���� �ֵ� �̹��� �����Ӹ�
                    //WhatBoss
                    switch (waveCount)
                    {
                        case 0:  //������ ����
                            Instantiate(Boss, pos, Quaternion.identity);  //����
                            break;
                        case 1:  //�ƴ��� ����
                            Instantiate(Anubis, pos, Quaternion.identity);  //����
                            break;
                        case 2:  //������ ����
                            Instantiate(Gazer, pos, Quaternion.identity);  //����
                            break;
                        case 3:  //�� ����
                            Instantiate(Golem, pos, Quaternion.identity);  //����
                            break;
                    }
                    bossCount++;
    
                    BossCut.SetActive(true);
                }
                
                else
                {
                    //isBoss = false;
                    Instantiate(Dragon, pos, Quaternion.identity);
                    BossCut.SetActive(true);
                }
                
               
            }
            waveCount++;
        }
    
        
    
    }

    △코드 전문

     

    캡스톤을 진행하면서 가장 스파게티로 짜인 코드가 이 UI라는 스크립트다. 

     

    보이는가, 이 수많은 Public 변수들이!

    이 코드는 총 4가지의 역할을 수행한다. 몬스터/보스 몬스터를 소환하기, 오브젝트 풀링, 각종 UI 관리, BGM관리.

     

    우선 몬스터와 보스 몬스터를 생성하는 코드다.

        IEnumerator MonsterSpawn()
        {
    
            while (true)
            {
                GameObject[] LenMob;
                LenMob = GameObject.FindGameObjectsWithTag("Monster");
                 if (LenMob.Length <= max - 3) {
                if (waveCount < 1  &&  isBossSpawned == false)
                {
                    for (int i = 0; i < NumberOfMonsters; i++)
                    {
                      // float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                        // float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                        float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                        float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
                        int k = Random.Range(0, 2);
                        int j = Random.Range(0, 2);
    
                        if (k == 1)
                        {
                            x *= -1;
                        }
                        if (j == 1)
                        {
                            z *= -1;
                        }
    
                        Vector3 pos = new Vector3(x, 0, z);
    
                            //var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                           var mob = _Pool.Get();
    
                        mob.transform.position = pos;
                        //Instantiate(mob, pos, Quaternion.identity);  //����
                    }
    
    
                }
    
                    //2���� ����ȯ
                    //if (waveCount == 1) && LenMob.Length <= max - 3
                    if (isBossSpawned == false && waveCount >= 1)
                    {
    
                        for (int i = 0; i < NumberOfMonsters / 2; i++)
                        {
    
    
                            float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                            float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                            int k = Random.Range(0, 2);
                            int j = Random.Range(0, 2);
    
                            if (k == 0)
                            {
                                x *= -1;
                            }
                            if (j == 0)
                            {
                                z *= -1;
                            }
    
                            Vector3 pos = new Vector3(x, 0, z);
                            // var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                            var mob = _Pool.Get();
                            Instantiate(mob, pos, Quaternion.identity);  //����
                        }
    
                        for (int i = 0; i < NumberOfMonsters / 2; i++)
                        {
    
    
                            //  float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                            //float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                            float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                            float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
    
                            int k = Random.Range(0, 2);
                            int j = Random.Range(0, 2);
    
                            if (k == 0)
                            {
                                x *= -1;
                            }
                            if (j == 0)
                            {
                                z *= -1;
                            }
    
    
                           // Debug.Log("거미왜나옴");
                            Vector3 pos = new Vector3(x, plane.position.y, z);
                            //var mob = pulling.GetObject(pulling.Instance.QueueDic["SlimeMonster"], "SlimeMonster");
                            //var mob = pulling.GetObject();
                            // Instantiate(mob, pos, Quaternion.identity);  //����
    
                            var mob2 = Pool1.Get();
    
                            mob2.transform.position = pos;
    
                        }
                    }
                    /*
    
                    for (int i = 0; i < NumberOfMonsters / 2; i++)
                    {
    
    
                        float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                        float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                              Vector3 pos = new Vector3(x, 0, z);
                              var mob = pulling.GetObject(pulling.Instance.QueueDic["SpiderMonster"], "SpiderMonster");
                              //var mob = pulling.GetObject();
                              Instantiate(mob, pos, Quaternion.identity);  //����
                          }
    
                          for (int i = 0; i < NumberOfMonsters / 3; i++)
                          {
    
    
                              float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                              float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                              int k = Random.Range(0, 2);
                              int j = Random.Range(0, 2);
    
                              if (k == 0)
                              {
                                  x *= -1;
                              }
                              if (j == 0)
                              {
                                  z *= -1;
                              }
    
    
    
                              Vector3 pos = new Vector3(x, 0, z);
                              var mob = pulling.GetObject(pulling.Instance.QueueDic["MushroomMonster"], "MushroomMonster");
                              //var mob = pulling.GetObject();
                              Instantiate(mob, pos, Quaternion.identity);  //����
                          }
    
                      */
    
                }
                    yield return new WaitForSeconds(spawn);
    
            }
        }
    
    
        IEnumerator bossMobSpawn()
        {
            yield return new WaitForSeconds(bossSpawn);
    
            if (!isBossSpawned)
            {     
    
    
                //float x = Random.Range(camWidth - radius, maxDistance);  //ī�޶� ���� ���� x��ǥ
                // float z = Random.Range(camHeight - radius, maxDistance);  //z��ǥ
    
                float x = Random.Range(player.transform.position.x + 6.0f, maxDistance);  //�÷��̾� ���� ���� x��ǥ
                float z = Random.Range(player.transform.position.z + 6.0f, maxDistance);  //z��ǥ
    
    
                int k = Random.Range(0, 2);
                int j = Random.Range(0, 2);
    
                if (k == 0)
                {
                    x *= -1;
                }
                if (j == 0)
                {
                    z *= -1;
                }
    
    
    
                // Vector3 pos = new Vector3(0, 0, 0);
                Vector3 pos = new Vector3(x, plane.position.y + 1.0f, z);
                if (bossCount < 4)
                {
                   // isBoss = false;
                    //int WhatBoss = UnityEngine.Random.Range(0, 1); //4���� �ֵ� �̹��� �����Ӹ�
                    //WhatBoss
                    switch (waveCount)
                    {
                        case 0:  //������ ����
                            Instantiate(Boss, pos, Quaternion.identity);  //����
                            break;
                        case 1:  //�ƴ��� ����
                            Instantiate(Anubis, pos, Quaternion.identity);  //����
                            break;
                        case 2:  //������ ����
                            Instantiate(Gazer, pos, Quaternion.identity);  //����
                            break;
                        case 3:  //�� ����
                            Instantiate(Golem, pos, Quaternion.identity);  //����
                            break;
                    }
                    bossCount++;
    
                    BossCut.SetActive(true);
                }
                
                else
                {
                    //isBoss = false;
                    Instantiate(Dragon, pos, Quaternion.identity);
                    BossCut.SetActive(true);
                }
                
               
            }
            waveCount++;
        }

     결은 비슷하다. 둘 다 플레이어에서 일정 거리 벌어진 곳에서 몬스터를 소환한다. 나는 생성 위치를 랜덤하게 하기 위해서 max, maxdistance 등을 이용해서 나오는 거리를 조절하고, 방향을 k, j변수를 이용해서 랜덤 방향으로 소환했다. 같은 거리에 있더라도 (1,1),(1,0),(1,-1),(0,0),(0,1),(0,-1),(-1,-1),(-1,0),(-1,1)의 경우의 수는 다 다르다. 따라서 이런 식으로 곱하면 방향을 랜덤하게 가져올 수 있다.

     몬스터의 경우 최댓값을 넘어서 나오면 무한정 나오게 될 것이다. 따라서 몬스터의 숫자를 tag를 통해 배열로 받아 오고, 배열의 길이에 따라 작동되고 말고를 결정하게 만들었다. 저 -3은 시험 버전의 우리 게임에서는 원래 몬스터가 3마리씩 나왔기 때문에 저렇다. 나중에는 한 번에 25~50마리씩 생성되다 보니 저 정도 숫자가 티가 나지 않았다. 원래대로라면 바꾸는 게 맞을 것이다.

     보스 몬스터의 경우 case-switch문을 이용해서 각 웨이브별로 보스몹을 정해 놨는데, 사실 원래는 랜덤 값을 통해서 랜덤한 보스를 등장시켰다. 그렇지만 각 보스별 난이도 차이가 명확하므로 마지막에 그냥 고정 보스로 바꿔 놓았다. 어차피 게임 플레이 타임도 지정되어 있기 때문에 당장에는 큰 문제가 없을 것이다. 이후에 보완하게 된다면 웨이브 별 보스 목록을 만들어서 소환해야 될 것 같다.

     이때, 보스 몬스터가 나왔다면 몬스터가 소환되지 않았으면 좋겠다고 생각했다. 우리의 보스들은 만만한 친구들이 아니니까. 그래서 처음에는 보스 몬스터가 나왔을 때 public GameObject를 이용해서 Gamemanager를 호출하고, 이 UI의 isboss,isbossSpawned 변수를 변환시키려고 했다. 그런데 역시나, 다른 코드에서 접근하는 만큼 제대로 작동하지 않았다. 그래서 내가 내놓은 답은 간단했다. GameObject[] 배열 변수를 선언하고, 배열의 길이가 0이라면 보스 몬스터가 없는 것 아니겠는가. 그렇기 때문에 Update에서 이 확인 작업을 지속하는 것으로 해결했다.

            if (GameObject.FindWithTag("Boss") == null)  // Boss 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Slime") == null)  // Slime 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Gazer") == null)  // Gazer 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Golem") == null)  // Golem 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
            else if (GameObject.FindWithTag("Anubis") == null)  // Anubis 태그를 가진 오브젝트를 찾지 못했을 때
            {
                isBoss = true; // isBoss를 true로 설정하여 해당 조건을 만족하는 상황을 나타냄
                isBossSpawned = false;
            }
    
            else
            {
                isBoss = false; //isboss�� true�� ���� ���� �����ǰ� ��
                isBossSpawned = true;
            }

     

     

     

    풀링은 정확하게 https://wergia.tistory.com/353 여기 있는 것과 같이 구현했다. 또한 UI는 다른 팀원이 담당하였기에 초기작업밖에 하지 않은 내 코드를 구태여 기술하진 않으려 한다.

     

     [System.Serializable]   
        public struct BgmType
        {
            public string name;
            public AudioClip audio;
        }
    
        // Inspector 에표시할 배경음악 목록
        public BgmType[] BGMList;
    
        private AudioSource BGM;
        private string NowBGMname = "";
    
        public bool isPlayer; //플레이어가 살아 있을 때만 BGM이 나오도록
        
        
        private void Awake()
        {
            UIP.SetActive(isActive);
            //Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<Image>(); //�� ������Ʈ �޾ƿ�
            Timer = GameObject.FindGameObjectWithTag("Timer")?.GetComponent<TextMeshProUGUI>(); //�� ������Ʈ �޾ƿ�
            BossCut.SetActive(false);
           // StartCoroutine(MonsterSpawn()); 
           //StartCoroutine(bossMobSpawn());  
            waveCount = 0;
            bossCount = 0;
            maxtime = 10;
            timer = 900;
            isBossSpawned = false;
    
    
            camHeight = Camera.main.orthographicSize; //ī�޶� ����
            camWidth = camHeight * Camera.main.aspect;
    
    
    
            _Pool = new ObjectPool<MonsterCtrl>(SpawnMonster, GetMob, ReleaseMob, OnDestroyMob, maxSize: maxOfMonsters);
            Pool1 = new ObjectPool<MonsterCtrl>(SpawnMonster2, GetMob, ReleaseMob, OnDestroyMob, maxSize: maxOfMonsters);
            instance=this;
    
            BGM = gameObject.AddComponent<AudioSource>();
            BGM.loop = true;
            if (BGMList.Length > 0) PlayBGM(BGMList[0].name);
    
    
            isPlayer = true;
        }
    
        public void PlayBGM(string name)  //음악 재생용 함수. 이름을 넣어서 사용한다.
        {
            if (NowBGMname.Equals(name)) return;
    
            for (int i = 0; i < BGMList.Length; ++i)
                if (BGMList[i].name.Equals(name))
                {
                    BGM.clip = BGMList[i].audio;
                    BGM.Play();
                    NowBGMname = name;
                }
        }
        
        void update(){
                if (!isPlayer)
                BGM.mute = true;
            if (!isBoss)
            {
                PlayBGM(BGMList[1].name);
            }
            else
            {
                PlayBGM(BGMList[0].name);
            }
    
            Debug.Log(isBoss);
            }

    △ BGM 관련 코드들

     

    BGM에 관련된 코드들을 따로따로 모아 놓은 것들이다. BgmType 구조체를 사용해서 이름과 AudioSource를 매칭하고, 해당 구조체를 배열로 만들어서 여러 값을 받는다. 이후 이 구조체의 이름을 받아서 상태에 따라 BGM을 재생한다.

     

     

     

Designed by Tistory.