using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager2 : MonoBehaviour { 

    [SerializeField] GameObject enemy_bullet_prefab;
    [SerializeField] GameObject bullet_pool;
    [SerializeField] int bullet_volume = 10;
    private List<GameObject> bulletPool = new List<GameObject>();

    public static GameManager2 Instance { get; private set; } // 싱글톤
    private GameManager2()
    {

    }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject); // Scene이 전환되어도 파괴되지 않도록 설정
        }
        else if (Instance != this)
        {
            Destroy(gameObject); // 중복 인스턴스가 생성된 경우 제거
        }
    }

    private void Start()
    {
        InitializeBulletPool(bullet_volume);
    }

    private void InitializeBulletPool(int count)
    {
        for (int i = 0; i < count; i++)
        {
            GameObject bullet = Instantiate(enemy_bullet_prefab, bullet_pool.transform);
            bullet.SetActive(false);
            bulletPool.Add(bullet);
        }
    }

    // 오브젝트 풀에서 사용 가능한 총알을 가져오는 메소드
    public GameObject GetBullet()
    {
        foreach (GameObject bullet in bulletPool)
        {
            if (!bullet.activeInHierarchy)
            {
                bullet.SetActive(true);
                return bullet;
            }
        }

        // 모든 총알이 사용 중이면, 추가로 생성하여 풀에 추가
        GameObject newBullet = Instantiate(enemy_bullet_prefab, bullet_pool.transform);
        newBullet.SetActive(false);
        bulletPool.Add(newBullet);
        return newBullet;
    }

    public GameObject GetBulletPool()
    {
        return this.bullet_pool;
    }
}

GameManager2.cs (싱글톤)

 

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializeField] Transform playerPos;
    [SerializeField] Transform firePos;
    [SerializeField] Transform shoot_bullet_pool;
    [SerializeField] int speed = 10;


    private void Start()
    {
        StartCoroutine("LookAtPlayerPos");
        StartCoroutine("CoShoot");
    }

   
    IEnumerator LookAtPlayerPos()
    {
        while (true)
        {
            this.gameObject.transform.LookAt(playerPos);

            yield return null;

        }
    }

    IEnumerator CoShoot()
    {
        while (true)
        {
            GameObject bullet = GameManager2.Instance.GetBullet();
            bullet.transform.SetParent(shoot_bullet_pool);
            bullet.transform.position = firePos.position;
            bullet.transform.rotation = firePos.rotation;


           


            yield return new WaitForSeconds(1);
        }
    }

    
    

}

Enemy.cs

 

using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 10f;

    void Update()
    {
        // 앞으로 이동 (로컬 z축 방향)
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Wall") || other.gameObject.CompareTag("Player"))
        {

            this.gameObject.transform.SetParent(GameManager2.Instance.GetBulletPool().transform);
            this.gameObject.SetActive(false);
        }
    }
}

Bullet.cs

 

using Newtonsoft.Json.Bson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player2 : MonoBehaviour
{
    public float moveSpeed = 5f;
    void Start()
    {
        StartCoroutine("CoMove");
    }



    IEnumerator CoMove()
    {
        while (true)
        {
            float h = Input.GetAxisRaw("Horizontal");
            float v = Input.GetAxisRaw("Vertical");


            Vector3 movement = new Vector3(h, 0f, v) * moveSpeed * Time.deltaTime;

            transform.Translate(movement);

            yield return null;
        }
    }




}

Player2.cs

반응형

+ Recent posts