139 lines
4.2 KiB
C#
139 lines
4.2 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class ModelManager : MonoBehaviour
|
|
{
|
|
private Dictionary<string, HashSet<ChildModel>> _portDict; //port mapped on
|
|
private Dictionary<string, ChildModel> _childModelDict; //childModels by their ID
|
|
|
|
private List<BaseModel> _baseModelList; //available BaseModels
|
|
|
|
public GameObject baseModelGO; //GameObject of the BaseModel
|
|
public BaseModel BaseModel //current baseModel Phenotype
|
|
{
|
|
get { return baseModelGO.GetComponent<BaseModelBehaviour>().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<string, HashSet<ChildModel>>();
|
|
_childModelDict = new Dictionary<string, ChildModel>();
|
|
_baseModelList = new List<BaseModel>();
|
|
//baseModelSelector = FindFirstObjectByType<BaseModelSelector>();
|
|
|
|
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<ChildModel>());
|
|
return;
|
|
}
|
|
//portDict
|
|
if (!_portDict.ContainsKey(model.Port))
|
|
{
|
|
_portDict.Add(model.Port, new HashSet<ChildModel>());
|
|
}
|
|
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<BaseModel>();
|
|
_baseModelList.Add(baseModel);
|
|
}
|
|
|
|
public HashSet<ChildModel> GetChildModelsForPort(string port)
|
|
{
|
|
// Check if key exists in the dictionary
|
|
if (_portDict.TryGetValue(port, out HashSet<ChildModel> 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<ChildModel>();
|
|
}
|
|
}
|
|
|
|
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<BaseModelBehaviour>();
|
|
// 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<BaseModelBehaviour>().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();
|
|
}
|
|
|
|
}
|