Esempio n. 1
0
    private void CheckNode(int x, int y)
    {
        //check if out of bounds
        if (x < 0 || x > resolutionX - 1)
        {
            return;
        }
        if (y < 0 || y > resolutionY - 1)
        {
            return;
        }

        Node node = grid[x, y];

        if (node.terrain == TerrainType.Unwalkable)
        {
            return;
        }

        //check if open and closed contains this
        if (closed.Contains(node)) //no work eh?
        {
            return;
        }
        foreach (NodeCom nC in open) //es tu no work
        {
            if (nC.node == node)
            {
                return;
            }
        }

        //add to open
        NodeCom _n    = new NodeCom(node, curNode);
        Vector2 myPos = ConvertNodeToMap(_n.node);

        _n.value += (int)Vector2.Distance(myPos, player.transform.position);
        _n.value += (int)Vector2.Distance(myPos, endPos);
        _n.value *= (int)_n.node.terrain;

        open.Add(_n);
    }
Esempio n. 2
0
    private IEnumerator _GetMapMovement(Node goal, bool load)
    {
        if (!(goal != null))
        {
            yield break;
        }

        open   = new List <NodeCom>();
        closed = new List <Node>();

        //add start node
        startNode = new NodeCom(ConvertMapToNode(player.transform.position), null);
        open.Add(startNode);
        endPos = ConvertNodeToMap(goal);

        int calc = 0;

        curNode = null;
        int x, y;

        while (open.Count > 0)
        {
            open.Sort();

            //remove from open and add to closed
            curNode = open[0];
            //if goal has been reached
            if (curNode.node == goal)
            {
                break;
            }

            open.RemoveAt(0);
            closed.Add(curNode.node);

            //shortcuts
            x = curNode.node.x;
            y = curNode.node.y;

            //check adjecent
            CheckNode(x + 1, y);
            CheckNode(x - 1, y);
            CheckNode(x, y + 1);
            CheckNode(x, y - 1);

            //optimization
            calc++;
            if (calc > calculationsPerFrame)
            {
                calc = 0;
                yield return(null);
            }
        }

        if (curNode.node != goal)
        {
            yield break;
        }

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

        while (curNode != null)
        {
            path.Add(ConvertNodeToMap(curNode.node));
            curNode = curNode.parentNode;
        }

        ready = true;
        player.Move(path, load);
    }