예제 #1
0
    public static void SetUpNodes(Vector2 c, int unitOwner, UnitGroup grouping, bool forFloodFill)
    {
        center        = c;
        Tile[,] tiles = GridManager.GetTiles();
        Dictionary <Vector2, Unit> enemyUnits = GridManager.GetUnitsForOwner(BattleManager.GetPlayerIndexAfter(unitOwner));

        nodes = new OldNode[GridManager.Width(), GridManager.Height()];
        for (int x = 0; x < GridManager.Width(); x++)
        {
            for (int y = 0; y < GridManager.Height(); y++)
            {
                Vector2 position = new Vector2(x, y);
                int     moveCost;
                if (enemyUnits.ContainsKey(position) && !forFloodFill)
                {
                    moveCost = -1;
                }
                else
                {
                    moveCost = tiles[x, y].moveCosts[(int)grouping];
                }
                nodes[x, y] = new OldNode(position, moveCost);
            }
        }
        nodes[(int)center.x, (int)center.y].pathCost = 0;
    }
예제 #2
0
    public static List <Vector2> GetMoveCoordsForFloodFill(Vector2 position, int movePoints)
    {
        List <Vector2> coords = new List <Vector2>();

        coords.Add(position);
        queue.Enqueue(nodes[(int)position.x, (int)position.y]);
        while (queue.Count > 0)
        {
            OldNode u = queue.Dequeue();
            foreach (Vector2 direction in directions)
            {
                int vx = (int)(u.position.x + direction.x);
                int vy = (int)(u.position.y + direction.y);
                if (NodeInBounds(vx, vy))
                {
                    OldNode v = nodes[vx, vy];
                    if (v.moveCost > 0)
                    {
                        int newPathCost = u.pathCost + v.moveCost;
                        if (newPathCost < v.pathCost && newPathCost <= movePoints)
                        {
                            v.pathCost = newPathCost;
                            v.trace    = direction;
                            if (!coords.Contains(v.position))
                            {
                                coords.Add(v.position);
                            }
                            queue.Enqueue(v);
                        }
                    }
                }
            }
        }
        return(coords);
    }
예제 #3
0
    public static List <Vector2> GetPathToPoint(Vector2 p)
    {
        List <Vector2> path         = new List <Vector2>();
        int            abortCounter = 0;

        while (p != center)
        {
            Debug.LogFormat("{0},{1}", p.x, p.y);
            OldNode target = nodes[(int)p.x, (int)p.y];
            path.Insert(0, target.trace);
            p -= target.trace;
            abortCounter++;
            if (abortCounter == 50)
            {
                break;
            }
        }
        return(path);
    }
예제 #4
0
    public static List <Vector2> GetMoveCoordsFromNodes()
    {
        List <Vector2> coords = new List <Vector2>();

        coords.Add(unit.xy);
        Dictionary <Vector2, Unit> friendlyUnits = GridManager.GetUnitsForOwner(unit.owner);

        queue.Enqueue(nodes[(int)center.x, (int)center.y]);
        while (queue.Count > 0)
        {
            OldNode u = queue.Dequeue();
            foreach (Vector2 direction in directions)
            {
                int vx = (int)(u.position.x + direction.x);
                int vy = (int)(u.position.y + direction.y);
                if (NodeInBounds(vx, vy))
                {
                    OldNode v = nodes[vx, vy];
                    if (v.moveCost > 0)
                    {
                        int newPathCost = u.pathCost + v.moveCost;
                        if (newPathCost < v.pathCost && newPathCost <= unit.movePoints)
                        {
                            v.pathCost = newPathCost;
                            v.trace    = direction;
                            if (!coords.Contains(v.position) && !friendlyUnits.ContainsKey(v.position))
                            {
                                coords.Add(v.position);
                            }
                            queue.Enqueue(v);
                        }
                    }
                }
            }
        }
        return(coords);
    }