using System.Collections.Generic; using UnityEngine; using NanoBrain; namespace CreatureControl { public class Mouth : MonoBehaviour { public GameObject foodPrefab; public Brain nanoBrain; public Neuron havingFood; public Nucleus enableFoodPheromones; void Awake() { this.nanoBrain = GetComponentInParent(); if (this.nanoBrain == null || this.nanoBrain.brain == null) return; if (nanoBrain.brain.GetNucleus("Mouth") is Neuron mouthNeuron) mouthNeuron.WhenFiring += CheckGrab; this.havingFood = nanoBrain.brain.GetNeuron("Having Food"); } void Start() { havingFood?.SetBias(Vector3.zero); } protected void CheckGrab() { if (havingFood == null) return; Collider[] colliders = Physics.OverlapSphere(this.transform.position, 0.04f); foreach (Collider c in colliders) { if (havingFood.outputValue.x > 0) { AntsNest nest = c.GetComponentInParent(); if (nest != null) LetGo(); } else { Food food = c.GetComponentInParent(); if (food != null) Grab(); } } } public bool Grab() { Debug.Log($"{this.transform.parent.name} Grab food"); havingFood?.SetBias(Vector3.one); GameObject food = Instantiate(foodPrefab); food.transform.SetParent(this.transform, false); food.transform.localPosition = Vector3.zero; return true; } public void LetGo() { Debug.Log($"{this.transform.parent.name} Release food"); List toDelete = new(); for (int i = 0; i < transform.childCount; i++) toDelete.Add(transform.GetChild(i).gameObject); foreach (GameObject go in toDelete) Destroy(go); havingFood?.SetBias(Vector3.zero); } } }