Exemplo n.º 1
0
 /// <summary>
 /// nastaví kill priority podle zdálenosti
 /// 200 je cca max vzdálenost na mapě
 /// </summary>
 public void SetKillPriorities()
 {
     if (aiBase.player1 != null && aiBase.playerNumber != 1)
     {
         killPlayer1Priority = (int)(90 * aiMapLogic.GetDistanceFactor(aiMapLogic.GetDistance(aiBase.gameObject, aiBase.player1.gameObject)));
     }
     if (aiBase.player2 != null && aiBase.playerNumber != 2)
     {
         killPlayer2Priority = (int)(90 * aiMapLogic.GetDistanceFactor(aiMapLogic.GetDistance(aiBase.gameObject, aiBase.player2.gameObject)));
     }
     if (aiBase.player3 != null && aiBase.playerNumber != 3)
     {
         killPlayer3Priority = (int)(90 * aiMapLogic.GetDistanceFactor(aiMapLogic.GetDistance(aiBase.gameObject, aiBase.player3.gameObject)));
     }
     if (aiBase.player4 != null && aiBase.playerNumber != 4)
     {
         killPlayer4Priority = (int)(90 * aiMapLogic.GetDistanceFactor(aiMapLogic.GetDistance(aiBase.gameObject, aiBase.player4.gameObject)));
     }
 }
Exemplo n.º 2
0
    public List <Vector2> GetPathTo(Vector2 target)
    {
        float step = characterColliderWidth;

        List <Vector2>  path         = new List <Vector2>();
        PathNode        startNode    = new PathNode(new Vector2(aiBase.posX, aiBase.posY), 0);
        List <PathNode> visitedNodes = new List <PathNode>();

        bool found          = false;
        int  finalNodeIndex = 0;

        //trying 5 different starting points!!!!!!!!!!!!!!! - only 1 for now
        for (int start = 0; start < 1; start++)
        {
            visitedNodes.Clear();
            switch (start)
            {
            case 0:    //center
                startNode = new PathNode(new Vector2(aiBase.posX, aiBase.posY), 0);
                break;

            case 1:    //left
                startNode = new PathNode(new Vector2(aiBase.posX - step / 2, aiBase.posY), 0);
                break;

            case 2:    //up
                startNode = new PathNode(new Vector2(aiBase.posX, aiBase.posY + step / 2), 0);
                break;

            case 3:    //right
                startNode = new PathNode(new Vector2(aiBase.posX + step / 2, aiBase.posY), 0);
                break;

            case 4:    //down
                startNode = new PathNode(new Vector2(aiBase.posX, aiBase.posY - step / 2), 0);
                break;

            default:
                break;
            }

            visitedNodes.Add(startNode);

            for (int i = 0; i < visitedNodes.Count; i++)
            {
                PathNode currentNode = visitedNodes[i];
                //end process when current node is close to target
                if (aiMapLogic.GetDistance(currentNode.node, target) < step)
                {
                    finalNodeIndex = i;
                    found          = true;
                    break;
                }
                if (i > 3000)
                {
                    finalNodeIndex = 100;
                    break;
                }


                //set neighbouring nodes
                PathNode nodeLeft  = new PathNode(new Vector2(currentNode.node.x - step, currentNode.node.y), i);
                PathNode nodeUp    = new PathNode(new Vector2(currentNode.node.x, currentNode.node.y + step), i);
                PathNode nodeRight = new PathNode(new Vector2(currentNode.node.x + step, currentNode.node.y), i);
                PathNode nodeDown  = new PathNode(new Vector2(currentNode.node.x, currentNode.node.y - step), i);
                //add to list if there is no collision and they are not already in list
                if (!CharacterCollidesBarrier(nodeLeft.node) && !visitedNodes.Contains(nodeLeft) && !CharacterCollidesMine(nodeLeft.node))
                {
                    visitedNodes.Add(nodeLeft);
                }
                if (!CharacterCollidesBarrier(nodeUp.node) && !visitedNodes.Contains(nodeUp) && !CharacterCollidesMine(nodeUp.node))
                {
                    visitedNodes.Add(nodeUp);
                }
                if (!CharacterCollidesBarrier(nodeRight.node) && !visitedNodes.Contains(nodeRight) && !CharacterCollidesMine(nodeRight.node))
                {
                    visitedNodes.Add(nodeRight);
                }
                if (!CharacterCollidesBarrier(nodeDown.node) && !visitedNodes.Contains(nodeDown) && !CharacterCollidesMine(nodeDown.node))
                {
                    visitedNodes.Add(nodeDown);
                }
            }

            if (found)
            {
                break;
            }
        }

        //reversly find the way back to starting node
        int index = finalNodeIndex;

        while (index != 0)
        {
            path.Add(visitedNodes[index].node);
            index = visitedNodes[index].parentIndex;
        }
        //reverse path
        path.Reverse();
        return(path);
    }
Exemplo n.º 3
0
    public Vector2 GetBestShootSpot(Vector2 targetPosition)
    {
        //first try simplest way - due to collider offset bug
        if (CanShootStraightTo(targetPosition))
        {
            return(new Vector2(aiBase.posX, aiBase.posY));
        }

        Vector2 bestHorizontal = new Vector2(targetPosition.x, targetPosition.y);
        Vector2 bestVertical   = new Vector2(targetPosition.x, targetPosition.y);

        Vector2 bestHorizontalUp = new Vector2(
            bestVertical.x, bestVertical.y + aiBase.characterColliderHeight);
        Vector2 bestHorizontalDown = new Vector2(
            bestVertical.x, bestVertical.y - aiBase.characterColliderHeight);

        Vector2 bestVerticalLeft = new Vector2(
            bestVertical.x - aiBase.characterColliderWidth, bestVertical.y);
        Vector2 bestVerticalRight = new Vector2(
            bestVertical.x + aiBase.characterColliderWidth, bestVertical.y);

        while (bestHorizontalDown.y > aiBase.mapMinY &&
               !aiMovementLogic.CharacterCollidesBarrier(bestHorizontalDown) &&
               !aiMovementLogic.CharacterCollidesMine(bestHorizontalDown) &&
               bestHorizontalDown.y > aiBase.posY ||
               aiMovementLogic.CharacterCollidesNotproofBarrier(bestHorizontalDown))
        {
            bestHorizontalDown.y -= 0.1f;
        }


        while (bestHorizontalUp.y < aiBase.mapMaxY &&
               !aiMovementLogic.CharacterCollidesBarrier(bestHorizontalUp) &&
               !aiMovementLogic.CharacterCollidesMine(bestHorizontalUp) &&
               bestHorizontalUp.y < aiBase.posY ||
               aiMovementLogic.CharacterCollidesNotproofBarrier(bestHorizontalUp))
        {
            bestHorizontalUp.y += 0.1f;
        }
        while (
            (bestVerticalLeft.x > aiBase.mapMinX &&
             !aiMovementLogic.CharacterCollidesBarrier(bestVerticalLeft) &&
             !aiMovementLogic.CharacterCollidesMine(bestVerticalLeft) &&
             bestVerticalLeft.x > aiBase.posX) ||
            aiMovementLogic.CharacterCollidesNotproofBarrier(bestVerticalLeft))
        {
            bestVerticalLeft.x -= 0.1f;
        }
        while (bestVerticalRight.x < aiBase.mapMaxX &&
               !aiMovementLogic.CharacterCollidesBarrier(bestVerticalRight) &&
               !aiMovementLogic.CharacterCollidesMine(bestVerticalRight) &&
               bestVerticalRight.x < aiBase.posX ||
               aiMovementLogic.CharacterCollidesNotproofBarrier(bestVerticalRight))
        {
            bestVerticalRight.x += 0.1f;
        }

        List <Vector2> possibleShootSpots = new List <Vector2>();

        possibleShootSpots.Add(bestHorizontalDown);
        possibleShootSpots.Add(bestHorizontalUp);
        possibleShootSpots.Add(bestVerticalLeft);
        possibleShootSpots.Add(bestVerticalRight);

        int   spotIndex      = 0;
        float distanceFromMe = 100; //big enough

        for (int i = 0; i < possibleShootSpots.Count; i++)
        {
            if (distanceFromMe > aiMapLogic.GetDistance(aiBase.gameObject.transform.position, possibleShootSpots[i]))
            {
                spotIndex      = i;
                distanceFromMe = aiMapLogic.GetDistance(aiBase.gameObject.transform.position, possibleShootSpots[i]);
            }
        }
        //Debug.DrawRay(possibleShootSpots[spotIndex], aiBase.up, Color.green);

        return(possibleShootSpots[spotIndex]);
    }