using System; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; using UnityEngine.UI; public class ActiveScoppedElements : MonoBehaviour { [Serializable] public struct ObjectToInstantiate { public List elements; public GameObject obj; } // Start is called before the first frame update public TextAsset jsonFile; public HashSet actives = new HashSet(); public HashSet InstantiatesObject = new HashSet(); public TextMeshProUGUI textToUpdate; public TextMeshProUGUI objectInstantiateText; /*public ObjectToInstantiate[] objectToInstantiates;*/ public HashSet DisableGameObjects = new HashSet(); public GameObject[] InformationObjs; public GameObject[] DebugObjs; public Button button; void Start() { if (button != null) { button.onClick.AddListener(this.Center); } if (this.textToUpdate != null) { textToUpdate.text = "Scanner des Markers..."; } } public void AddScoppedElement(String name) { Debug.Log("Added " + name); if (!actives.Contains(name)) { actives.Add(name); RefreshText(); RefreshAtom(); } } public void Center() { Debug.Log("Center called !"); foreach (var obj in InstantiatesObject) { Vector3 cameraPosition = Camera.main.transform.position; // Obtenez la rotation de la caméra sans inclure la rotation autour de l'axe Y Quaternion cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0); // Calculez la position finale en ajoutant la direction vers l'avant multipliée par la distance désirée Vector3 position = cameraPosition + cameraRotation * Vector3.forward * 0.35f; //var instantiatedObj = Instantiate(objt.obj, position, cameraRotation); obj.transform.position = position; obj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); obj.transform.rotation = cameraRotation; } } private void RefreshText() { if (this.textToUpdate != null) { this.textToUpdate.text = String.Join(", ", this.actives); } } static IEnumerable GetPermutations(List list) { if (list.Count == 1) { // Cas de base : une seule chaîne yield return list[0]; } else { // Récursion pour obtenir les permutations du reste de la liste foreach (var item in list) { var remaining = new List(list); remaining.Remove(item); foreach (var subPermutation in GetPermutations(remaining)) { // Concaténer l'élément actuel avec les permutations du reste yield return item + subPermutation; } } } } private void DisableAllMarkerModel() { foreach (var o in GameObject.FindGameObjectsWithTag("OBJ")) { o.SetActive(false); DisableGameObjects.Add(o); } } private void EnableAllMarkerModel() { foreach (var o in DisableGameObjects) { o.SetActive(true); } DisableGameObjects.Clear(); } private GameObject FindGameObjectWithFormula() { var factory = MoleculeFactory.getInstrance(this.jsonFile); Debug.Log("ACTIVES ELEMENTS:"); Debug.Log(this.actives); foreach (var formula in GetPermutations(this.actives.ToList())) { Debug.Log("FORMULA: " + String.Join("", formula)); if (factory.hasMolecule(String.Join("", formula))) { return factory.createMolecule(String.Join("", formula)); } } return null; } private void RefreshAtom() { var objToFind = FindGameObjectWithFormula(); if (objToFind != null) { Vector3 cameraPosition = Camera.main.transform.position; // Obtenez la rotation de la caméra sans inclure la rotation autour de l'axe Y Quaternion cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0); // Calculez la position finale en ajoutant la direction vers l'avant multipliée par la distance désirée Vector3 position = cameraPosition + cameraRotation * Vector3.forward * 0.35f; //var instantiatedObj = Instantiate(objt.obj, position, cameraRotation); objToFind.transform.position = position; objToFind.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); objToFind.transform.rotation = cameraRotation; MeshRenderer meshRenderer = objToFind.GetComponent(); if (meshRenderer != null) { float sizeInDirection = meshRenderer.bounds.size.x; // Pour la hauteur, ajustez selon votre besoin Debug.Log("SizeInDirection: " + sizeInDirection); objToFind.transform.position -= objToFind.transform.right * sizeInDirection * 0.175f; } Debug.Log("RefreshAtom(): Created " + objToFind.name); objToFind.name = "[N] " + objToFind.name; InstantiatesObject.Add(objToFind); this.OnInstantiateObject(objToFind); Debug.Log("Instantiate " + objToFind.name); DisableAllMarkerModel(); } else { foreach (var o in InstantiatesObject) { Destroy(o); OnDestroyObject(o); } InstantiatesObject.Clear(); EnableAllMarkerModel(); } /*foreach (var objt in objectToInstantiates) { if (objt.elements.TrueForAll((s => actives.Contains(s)))) { foreach (var objName in objt.elements) { var a = GameObject.Find("OBJ_" + objName); if (a != null) { a.SetActive(false); DisableGameObjects.Add(a); } } // Obtenez la position de la caméra Vector3 cameraPosition = Camera.main.transform.position; // Obtenez la rotation de la caméra sans inclure la rotation autour de l'axe Y Quaternion cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0); // Calculez la position finale en ajoutant la direction vers l'avant multipliée par la distance désirée Vector3 position = cameraPosition + cameraRotation * Vector3.forward * 0.35f; //var instantiatedObj = Instantiate(objt.obj, position, cameraRotation); var obj = MoleculeFactory.getInstrance(this.jsonFile).createMolecule("CO2"); obj.transform.position = position; obj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f); obj.transform.rotation = cameraRotation; Debug.Log("RefreshAtom(): Created " + obj.name); obj.name = "[N] " + objt.obj.name; InstantiatesObject.Add(obj); this.OnInstantiateObject(obj); Debug.Log("Instantiate " + obj.name); } else { var a = InstantiatesObject.FirstOrDefault(s => s.name == ("[N] " + objt.obj.name)); if (a != null) { Debug.Log("Destroy " + a.name); InstantiatesObject.Remove(a); OnDestroyObject(a); Destroy(a); foreach (var disableGameObject in DisableGameObjects) { disableGameObject.SetActive(true); } DisableGameObjects.Clear(); } } }*/ if (InstantiatesObject.Count > 0) { objectInstantiateText.text = String.Join(", ", InstantiatesObject.Select((s) => s.name)); } else { objectInstantiateText.text = "En attente..."; } } private void OnInstantiateObject(GameObject obj) { foreach (var informationObj in InformationObjs) { informationObj.SetActive(true); } foreach (var debugObj in DebugObjs) { debugObj.SetActive(false); } } private void OnDestroyObject(GameObject obj) { foreach (var informationObj in InformationObjs) { informationObj.SetActive(false); } foreach (var debugObj in DebugObjs) { debugObj.SetActive(true); } } public void RemoveUnscoppedElement(String elementName) { Debug.Log("Removed " + elementName); actives.Remove(elementName); RefreshText(); RefreshAtom(); } void Update() { } }