public void Start() { //this.StatusList = new List<MonoBehaviour>(); this.Hunger = this.gameObject.AddComponent <BHV_Hunger>() as BHV_Hunger; this.Sight = this.gameObject.AddComponent <BHV_Vision>() as BHV_Vision; this.Sight.Belonging = theLivingCreature.belonging; this.Locomotion = this.gameObject.AddComponent <BHV_Motion>() as BHV_Motion; this.Empathy = this.gameObject.AddComponent <BHV_Social>() as BHV_Social; }
//Todo a unique loop to return if I see food or danger or social. //Each update, I refresh my vision: public void Update() { //this.currentVision = new List<Matter>(); //empty list for solving big issue, no time to fix (sight is not updated when food is eaten) //this.currentVision.Clear(); Vector3 position = this.transform.position; //The position of the creature Vector3 EyePosition = TheEye.transform.position; //Debug.Log ("this.Belonging.Population"+this.Belonging.Population.Count); foreach (Matter currentItem in this.Belonging.Population) { //Debug.Log (this.Belonging.Population.IndexOf(currentItem) +" = "+ currentItem.root.name); //remove itself from loop ! if (currentItem.root != this.gameObject) { float d = Vector3.Distance(currentItem.root.transform.position, this.gameObject.transform.position); //Collision detection (TODO: not the right place) if (currentItem.GetType().ToString() != "SimDim.Food") { SimDim.LivingCreature currentSeenGuy = currentItem as SimDim.LivingCreature; //behaviour from Social, but call here to optimize loop: if (d < 0.1f * currentSeenGuy.Age) { //Contact Management. BHV_Social comp = this.gameObject.GetComponent <BHV_Social>() as BHV_Social; //mayve, LATER, we can manage food like any kills. comp.kill(currentSeenGuy); return; } /* * if(d<10.0f) * { //Social Management. * * BHV_See comp = currentSeenGuy.root.GetComponent<BHV_See>() as BHV_See; * foreach(SimDim.Matter cur in this.currentVision) * { * if(cur.root == currentItem.root) * { //I see a guy who is very near (maybe too much?) * * //BHV_Social comp2 = this.gameObject.GetComponent<BHV_Social>() as BHV_Social; * //comp2.salute(currentItem); * //return ; * } * } * * } */ } if (d < SightRange) { // here we are at range //TODO: test if we are at angle Vector3 vSightDirection = EyePosition - position; Vector3 vObjDirection = currentItem.root.transform.position - EyePosition; float DotProduct = Vector3.Dot(vSightDirection, vObjDirection); //Debug.Log ( currentItem.root.name +" ==> "+ DotProduct ); //Debug.Log (currentItem.GetType()); if (DotProduct > 0) { //We are in front of the creature if (!this.currentVision.Contains(currentItem)) { this.currentVision.Add(currentItem); //FOOD MANAGEMENT if (currentItem.GetType().ToString() == "SimDim.Food") //There must be a better way but... NO NET { //Debug.Log (this.gameObject.name+" see food :"+currentItem.root.name); this.FoodList.Add(currentItem as Food); } else { //SOCIAL MANAGEMENT //Debug.Log (currentItem.GetType().ToString()); //Debug.Log (this.gameObject.name+" see :"+currentItem.root.name); } } } else { if (this.currentVision.Contains(currentItem)) { this.currentVision.Remove(currentItem); } } } else { if (this.currentVision.Contains(currentItem)) { this.currentVision.Remove(currentItem); } } } } }