void ReiniciarProcura() { PrimeiraProcura = false; List <GameObject> ListPonto = new List <GameObject>(); // Nao chegou no destino foreach (GameObject SlotGrid in ArrayGrid) { if (SlotGrid.GetComponent <SlotGrid>().Custo == 49) { ListPonto.Add(SlotGrid.gameObject); } } int indice = 0; Valores_ASTAR(ListPonto[indice].gameObject.GetComponent <SlotGrid>().pos_x, ListPonto[indice].gameObject.GetComponent <SlotGrid>().pos_y, ListPonto[indice].gameObject.GetComponent <SlotGrid>().Custo, ListPonto[indice].gameObject.GetComponent <SlotGrid>().Custo + 50); }
void Valores_ASTAR(int Slotx, int Sloty, int IncioIndex, int limiteindex) { Chegou = false; MenorDist = -1; ListaSquare.Clear(); foreach (GameObject SlotGrid in ArrayGrid) { if (PrimeiraProcura) { SlotGrid.gameObject.GetComponent <SlotGrid>().Custo = -1; SlotGrid.gameObject.GetComponent <SlotGrid>().Alvo = false; } SlotGrid.gameObject.GetComponent <SlotGrid>().Marcado = 0; } //Custo do local final =0 hit.collider.gameObject.GetComponent <SlotGrid>().Alvo = true; for (int index = IncioIndex; index < limiteindex; index++) { //Diagonais /* * if(!ArrayGrid[Slotx+1,Sloty+1].gameObject.GetComponent<SlotGrid>().Alvo && ArrayGrid[Slotx+1,Sloty+1].gameObject.GetComponent<SlotGrid>().Custo!=100){ * ArrayGrid[Slotx+1,Sloty+1].gameObject.GetComponent<SlotGrid>().Custo = index; * ArrayGrid[Slotx+1,Sloty+1].gameObject.GetComponent<SpriteRenderer>().color = Color.red; * Dist1 = ArrayGrid[Slotx+1,Sloty+1].gameObject.GetComponent<SlotGrid>().DistanciaJogador; * } * * if(!ArrayGrid[Slotx-1,Sloty+1].gameObject.GetComponent<SlotGrid>().Alvo && ArrayGrid[Slotx-1,Sloty+1].gameObject.GetComponent<SlotGrid>().Custo!=100){ * ArrayGrid[Slotx-1,Sloty+1].gameObject.GetComponent<SlotGrid>().Custo = index; * ArrayGrid[Slotx-1,Sloty+1].gameObject.GetComponent<SpriteRenderer>().color = Color.red; * Dist2 = ArrayGrid[Slotx-1,Sloty+1].gameObject.GetComponent<SlotGrid>().DistanciaJogador; * } * * if(!ArrayGrid[Slotx-1,Sloty-1].gameObject.GetComponent<SlotGrid>().Alvo && ArrayGrid[Slotx-1,Sloty-1].gameObject.GetComponent<SlotGrid>().Custo !=100){ * ArrayGrid[Slotx-1,Sloty-1].gameObject.GetComponent<SlotGrid>().Custo = index; * ArrayGrid[Slotx-1,Sloty-1].gameObject.GetComponent<SpriteRenderer>().color = Color.red; * Dist3 = ArrayGrid[Slotx-1,Sloty-1].gameObject.GetComponent<SlotGrid>().DistanciaJogador; * } * * if(!ArrayGrid[Slotx+1,Sloty-1].gameObject.GetComponent<SlotGrid>().Alvo && ArrayGrid[Slotx+1,Sloty-1].gameObject.GetComponent<SlotGrid>().Custo!=100){ * ArrayGrid[Slotx+1,Sloty-1].gameObject.GetComponent<SlotGrid>().Custo = index; * ArrayGrid[Slotx+1,Sloty-1].gameObject.GetComponent<SpriteRenderer>().color = Color.red; * Dist4 = ArrayGrid[Slotx+1,Sloty-1].gameObject.GetComponent<SlotGrid>().DistanciaJogador; * } */ //HORTOGONAIS if (Sloty - 1 != ProfundidadeGrid) { if (!ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SlotGrid>().Alvo&& ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SlotGrid>().Custo != 100) { ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SlotGrid>().Custo = index; ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SpriteRenderer>().color = Color.red; ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SlotGrid>().Marcado += 1; Dist5 = ArrayGrid[Slotx, Sloty + 1].gameObject.GetComponent <SlotGrid>().DistanciaJogador; } } if (Sloty != 0) { if (!ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SlotGrid>().Alvo&& ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SlotGrid>().Custo != 100) { ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SlotGrid>().Custo = index; ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SpriteRenderer>().color = Color.red; ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SlotGrid>().Marcado += 1; Dist6 = ArrayGrid[Slotx, Sloty - 1].gameObject.GetComponent <SlotGrid>().DistanciaJogador; } } if (Slotx != 0) { if (!ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SlotGrid>().Alvo&& ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SlotGrid>().Custo != 100) { ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SlotGrid>().Custo = index; ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SpriteRenderer>().color = Color.red; ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SlotGrid>().Marcado += 1; Dist7 = ArrayGrid[Slotx - 1, Sloty].gameObject.GetComponent <SlotGrid>().DistanciaJogador; } } if (Slotx - 1 != LarguraGrid) { if (!ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SlotGrid>().Alvo&& ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SlotGrid>().Custo != 100) { ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SlotGrid>().Custo = index; ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SpriteRenderer>().color = Color.red; ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SlotGrid>().Marcado += 1; Dist8 = ArrayGrid[Slotx + 1, Sloty].gameObject.GetComponent <SlotGrid>().DistanciaJogador; } } foreach (GameObject SlotGrid in ArrayGrid) { if ( !SlotGrid.GetComponent <SlotGrid>().Cheio&& SlotGrid.GetComponent <SlotGrid>().Custo == index && SlotGrid.GetComponent <SlotGrid>().Custo != 100 && SlotGrid.GetComponent <SlotGrid>().Marcado == 1 && !SlotGrid.GetComponent <SlotGrid>().Alvo ) { ListaSquare.Add(SlotGrid.GetComponent <SlotGrid>().DistanciaJogador); } } if (ListaSquare.Count == 4) { MenorDist = Mathf.Min(ListaSquare[0], ListaSquare[1], ListaSquare[2], ListaSquare[3]); } else if (ListaSquare.Count == 3) { MenorDist = Mathf.Min(ListaSquare[0], ListaSquare[1], ListaSquare[2]); } else if (ListaSquare.Count == 2) { MenorDist = Mathf.Min(ListaSquare[0], ListaSquare[1]); } else if (ListaSquare.Count == 1) { MenorDist = ListaSquare[0]; } foreach (GameObject SlotGrid in ArrayGrid) { if (SlotGrid.GetComponent <SlotGrid>().DistanciaJogador == MenorDist && SlotGrid.GetComponent <SlotGrid>().Custo == index && SlotGrid.GetComponent <SlotGrid>().Custo != 100 && !SlotGrid.GetComponent <SlotGrid>().Cheio&& SlotGrid.GetComponent <SlotGrid>().Marcado == 1 && !SlotGrid.GetComponent <SlotGrid>().Alvo) { Slotx = SlotGrid.GetComponent <SlotGrid>().pos_x; Sloty = SlotGrid.GetComponent <SlotGrid>().pos_y; //ListaCaminho.Add(SlotGrid.gameObject); //break; } } ListaSquare.Clear(); indexAtual = index; } foreach (GameObject SlotGrid in ArrayGrid) { if (SlotGrid.gameObject.GetComponent <SlotGrid>().OcupadoJogador&& SlotGrid.gameObject.GetComponent <SlotGrid>().Marcado > 0) { Chegou = true; PrimeiraProcura = true; } } if (!Chegou) { ReiniciarProcura(); } }