/// <summary> /// Finds the nearest free molecule of a specific species. /// </summary> /// <returns><c>true</c>, if nearest molecule for reaction was found, <c>false</c> otherwise.</returns> /// <param name="reference">Reference.</param> /// <param name="species">Species.</param> /// <param name="molecule">Molecule.</param> private bool FindNearestMoleculeForReaction(Molecule reference, MoleculeSpecies species, out Molecule molecule) { Vector3 position = reference.Position; float minDistance = float.MaxValue; molecule = null; ShortKeyDict <MoleculeSpecies, MoleculeSet> .Entry entry; if (collection.Find(species, out entry)) { MoleculeSet ms = entry.Value; foreach (var m in ms.Free) { float distance = Vector3.Distance(position, m.Position); if ((distance < minDistance) && (reference != m)) { minDistance = distance; molecule = m; } } } else { // molecule species not listed --> molecule == null --> false is returned } return(molecule != null); }
/// <summary> /// Gets the quantity of molecules of a specific species in the environment. /// Returns only the free molecules. /// </summary> /// <returns>The count of molecules</returns> /// <param name="species">Species.</param> public ulong GetQuantity(MoleculeSpecies species) { if (Application.isPlaying) { ShortKeyDict <MoleculeSpecies, MoleculeSet> .Entry entry; if (collection.Find(species, out entry)) { return(entry.Value.Free.Count); } else { return(0); } } else { // when not playing, the molecules are not registered to the molecule // manager. Therefore they have to be counted this way: ulong count = 0; foreach (var m in GameObject.FindObjectsOfType <Molecule>()) { if (m.Species == species) { count++; } } return(count); } }
/// <summary> /// Internal Method to removes all molecules of a specific species or all /// molecules. /// </summary> /// <param name="moleculeSpecies">species to remove.</param> /// <param name="all">If set to <c>true</c> all molecules (species does not matter) are removed.</param> private void RemoveMolecules(MoleculeSpecies moleculeSpecies, bool all) { Molecule[] molecules = GameObject.FindObjectsOfType <Molecule>(); foreach (var m in molecules) { if (all || moleculeSpecies.Equals(m.Species)) { GameObject.DestroyImmediate(m.gameObject); } } }
/// <summary> /// Finds a random free molecule of a specific species. /// </summary> /// <returns><c>true</c>, if random molecule for reaction was found, <c>false</c> otherwise.</returns> /// <param name="species">Species.</param> /// <param name="molecule">Molecule.</param> private bool FindRandomMoleculeForReaction(MoleculeSpecies species, out Molecule molecule) { ShortKeyDict <MoleculeSpecies, MoleculeSet> .Entry entry; if (collection.Find(species, out entry)) { MoleculeCollection free = entry.Value.Free; ulong c = free.Count; if (c == 0) { molecule = null; return(false); } uint i = (uint)(Random.value * c); if (i == free.Count) { i--; } // because Random.value's maximum is inclusive 1 foreach (var m in free) { if (i == 0) { molecule = m; return(true); } i--; } throw new System.Exception("should not be possible"); } else { molecule = null; return(false); } }
/// <summary> /// Initializes a new instance of the <see cref="CellUnity.MoleculeManager+MoleculeSet"/> class. /// </summary> /// <param name="species">Species of the MolculeSet</param> public MoleculeSet(MoleculeSpecies species) { Free = new MoleculeCollection(species.Name + " free"); Reacting = new MoleculeCollection(species.Name + " reacting"); }
/// <summary> /// Removes all molecules of a specific species. /// (All GameObjects with the <see cref="Molecule"/>-Script applied, /// associated with the species). /// </summary> /// <param name="moleculeSpecies">Species to remove</param> public void RemoveMolecules(MoleculeSpecies moleculeSpecies) { RemoveMolecules(moleculeSpecies, false); }
/// <summary> /// Removes a species from the environment. All molecule instances associated /// with this species are removed. The species is destroyed immediatly /// (ScriptableObject.DestroyImmediate). /// </summary> /// <param name="s">species to remove</param> public void RemoveSpecies(MoleculeSpecies s) { s.Delete(); species.Remove(s); ScriptableObject.DestroyImmediate(s, true); }
/// <summary> /// Adds a new species to the environment. /// </summary> /// <param name="s">species to add</param> public void AddSpecies(MoleculeSpecies s) { species.Add(s); UnityEditor.AssetDatabase.AddObjectToAsset(s, this); }