using UnityEngine; using System.Collections.Generic; using UnityEngine.Serialization; public class ModelManager : MonoBehaviour { private Dictionary> _portDict; //port mapped on private Dictionary _childModelDict; //childModels by their ID private List _baseModelList; //available BaseModels public GameObject baseModelGO; //GameObject of the BaseModel public BaseModel BaseModel //current baseModel Phenotype { get { return baseModelGO.GetComponent().BaseModel; } } public BaseModelBehaviour BaseModelBehaviour; public BaseModelSelector baseModelSelector; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { Debug.Log("ModelManager: " + gameObject.name); _portDict = new Dictionary>(); _childModelDict = new Dictionary(); _baseModelList = new List(); //baseModelSelector = FindFirstObjectByType(); foreach (var baseModel in ModelList.BaseModels) { RegisterBaseModel(baseModel); } foreach (var childModel in ModelList.ChildModels) { RegisterChildModel(childModel); } if (baseModelGO == null) //set in the editor { //TODO: send into model selection Debug.LogError("baseModel is null, fix the configuration"); } } // Update is called once per frame void Update() { } void RegisterChildModel(ChildModel model) { //childModelDict if (_childModelDict.ContainsKey(model.NameId)) { Debug.LogWarning("Model " + model.NameId + " already registered! [childModelDict] Skipping..."); return; } _childModelDict[model.NameId] = model; if (string.IsNullOrEmpty(model.Port)) { _portDict.Add("null", new HashSet()); return; } //portDict if (!_portDict.ContainsKey(model.Port)) { _portDict.Add(model.Port, new HashSet()); } if (_portDict[model.Port].Contains(model)) { Debug.LogWarning("Model " + model.NameId + " already registered! [portDict] Skipping..."); return; } _portDict[model.Port].Add(model); } void RegisterBaseModel(BaseModel baseModel) { if (baseModel == null) { return; } _baseModelList ??= new List(); _baseModelList.Add(baseModel); } public HashSet GetChildModelsForPort(string port) { // Check if key exists in the dictionary if (_portDict.TryGetValue(port, out HashSet childModels)) { return childModels; // Return childModels of the port } else { // Warning and return empty set if key not found Debug.LogWarning($"No model found for port {port}."); return new HashSet(); } } public ChildModel GetById(string id) { return _childModelDict[id]; } public void LoadSelectedModel(BaseModel baseModel) { //create if doesn't exist if (this.baseModelGO == null) { // Create new GameObject for the baseModel this.baseModelGO = new GameObject(baseModel.NameId) { name = baseModel.NameId, }; this.baseModelGO.AddComponent(); // Positioning this.baseModelGO.transform.position = new Vector3(0, 0, 1); this.baseModelGO.transform.rotation = Quaternion.Euler(0, 90, 0); this.baseModelGO.SetActive(true); } this.baseModelGO.GetComponent().BaseModel = baseModel; this.baseModelGO.SetActive(true); Debug.Log($"Model {baseModel.NameHuman} lock and loaded."); } public void OpenSelector() { Debug.Log("Open Selector"); baseModelGO.SetActive(false); baseModelSelector.Activate(); } }