Esempio n. 1
0
    public void RequestPathInstructions(GameObject character, Vector3 location, float jumpH, /*char abilities*/ bool movement, bool jump, bool fall)
    {
        bool       replaced  = false;
        threadLock newLocker = new threadLock(character, location, jumpH, movement, jump, fall);

        for (int i = 1; i < orders.Count; i++)
        {
            if (orders[i].character == character)
            {
                orders[i] = newLocker; replaced = true; break;
            }
        }

        if (!replaced)
        {
            orders.Add(newLocker);
        }
    }
Esempio n. 2
0
    public void FindPath(object threadLocker)
    {
        threadLock a             = (threadLock)threadLocker;
        Vector3    character     = a.charPos;
        Vector3    location      = a.end;
        float      characterJump = a.jump;

        List <instructions> instr = new List <instructions>();

        List <pathNode> openNodes   = new List <pathNode>();
        List <pathNode> closedNodes = new List <pathNode>();
        List <pathNode> pathNodes   = new List <pathNode>();

        ResetLists(); //sets parent to null

        pathNode startNode = new pathNode(EPathNodeType.None, Vector3.zero);

        if (a.usingLadder)
        {
            startNode = getNearestLadderNode(character);
        }
        else
        {
            startNode = getNearestGroundNode(character);
        }

        pathNode endNode = getNearestNode(location);

        /*if a point couldnt be found or if character can't move cancel path*/
        if (endNode == null || startNode == null || !a.canMove)
        {
            a.passed = false;
            a.instr  = instr;
            readyOrders.Add(a);
            return;
        }

        startNode.g = 0;
        startNode.f = Vector3.Distance(startNode.pos, endNode.pos);
        openNodes.Add(startNode);

        pathNode currentNode = new pathNode(EPathNodeType.None, Vector3.zero);

        while (openNodes.Count > 0)
        {
            float lowestScore = float.MaxValue;
            for (int i = 0; i < openNodes.Count; i++)
            {
                if (openNodes[i].f < lowestScore)
                {
                    currentNode = openNodes[i]; lowestScore = currentNode.f;
                }
            }
            if (currentNode == endNode)
            {
                closedNodes.Add(currentNode); break;
            }
            else
            {
                closedNodes.Add(currentNode);
                openNodes.Remove(currentNode);
                if (currentNode.type != EPathNodeType.jump || (currentNode.type == EPathNodeType.jump &&
                                                               Mathf.Abs(currentNode.realHeight - characterJump) < jumpHeightIncrement * 0.92) && characterJump <= currentNode.realHeight + jumpHeightIncrement * 0.08)
                {
                    for (int i = 0; i < currentNode.neighbours.Count; i++)
                    {
                        if (!a.canJump && currentNode.neighbours[i].type == EPathNodeType.jump)
                        {
                            continue;
                        }
                        if (!a.canClimb && currentNode.neighbours[i].type == EPathNodeType.climb)
                        {
                            continue;
                        }
                        if (!a.canFall && currentNode.neighbours[i].type == EPathNodeType.fall)
                        {
                            continue;
                        }
                        if (!a.canPortal && currentNode.neighbours[i].type == EPathNodeType.portal)
                        {
                            continue;
                        }

                        if (currentNode.neighbours[i].parent == null)
                        {
                            currentNode.neighbours[i].g = currentNode.neighbours[i].c + currentNode.g;
                            currentNode.neighbours[i].h = Vector3.Distance(currentNode.neighbours[i].pos, endNode.pos);
                            if (currentNode.neighbours[i].type == EPathNodeType.jump)
                            {
                                currentNode.neighbours[i].h += currentNode.neighbours[i].realHeight;
                            }
                            currentNode.neighbours[i].f      = currentNode.neighbours[i].g + currentNode.neighbours[i].h;
                            currentNode.neighbours[i].parent = currentNode;
                            openNodes.Add(currentNode.neighbours[i]);
                        }
                        else
                        {
                            if (currentNode.g + currentNode.neighbours[i].c < currentNode.neighbours[i].g)
                            {
                                currentNode.neighbours[i].g      = currentNode.neighbours[i].c + currentNode.g;
                                currentNode.neighbours[i].f      = currentNode.neighbours[i].g + currentNode.neighbours[i].h;
                                currentNode.neighbours[i].parent = currentNode;
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < 700; i++)
        {
            if (currentNode.parent == null)
            {
                break;
            }

            if (i > 600)
            {
                Debug.Log("somethingwrong");
            }

            pathNodes.Add(currentNode);
            currentNode = currentNode.parent;
            if (currentNode == startNode)
            {
                pathNodes.Add(startNode);
                break;
            }
        }

        a.passed = pathNodes[0] == endNode;
        pathNodes.Reverse();
        for (int i = 0; i < pathNodes.Count; i++)
        {
            instr.Add(new instructions(pathNodes[i].pos, pathNodes[i].type));
        }

        a.instr = instr;
        readyOrders.Add(a);
    }