-
Notifications
You must be signed in to change notification settings - Fork 2
5 ‐ EnemyManager
문현우 edited this page Sep 13, 2023
·
3 revisions
🎇 Managed by 김광수
- 맵에 적을 스폰하는 기능을 담당합니다.
-
Awake()
- Enemymanager 클래스의 인스턴스를 생성하고, Enemies 필드와 mapList 필드를 초기화합니다.
-
InitMap()
- 맵은 3x3격자로 이루어져 있으며, 각 격자에는 하나의 적을 스폰할 수 있습니다.
-
SpawnEnemy()
- 맵의 빈칸에 적을 스폰합니다. 만약 맵에 빈칸이 없다면, 더 이상 소환하지 않습니다.
-
FindEmptyPos()
- 맵에 있는 빈 칸의 위치를 반환합니다.
EnemyManger
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public static EnemyManager Instance { get; private set; }
public List<Enemy> Enemies { get; private set; }
[SerializeField] private GameObject[] enemyPrefabs = new GameObject[4];
List<List<int[]>> mapList = new List<List<int[]>>();
float startX = -17.5f;
float startY = -9f;
float spacing = 1f;
int width = 36;
int height = 19;
private void Awake()
{
if(Instance == null) Instance = this;
Enemies = new List<Enemy>();
mapList = new List<List<int[]>>();
InitMap();
}
public void InitMap()
{
for (int i = 0; i < 3; i++)
{
List<int[]> temp = new List<int[]>();
for (int j = i; j < height - i; j++)
{
temp.Add(new int[] { j, i });
temp.Add(new int[] { j, width - 1 - i });
}
for (int j = i + 1; j < width - (i + 1); j++)
{
temp.Add(new int[] { i, j });
temp.Add(new int[] { height - 1 - i, j });
}
mapList.Add(temp);
}
}
public void SpawnEnemy()
{
if (mapList.Count == 0)
{
Debug.Log("Enemy를 더이상 Spawn시킬 자리가 없습니다");
return;
}
GameObject go = Instantiate(enemyPrefabs[UnityEngine.Random.Range(0,4)], FindEmptyPos(), Quaternion.identity);
Enemies.Add(go.GetComponent<Enemy>());
}
public Vector3 FindEmptyPos()
{
int index = UnityEngine.Random.Range(0, mapList[0].Count);
int[] pos = mapList[0][index];
mapList[0].RemoveAt(index);
if(mapList[0].Count == 0)
{
mapList.RemoveAt(0);
}
return new Vector3(startX, startY) + new Vector3(pos[1] * spacing, pos[0] * spacing);
}
}