Пример #1
0
    public bool FindPath(int heroID, Building destBuilding)
    {
        CityBlock destBlock   = destBuilding.GetComponentInParent <CityBlock> ();
        int2      destination = destBlock.GridPos() * blockSize + destBuilding.door.OutsideTile();

        if (FindPath(heroID, destination) == false)
        {
            return(false);
        }

        heroes [heroID].toDoList.Enqueue(new Action(action_type.enter_building, destBuilding.door.gameObject));
        heroes [heroID].path.Enqueue(destBlock.GridPos() * blockSize + destBuilding.door.InsideTile());
        destBuilding.IncrementHeroCount();

        return(true);
    }
Пример #2
0
    public bool FindPath(int heroID, int2 destination)
    {
        if (heroID > heroes.Count)
        {
            return(false);
        }

        CityBlock endBlock;

        if (cityBlocks.TryGetValue(destination.DivideAndFloor(blockSize), out endBlock) == false)
        {
            return(false);
        }

        CityBlock startBlock;
        int2      start = heroes[heroID].gPos;

        if (cityBlocks.TryGetValue(start.DivideAndFloor(blockSize), out startBlock) == false)
        {
            Debug.Assert(false);
        }

        if (endBlock == startBlock)
        {
            return(startBlock.FindPath(start % blockSize, destination % blockSize, heroes[heroID].path));
        }

        Queue <int2> path         = new Queue <int2>();
        CityBlock    currentBlock = startBlock;
        int2         connection1  = new int2();
        int2         connection2  = new int2();

        heroes[heroID].path.Clear();
        start = start % blockSize;

        while (currentBlock != endBlock)
        {
            if (endBlock.GridPos().x > currentBlock.GridPos().x)
            {
                connection1.Set(blockSize - 1, currentBlock.mainConnections[3] * 2);
                if (start.y > connection1.y)
                {
                    connection1.y += 1;
                }
                connection2.Set(blockSize - 1, currentBlock.secondaryConnections[3]);

                if (FindBestPathAndAdd(currentBlock, start, connection1, connection2, heroID, out connection1) == false)
                {
                    return(false);
                }

                start.Set(0, connection1.y);
                if (cityBlocks.TryGetValue(currentBlock.GridPos() + new int2(1, 0), out currentBlock) == false)
                {
                    return(false);
                }
            }
            if (endBlock.GridPos().x < currentBlock.GridPos().x)
            {
                connection1.Set(0, currentBlock.mainConnections[1] * 2);
                if (start.y > connection1.y)
                {
                    connection1.y += 1;
                }
                connection2.Set(0, currentBlock.secondaryConnections[1]);

                if (FindBestPathAndAdd(currentBlock, start, connection1, connection2, heroID, out connection1) == false)
                {
                    return(false);
                }

                start.Set(blockSize - 1, connection1.y);
                if (cityBlocks.TryGetValue(currentBlock.GridPos() + new int2(-1, 0), out currentBlock) == false)
                {
                    return(false);
                }
            }
            if (endBlock.GridPos().y > currentBlock.GridPos().y)
            {
                connection1.Set(currentBlock.mainConnections[2] * 2, blockSize - 1);
                if (start.x > connection1.x)
                {
                    connection1.x += 1;
                }
                connection2.Set(currentBlock.secondaryConnections[2], blockSize - 1);

                if (FindBestPathAndAdd(currentBlock, start, connection1, connection2, heroID, out connection1) == false)
                {
                    return(false);
                }

                start.Set(connection1.x, 0);
                if (cityBlocks.TryGetValue(currentBlock.GridPos() + new int2(0, 1), out currentBlock) == false)
                {
                    return(false);
                }
            }
            if (endBlock.GridPos().y < currentBlock.GridPos().y)
            {
                connection1.Set(currentBlock.mainConnections[0] * 2, 0);
                if (start.x > connection1.x)
                {
                    connection1.x += 1;
                }
                connection2.Set(currentBlock.secondaryConnections[0], 0);

                if (FindBestPathAndAdd(currentBlock, start, connection1, connection2, heroID, out connection1) == false)
                {
                    return(false);
                }

                start.Set(connection1.x, blockSize - 1);
                if (cityBlocks.TryGetValue(currentBlock.GridPos() + new int2(0, -1), out currentBlock) == false)
                {
                    return(false);
                }
            }
        }

        if (endBlock.FindPath(start, destination % blockSize, path) == false)
        {
            return(false);
        }

        while (path.Count > 0)
        {
            heroes[heroID].path.Enqueue(path.Dequeue());
        }

        return(true);
    }