Exemple #1
0
        private void setOrder(List <GameObject> playerObjects)
        {
            gameObjects = new List <GameObject>();
            BoardSpec boardSpec  = BoardSpec.getInstance();
            Square    mainSquare = boardSpec.SquareFromWorldPoint(main.transform.position);
            var       items      = new Dictionary <GameObject, int>();

            foreach (GameObject playerObject in playerObjects)
            {
                Player target       = playerObject.GetComponent <Player>();
                Square targetSquare = boardSpec.SquareFromWorldPoint(target.transform.position);
                int    distancia    = BoardSpec.GetDistance(mainSquare, targetSquare);
                items.Add(playerObject, distancia);
            }

            foreach (var item in items.OrderBy(i => i.Value))
            {
                gameObjects.Add(item.Key);
            }
        }
Exemple #2
0
        public IEnumerator CalcularPossibleAttacks()
        {
            BoardSpec boardSpec = BoardSpec.getInstance();

            foreach (GameObject gameObject in gameObjects)
            {
                Player target = gameObject.GetComponent <Player>();
                if (target != null && target.status.isDead)
                {
                    continue;
                }
                Square mainSquare               = boardSpec.SquareFromWorldPoint(main.transform.position);
                Square targetSquare             = boardSpec.SquareFromWorldPoint(target.transform.position);
                int    distanciaEstimadaInimigo = BoardSpec.GetSquareDistance(mainSquare, targetSquare);
                //Ataque Basico
                if (target.isAliado)
                {
                    CheckPossibleAttack(target, null, distanciaEstimadaInimigo);
                }

                List <Habilidade> habilidades = main.action.Habilidades.habilidades;
                foreach (Habilidade habilidade in habilidades)
                {
                    if (habilidade.custoPontoAcao > pontosDeAcao || habilidade.IsOfensiva && !target.isAliado || habilidade.custoSp > main.status.mana)
                    {
                        continue;
                    }
                    if (habilidade.minDistancia == 0 && habilidade.maxDistancia == 0 && target.name != main.name)
                    {
                        continue;
                    }
                    CheckPossibleAttack(target, habilidade, distanciaEstimadaInimigo);
                }
                yield return(null);
            }
            Pathfinding    pathfinding     = null;
            List <Vector2> moves           = null;
            bool           apenasMovimenta = true;

            if (possibleAttacks.Count > 0)
            {
                apenasMovimenta = false;
                Player         target         = null;
                int            cont           = 0;
                int            totalCost      = 0;
                MyDictionary   dict           = new MyDictionary();
                PossibleAttack greatestAttack = null;
                foreach (PossibleAttack possibleAttack in possibleAttacks)
                {
                    if (target == null || target.name != possibleAttack.targetName || possibleAttack.isRanged)
                    {
                        target      = possibleAttack.Target;
                        pathfinding = new Pathfinding();
                        Vector2 pos = new Vector2();
                        if (possibleAttack.isRangedInDanger)
                        {
                            pos = RunAndGun(target, possibleAttack.minDist);
                        }
                        else if (possibleAttack.isRanged)
                        {
                            pos = LongShot(target, possibleAttack.maxDist, possibleAttack.minDist);
                        }
                        else
                        {
                            pos = target.transform.position;
                        }
                        pathfinding.FindPath(main.transform.position, pos);
                        yield return(new WaitUntil(() => pathfinding.ready));

                        moves = pathfinding.getMoveInstructions();
                        yield return(null);
                    }
                    // verificar se distancia no pathfinder é diferente que distanciaEstimada - 1, pois não conta o quadrado do alvo
                    if ((possibleAttack.distanciaEstimada - 1) != moves.Count)
                    {
                        possibleAttack.distanciaEstimada = pathfinding.minSize;
                        possibleAttack.CalcularCustoPontoAcaoEstimado();
                        if (possibleAttack.actionPointCost <= pontosDeAcao)
                        {
                            possibleAttack.CalcularCusto(dificuldade);
                        }
                        else
                        {
                            cont++;
                            continue;
                        }
                    }
                    possibleAttack.moves = moves;
                    dict.Add(cont, totalCost + 1, totalCost + possibleAttack.cost, possibleAttack, possibleAttack.cost);
                    totalCost += possibleAttack.cost;
                    cont++;
                    if (greatestAttack == null || greatestAttack.cost < possibleAttack.cost)
                    {
                        greatestAttack = possibleAttack;
                    }
                }
                if (dict.Count != 0 && totalCost > 0)
                {
                    yield return(null);

                    if (dificuldade.Equals(Dificuldade.MEDIO))
                    {
                        bool continueLoop = true;
                        int  cost         = 60;
                        while (continueLoop)
                        {
                            var listPossibleAttack = dict.Where(myval => myval.Value.cost >= cost);
                            if (listPossibleAttack != null && listPossibleAttack.Count() > 0)
                            {
                                continueLoop = false;
                                dict         = new MyDictionary();
                                cont         = 0;
                                int newTotalCost = 0;
                                foreach (KeyValuePair <int, MyValue> item in listPossibleAttack.ToList())
                                {
                                    dict.Add(cont, newTotalCost + 1, newTotalCost + item.Value.cost, item.Value.possibleAttack, item.Value.cost);
                                    newTotalCost = item.Value.cost;
                                    cont++;
                                }
                                totalCost = newTotalCost;
                            }
                            else
                            {
                                cost -= 10;
                            }
                        }
                    }
                    if (!dificuldade.Equals(Dificuldade.DIFICIL))
                    {
                        int random = Random.Range(1, totalCost);
                        try {
                            // Pega lista
                            IEnumerable <KeyValuePair <int, MyValue> > asd = dict.Where(myval => myval.Value.pos1 <= random && myval.Value.pos2 >= random);
                            var itens = asd.ToList();
                            // Pega primeiro item da lista, pois a lista possui apenas 1 item
                            ataqueEscolhido = itens[0].Value.possibleAttack;
                        } catch (Exception e) {
                            Debug.Log(e);
                            Debug.Log(e.Message);
                            Debug.Log(e.Data);
                            yield break;
                        }
                    }
                    else
                    {
                        ataqueEscolhido = greatestAttack;
                    }
                    if (ataqueEscolhido == null)
                    {
                        apenasMovimenta = true;
                    }
                    else
                    {
                        foreach (Vector2 position in ataqueEscolhido.moves)
                        {
                            main.move.NpcMove((int)position.x, (int)position.y);
                        }
                        main.move.Move();
                        yield return(null);

                        if (ataqueEscolhido.isBasicAttack)
                        {
                            main.action.Attack(ataqueEscolhido.targetName);
                        }
                        else
                        {
                            main.action.Ability(ataqueEscolhido.habilidade.nome, ataqueEscolhido.targetName);
                        }
                        yield return(null);
                    }
                }
                else
                {
                    apenasMovimenta = true;
                }
            }
            if (apenasMovimenta)
            {
                int qtyMoves = -1;
                foreach (GameObject gameObject in gameObjects)
                {
                    Player target = gameObject.GetComponent <Player>();
                    if (target != null && (target.status.isDead || !target.isAliado))
                    {
                        continue;
                    }
                    pathfinding = new Pathfinding();
                    pathfinding.FindPath(main.transform.position, target.transform.position);
                    yield return(new WaitUntil(() => pathfinding.ready));

                    if (qtyMoves == -1 || qtyMoves > pathfinding.minSize)
                    {
                        qtyMoves = pathfinding.minSize;
                        moves    = pathfinding.getMoveInstructions();
                    }
                    yield return(null);
                }
                if (moves != null)
                {
                    foreach (Vector2 position in moves)
                    {
                        main.move.NpcMove((int)position.x, (int)position.y);
                    }
                    main.move.Move();
                }
            }
            yield break;
        }