NanoBrain-unitypackage/VisualEditor/NanoBrainComponent.cs

39 lines
1.3 KiB
C#

using System;
using UnityEngine;
public class NanoBrainComponent : MonoBehaviour {
public ClusterPrefab defaultBrain;
[NonSerialized]
private Cluster brainInstance;
public Cluster brain {
get {
if (brainInstance == null && defaultBrain != null) {
brainInstance = new Cluster(defaultBrain) {
name = defaultBrain.name + " (Instance)"
};
}
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
if (sc != null) {
UpdateWeight(brainInstance, "Containment", sc.containmentForce);
UpdateWeight(brainInstance, "Cohesion", sc.cohesionForce);
UpdateWeight(brainInstance, "Separation", sc.separationForce);
UpdateWeight(brainInstance, "Alignment", sc.alignmentForce);
}
return brainInstance;
}
}
public static void UpdateWeight(Cluster brain, string name, float weight) {
INucleus root = brain.output;
foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) {
if (synapse.weight != weight) {
synapse.weight = weight;
// Debug.Log($"Updated weight for {name}");
}
}
}
}
}