NanoBrain-unitypackage/NucleusArray.cs

67 lines
2.0 KiB
C#

using System.Linq;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class NucleusArray {
[SerializeReference]
private INucleus[] _nuclei;
private ClusterPrefab[] _clusters;
public IEnumerable<INucleus> nuclei {
get {
// if (_nuclei == null)
// return _clusters;
// else if (_clusters == null)
return _nuclei;
// else
// return _nuclei.Concat(_clusters);
}
}
public string name;
public NucleusArray(INucleus nucleus) {
this.name = nucleus.name;
this._nuclei = new INucleus[1];
this._nuclei[0] = nucleus;
this._clusters = new ClusterPrefab[0];
}
public NucleusArray(ClusterPrefab cluster) {
this.name = cluster.name;
this._nuclei = new INucleus[0];
this._clusters = new ClusterPrefab[1];
this._clusters[0] = cluster;
}
public void AddNucleus() {
if (this._nuclei.Length == 0) {
Debug.LogError("Empty perceptoid array, cannot add");
return;
}
int newLength = this._nuclei.Length + 1;
INucleus[] newArray = new INucleus[newLength];
for (int i = 0; i < this._nuclei.Length; i++)
newArray[i] = this._nuclei[i];
if (this._nuclei[0] is INucleus nucleus)
newArray[newLength - 1] = (INucleus) nucleus.Clone();
this._nuclei = newArray;
}
public void RemoveNucleus() {
int newLength = this._nuclei.Length - 1;
if (newLength == 0) {
Debug.LogWarning("Perceptoid array cannot be empty");
return;
}
INucleus[] newPerceptei = new INucleus[newLength];
for (int i = 0; i < newLength; i++)
newPerceptei[i] = this._nuclei[i];
// Delete the last perception
Neuron.Delete(this._nuclei[newLength]);
this._nuclei = newPerceptei;
}
}