// Use this for initialization void Start() { AngleRads = Mathf.Deg2Rad * Angle; Radius = 200; VisibleConePoints = new LinkedList <Vector2>(); visibleGameobjects = new List <GameObject>(); targetsAux = new List <GameObject>(); visibleGameobjects.Capacity = 50; Objects = new List <GameObject>(); whatToDoScript = this.GetComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); decisionTargetScript = this.GetComponent <DecisionTarget>(); movementController = GameObject.FindGameObjectWithTag("GameController").GetComponent <BehaviourAdder>(); whatToDoScript = this.GetComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); Objects = VisibleElements.visibleGameObjects; objecthand = this.GetComponent <ObjectHandler>(); personality = this.GetComponent <AIPersonality>(); rememberedObject = new GameObject(); rememberedObject.name = "rememberedObjectPosition"; }
private void checkObjectsWithinCone() { string objectINeedRemember = ObjectINeedRemember(); Vector2 A, B, C; A = VisibleConePoints.First.Value; B = source; C = VisibleConePoints.Last.Value; foreach (var singleObject in Objects) { Debug.DrawLine(A, B); Debug.DrawLine(B, C); Debug.DrawLine(C, A); if (isInTriangleABC(singleObject.transform.position, A, B, C)) { if (singleObject != this.gameObject) { visibleGameobjects.Add(singleObject); } //singleObject.GetComponent<SpriteRenderer>().color = Color.Lerp(Color.white, Color.red, Random.Range(0f, 1f)); } } if (visibleGameobjects.Count > 0) { if (objectINeedRemember != "None") { // = RememberedObject(objectINeedRemember); RoomMemory rM = this.gameObject.GetComponent <RoomMemory>(); if (personality.myMemory.objectWithinRoom.ContainsKey(objectINeedRemember)) { rM.SetDestinyRoom(personality.myMemory.objectWithinRoom[objectINeedRemember]); } //Debug.Log("Quiero ir a por el " + objectINeedRemember + ", que está en " + personality.myMemory.objectWithinRoom[objectINeedRemember]); priorityGO = RememberedObjectInARoom(objectINeedRemember); //Debug.Log("necesito el objeto: " + priorityGO); } else { priorityGO = decisionTargetScript.ChooseTarget(visibleGameobjects, this.gameObject); //Debug.Log("SOY: "+ this.gameObject.name +" PRIORITY GO ES " + priorityGO); } visibleGameobjects.Clear(); //Debug.Log ("priority object= " + priorityGO.name); if (priorityGO == null) // no ha visto nada { moveRandomly(A, C); } else { if (priorityGO.tag == "IA") //lo más prioritario es una persona { #region deccidingReg if (!IDecided) { // Debug.Log("veo una Ia voy a decidir, soy " + this.name ); IDecided = true; priorityGO.GetComponent <VisibilityConeCycleIA>().enabled = false; if (whatToDoScript == null) { whatToDoScript = this.gameObject.AddComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); } else { DestroyImmediate(whatToDoScript); DecisionTreeNode[] oldNodes = this.gameObject.GetComponents <DecisionTreeNode>(); foreach (DecisionTreeNode n in oldNodes) { DestroyImmediate(n); } whatToDoScript = this.gameObject.AddComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); } whatToDoScript.target = priorityGO; string[] behaviours = new string[3] { "Pursue", "AvoidWall", "Face" }; float[] weightedBehavs = { 0.7f, 1, 1 }; GameObject[] targets = { priorityGO, priorityGO, priorityGO }; movementController.addBehavioursOver(this.gameObject, targets, behaviours, weightedBehavs); } #endregion decidingReg } else if (priorityGO.tag == "Player") //lo más prioritario es una persona { #region deccidingReg if (!IDecided) { //Debug.Log ("veo al player"); // Debug.Log("veo una Ia voy a decidir, soy " + this.name ); IDecided = true; // Debug.Log("yo " + this.gameObject.transform + "veo a " + priorityGO + " (target)"); if (whatToDoScript == null) { whatToDoScript = this.gameObject.AddComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); } else { DestroyImmediate(whatToDoScript); whatToDoScript = this.gameObject.AddComponent <DecisionTreeISeeSomeoneWhatShouldIDo>(); } whatToDoScript.target = priorityGO; string[] behaviours = new string[3] { "Pursue", "AvoidWall", "Face" }; float[] weightedBehavs = { 0.7f, 1, 1 }; GameObject[] targets = { priorityGO, priorityGO, priorityGO }; movementController.addBehavioursOver(this.gameObject, targets, behaviours, weightedBehavs); } #endregion decidingReg } else //lo más prioritario es un objeto { if (VisibleElements.visibleGameObjects.Contains(priorityGO)) { //Debug.Log("priorityGo es: " + priorityGO); if (priorityGO.tag == "Object") { objecthand.desiredObject = priorityGO; } GameObject targetGO = (GameObject)Instantiate(ghostTarget, priorityGO.transform.position, Quaternion.identity); targetsAux.Add(targetGO); // Debug.Log("busco el objeto"); //Debug.Log ("Deseo " + objecthand.desiredObject); //objecthand.setDesiredGameObject(priorityGO); string[] behaviours = new string[3] { "Arrive", "AvoidWall", "LookWhereYouAreGoing" }; float[] weightedBehavs = { 0.7f, 1, 1 }; GameObject[] targets = { targetGO, targetGO, targetGO }; movementController.addBehavioursOver(this.gameObject, targets, behaviours, weightedBehavs); DeleteTargetAux(); } else { moveRandomly(A, C); } } } } else { if (this.GetComponent <AgentPositionController>() == null) { Debug.Log("no tengo control de movimiento y lo añado"); this.gameObject.AddComponent <AgentPositionController>(); } if (objectINeedRemember != "None") { //priorityGO = RememberedObject(objectINeedRemember); RoomMemory rM = this.gameObject.GetComponent <RoomMemory>(); if (personality.myMemory.objectWithinRoom.ContainsKey(objectINeedRemember)) { rM.SetDestinyRoom(personality.myMemory.objectWithinRoom[objectINeedRemember]); } //Debug.Log("Quiero ir a por el " + objectINeedRemember + ", que está en " + personality.myMemory.objectWithinRoom[objectINeedRemember]); priorityGO = RememberedObjectInARoom(objectINeedRemember); if (priorityGO != null) { if (priorityGO.tag == "Object") { objecthand.desiredObject = priorityGO; } //Debug.Log("Deseo " + objecthand.desiredObject); GameObject targetGO = (GameObject)Instantiate(ghostTarget, priorityGO.transform.position, Quaternion.identity); targetsAux.Add(targetGO); string[] behaviours = new string[3] { "Arrive", "AvoidWall", "LookWhereYouAreGoing" }; float[] weightedBehavs = { 0.7f, 1, 1 }; GameObject[] targets = { targetGO, targetGO, targetGO }; movementController.addBehavioursOver(this.gameObject, targets, behaviours, weightedBehavs); DeleteTargetAux(); } else { moveRandomly(A, C); } } else { moveRandomly(A, C); } } }