T-VIR-901_EpiLeARn/Assets/script/MoleculeFactory.cs

90 lines
3.5 KiB
C#
Raw Permalink Normal View History

2024-01-11 16:38:19 +01:00
using System;
using System.Collections.Generic;
using UnityEngine;
public class MoleculeFactory : MonoBehaviour{
public TextAsset jsonFile;
2024-01-18 14:24:40 +01:00
private AtomeFactory atomeFactory;
2024-01-11 16:38:19 +01:00
private static MoleculeFactory instance;
private Molecules moleculesInJson;
2024-01-18 12:03:38 +01:00
private Dictionary<string, Molecule> moleculesDictionary;
2024-01-11 16:38:19 +01:00
public MoleculeFactory(TextAsset jsonFile){
MoleculeFactory.instance = this;
2024-01-11 16:53:06 +01:00
moleculesDictionary = new Dictionary<string, Molecule>();
moleculesInJson = JsonUtility.FromJson<Molecules>(jsonFile.text);
foreach (var molecule in moleculesInJson.molecules){
moleculesDictionary.Add(molecule.formula, molecule);
}
2024-01-11 16:38:19 +01:00
}
public static MoleculeFactory getInstrance(){
if(MoleculeFactory.instance == null){
Debug.LogError("no Json file");
}
return MoleculeFactory.instance;
}
public static MoleculeFactory getInstrance(TextAsset jsonFile){
if(MoleculeFactory.instance == null){
MoleculeFactory.instance = new MoleculeFactory(jsonFile);
}
return MoleculeFactory.instance;
}
public GameObject createMolecule (string formula){
2024-01-11 16:53:06 +01:00
if(!moleculesDictionary.ContainsKey(formula)){
2024-01-11 17:16:12 +01:00
Debug.LogError("molecules does not existe in json");
2024-01-11 16:38:19 +01:00
}
2024-01-11 16:53:06 +01:00
Molecule molecule = moleculesDictionary[formula];
2024-01-11 17:11:59 +01:00
GameObject sortie = new GameObject(molecule.name);
foreach (Atom atom in molecule.atoms) {
2024-01-11 17:16:12 +01:00
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
2024-01-11 17:11:59 +01:00
sphere.transform.parent = sortie.transform;
sphere.name = atom.element;
sphere.transform.localPosition = new Vector3(atom.geometry[0], atom.geometry[1], atom.geometry[2]);
2024-01-18 14:24:40 +01:00
AtomeInformation att = atomeFactory.createAtome(atom.element);
2024-01-18 11:25:51 +01:00
Color myColor = new Color(0, 0, 1, 1);
2024-01-18 14:24:40 +01:00
ColorUtility.TryParseHtmlString(att.representation.color, out myColor);
2024-01-18 11:25:51 +01:00
sphere.GetComponent<Renderer>().material.color = myColor;
2024-01-11 17:11:59 +01:00
}
2024-01-12 16:28:35 +01:00
foreach (Bond bond in molecule.bonds){
float atom1x = molecule.atoms[bond.atoms[0]].geometry[0];
float atom2x = molecule.atoms[bond.atoms[1]].geometry[0];
float atom1y = molecule.atoms[bond.atoms[0]].geometry[1];
float atom2y = molecule.atoms[bond.atoms[1]].geometry[1];
float atom1z = molecule.atoms[bond.atoms[0]].geometry[2];
float atom2z = molecule.atoms[bond.atoms[1]].geometry[2];
Vector3 vecAtom1 = new Vector3(atom1x, atom1y, atom1z);
Vector3 vecAtom2 = new Vector3(atom2x, atom2y, atom2z);
2024-01-18 14:24:40 +01:00
GameObject GObond = CreateCylinderBetweenPoints(vecAtom1,vecAtom2, 0.3f);
2024-01-12 16:28:35 +01:00
GObond.transform.parent = sortie.transform;
}
2024-01-11 17:11:59 +01:00
return sortie;
2024-01-11 16:38:19 +01:00
}
2024-01-18 14:24:40 +01:00
public void setAtomFactory(AtomeFactory atomeFactory){
this.atomeFactory = atomeFactory;
}
2024-01-12 16:28:35 +01:00
private GameObject CreateCylinderBetweenPoints(Vector3 start, Vector3 end, float width){
var offset = end - start;
var scale = new Vector3(width, offset.magnitude / 2.0f, width);
var position = start + (offset / 2.0f);
var cylinderPrefab = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinderPrefab.transform.position = position;
cylinderPrefab.transform.rotation = Quaternion.identity;
cylinderPrefab.transform.up = offset;
cylinderPrefab.transform.localScale = scale;
return cylinderPrefab;
}
2024-01-11 16:38:19 +01:00
}