68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Passer.CreatureControl {
|
|
|
|
public class Mouth : MonoBehaviour {
|
|
public GameObject foodPrefab;
|
|
public NanoBrain nanoBrain;
|
|
|
|
public Neuron havingFood;
|
|
public Nucleus enableFoodPheromones;
|
|
|
|
void Awake() {
|
|
this.nanoBrain = GetComponentInParent<NanoBrain>();
|
|
if (this.nanoBrain == null || this.nanoBrain.brain == null)
|
|
return;
|
|
|
|
if (nanoBrain.brain.GetNucleus("Mouth") is Neuron mouthNeuron)
|
|
mouthNeuron.WhenFiring += CheckGrab;
|
|
this.havingFood = nanoBrain.brain.GetNucleus("Having Food") as Neuron;
|
|
}
|
|
|
|
void Start() {
|
|
havingFood?.SetBias(-Vector3.one);
|
|
}
|
|
|
|
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<AntsNest>();
|
|
if (nest != null)
|
|
LetGo();
|
|
}
|
|
else {
|
|
Food food = c.GetComponentInParent<Food>();
|
|
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<GameObject> 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.one);
|
|
}
|
|
}
|
|
|
|
} |