using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Events; public class BlockFactory : MonoBehaviour { private static BlockFactory Instance; private Environment environment; private List blockDataList; private Dictionary blocksData = new Dictionary(); public static BlockFactory getInstance() { if (Instance == null) { GameObject factoryObject = new GameObject("blockFactory"); Instance = factoryObject.AddComponent(); } return Instance; } private void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); } } private void Start() { environment = Environment.getInstance(); blockDataList = Resources.LoadAll("BlocksData").ToList(); foreach (BlockData blockData in blockDataList) { blocksData.Add(blockData.blockName, blockData); } } public GameObject createblock(string prefabId, Vector3 position, Quaternion rotation, string id = null, bool layerDiff = false) { Debug.Log(prefabId); if (blocksData.ContainsKey(prefabId)) { GameObject blockPrefab = blocksData[prefabId].blockPrefab; GameObject block = Instantiate(blockPrefab, position, rotation); if (layerDiff) { block.gameObject.layer = LayerMask.NameToLayer("Ignore Interaction"); Debug.Log($"Set layer of {block} to {LayerMask.LayerToName(block.layer)}"); } // the only blocks added to the environment is created with BlockFactory environment.addBlock(block.GetComponent(), id); return block; } else { Debug.LogWarning($"block with PrefabId {prefabId} not found!"); return null; } } }