109 lines
3.8 KiB
C#
109 lines
3.8 KiB
C#
using UnityEngine;
|
|
|
|
namespace NanoBrain {
|
|
|
|
public class ClusterArray : Nucleus {
|
|
|
|
public ClusterPrefab prefab;
|
|
public Cluster[] clusters;
|
|
|
|
public ClusterArray(ClusterPrefab prefab, Cluster parent, int size, Nucleus receiver = null) {
|
|
this.prefab = prefab;
|
|
this.name = prefab.name;
|
|
this.clusters = new Cluster[size];
|
|
for (int ix = 0; ix < size; ix++) {
|
|
Cluster cluster = new(prefab, parent);
|
|
cluster.defaultOutput.AddReceiver(receiver);
|
|
cluster.clusterArray = this;
|
|
this.clusters[ix] = cluster;
|
|
}
|
|
}
|
|
|
|
public ClusterArray(ClusterPrefab prefab, ClusterPrefab parent, int size, Nucleus receiver = null) {
|
|
this.prefab = prefab;
|
|
this.name = prefab.name;
|
|
this.clusters = new Cluster[size];
|
|
for (int ix = 0; ix < size; ix++) {
|
|
Cluster cluster = new(prefab, parent);
|
|
cluster.defaultOutput.AddReceiver(receiver);
|
|
cluster.clusterArray = this;
|
|
this.clusters[ix] = cluster;
|
|
}
|
|
}
|
|
|
|
public override Nucleus ShallowCloneTo(Cluster parent) {
|
|
ClusterArray clone = new(this.prefab, parent, this.clusters.Length) {
|
|
clusterPrefab = this.clusterPrefab,
|
|
};
|
|
|
|
return clone;
|
|
}
|
|
|
|
public override Nucleus Clone(ClusterPrefab parent) {
|
|
ClusterArray clone = new(this.prefab, parent, this.clusters.Length) {
|
|
};
|
|
|
|
return clone;
|
|
}
|
|
|
|
public void Add(ClusterPrefab prefab) {
|
|
if (this.clusters.Length == 0) {
|
|
Debug.LogError("Empty perceptoid array, cannot add");
|
|
}
|
|
int newLength = this.clusters.Length + 1;
|
|
Cluster[] newArray = new Cluster[newLength];
|
|
|
|
string baseName = this.name;
|
|
int colonPos = baseName.IndexOf(":");
|
|
if (colonPos > 0)
|
|
baseName = baseName[..colonPos];
|
|
|
|
for (int i = 0; i < this.clusters.Length; i++)
|
|
newArray[i] = this.clusters[i];
|
|
Cluster cluster = this.clusters[0];
|
|
newArray[newLength - 1] = cluster.Clone(prefab) as Cluster;
|
|
newArray[newLength - 1].name = $"{baseName}: {newLength - 1}";
|
|
}
|
|
|
|
public void Remove() {
|
|
int newLength = this.clusters.Length - 1;
|
|
if (newLength == 0) {
|
|
Debug.LogWarning("Perceptoid array cannot be empty");
|
|
}
|
|
Cluster[] newArray = new Cluster[newLength];
|
|
for (int i = 0; i < newLength; i++)
|
|
newArray[i] = this.clusters[i];
|
|
// Delete the last perception
|
|
//Cluster.Delete(nucleus);
|
|
}
|
|
|
|
public override void UpdateStateIsolated() {
|
|
// Clusters don't do anything,
|
|
// The nuclei in them do the work
|
|
// and should be called directly, not from the cluster
|
|
}
|
|
|
|
public virtual Cluster GetThingCluster() {
|
|
Cluster selectedCluster = SelectCluster();
|
|
return selectedCluster;
|
|
}
|
|
|
|
private Cluster SelectCluster() {
|
|
// Find a sleeping cluster
|
|
foreach (Cluster cluster in clusters) {
|
|
if (cluster.defaultOutput.isSleeping)
|
|
return cluster;
|
|
}
|
|
|
|
// Otherwise find the stalest cluster?
|
|
Cluster stalestCluster = clusters[0];
|
|
for (int ix = 1; ix < clusters.Length; ix++) {
|
|
if (clusters[ix].defaultOutput.stale > stalestCluster.defaultOutput.stale)
|
|
stalestCluster = clusters[ix];
|
|
}
|
|
|
|
return stalestCluster;
|
|
}
|
|
}
|
|
|
|
} |