58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class MoleculeFactory : MonoBehaviour{
|
|
|
|
public TextAsset jsonFile;
|
|
|
|
private static MoleculeFactory instance;
|
|
|
|
private Molecules moleculesInJson;
|
|
|
|
private Dictionary<string, Molecule> moleculesDictionary;
|
|
|
|
public MoleculeFactory(TextAsset jsonFile){
|
|
MoleculeFactory.instance = this;
|
|
moleculesDictionary = new Dictionary<string, Molecule>();
|
|
moleculesInJson = JsonUtility.FromJson<Molecules>(jsonFile.text);
|
|
foreach (var molecule in moleculesInJson.molecules){
|
|
moleculesDictionary.Add(molecule.formula, molecule);
|
|
}
|
|
}
|
|
|
|
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){
|
|
if(!moleculesDictionary.ContainsKey(formula)){
|
|
Debug.LogError("molecules does not existe in json")
|
|
}
|
|
Molecule molecule = moleculesDictionary[formula];
|
|
GameObject sortie = new GameObject(molecule.name);
|
|
|
|
foreach (Atom atom in molecule.atoms) {
|
|
GameObject Sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
|
sphere.transform.parent = sortie.transform;
|
|
sphere.name = atom.element;
|
|
sphere.transform.localPosition = new Vector3(atom.geometry[0], atom.geometry[1], atom.geometry[2]);
|
|
//TODO: géré la taille de l'atome
|
|
//TODO: géré la couleur de l'atome
|
|
}
|
|
return sortie;
|
|
}
|
|
|
|
} |