using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class MapManager : MonoBehaviour { [Header("[--------------- Map Objects ---------------]")] public List maps = new List(); private string currentMapName; private List currentMapObjects = new List(); private AbstractMap mapScript; private WaveManager waveScript; private AbstractWaveLogic waveLogicScript; public void Start() { if(Application.isEditor) { ValidateMapObjects(); } // TODO: To move and call once we do need the map. (the task will be to do when we add menus) InitializeMap(maps[0].name); // TODO: Remove me once tests are done //GetComponent().SetStoreObject(FindCurrentMapInList().store); //GetComponent().OpenStore(); } private void InitializeMap(string mapName) { if (currentMapName != null) DestroyMap(); currentMapName = mapName; MapObject map = FindCurrentMapInList(); if(map == null) { Debug.LogError("Could not find map " + currentMapObjects + " in list of map objects"); return; } SpawnAllMapPrefabs(map); SpawnMapScript(map); GetComponent().SetStoreObject(map.store); } private void SpawnMapScript(MapObject map) { if (String.IsNullOrEmpty(map.mapScriptName)) { map.mapScriptName = map.name.Replace("Object", ""); } try { mapScript = (AbstractMap) gameObject.AddComponent(Type.GetType(map.mapScriptName)); mapScript.OnMapLoaded(); } catch(Exception e) { Debug.LogError("MapScriptName " + map.mapScriptName + " is wrong: " + e.Message); } } public void SpawnWaveScript() { MapObject map = FindCurrentMapInList(); if (map == null) { Debug.LogError("Could not find map " + currentMapObjects + " in list of map objects"); return; } SpawnWaveScript(map); } private void SpawnWaveScript(MapObject map) { if (String.IsNullOrEmpty(map.waveLogicScriptName)) { Debug.LogError("Map Object " + map.name + " has an empty wave script name."); return; } try { waveScript = gameObject.AddComponent(); waveLogicScript = (AbstractWaveLogic) gameObject.AddComponent(Type.GetType(map.waveLogicScriptName)); waveScript.Prepare(map.waveObjects[GameManager.Instance.GetDifficulty()], waveLogicScript); } catch (Exception e) { Debug.LogError("WaveLogicScriptName " + map.waveLogicScriptName + " is wrong: " + e.Message); } } private void DestroyMap() { MapObject map = FindCurrentMapInList(); if (map == null) { Debug.LogError("Could not find map " + currentMapObjects + " in list of map objects"); return; } DespawnAllMapPrefabs(); DespawnMapScript(); DespawnWaveScript(); GetComponent().CloseStore(); currentMapName = null; } private void DespawnMapScript() { if (mapScript == null) return; mapScript.OnMapUnloaded(); Destroy(gameObject.GetComponent(mapScript.GetType())); } private void DespawnWaveScript() { if (waveScript == null || waveLogicScript == null) return; waveScript.DestroyActions(); Destroy(gameObject.GetComponent()); Destroy(gameObject.GetComponent(waveLogicScript.GetType())); } private void SpawnAllMapPrefabs(MapObject map) { GameObject prefab; foreach(var item in map.prefabs) { prefab = item.Key; GameObject go = Instantiate(prefab, item.Value.position, item.Value.rotation); currentMapObjects.Add(go); StaticBatchingUtility.Combine(prefab); } } private void DespawnAllMapPrefabs() { GameObject go; for (int i = 0; i < currentMapObjects.Count; i++) { go = currentMapObjects[i]; Destroy(go); } } private MapObject FindCurrentMapInList() { return maps.Find(x => x.name == currentMapName); } /// /// Validate Map Objects data, should only be called when in editor mode /// /// private void ValidateMapObjects() { foreach(MapObject map in maps) { if (map.prefabs.Count == 0) Debug.LogError("Map Object " + map.name + " does not contain any prefabs!"); if (String.IsNullOrEmpty(map.waveLogicScriptName)) Debug.LogError("Map Object " + map.name + " has an empty wave logic script name."); if (map.waveObjects.Count != Enum.GetNames(typeof(GameManager.Difficulty)).Length) Debug.LogError("Map Object " + map.name + " waveObjects do not reflect difficulty options."); } } }