Ejemplo n.º 1
0
 public AStarNode2D(AStarCost aStarCost, float cost, int x, int y, AStarNode goalNode = null, AStarNode parent = null )
     : base(parent, goalNode, cost)
 {
     _x = x;
     _y = y;
     _aStarCost = aStarCost;
 }
Ejemplo n.º 2
0
 public AStar(AStarCost aStarCost, int fromX, int fromY, int toX, int toY)
 {
     openList = new Heap();
     closedList = new Heap();
     _solution = new List<AStarNode>();
     AStarNode2D goalNode = new AStarNode2D(aStarCost, 0, toX, toY);
     startNode = new AStarNode2D(aStarCost, 0, fromX, fromY, goalNode);
 }
Ejemplo n.º 3
0
    public AStar(AStarCost aStarCost, int fromX, int fromY, int toX, int toY)
    {
        openList   = new Heap();
        closedList = new Heap();
        _solution  = new List <AStarNode>();
        AStarNode2D goalNode = new AStarNode2D(aStarCost, 0, toX, toY);

        startNode = new AStarNode2D(aStarCost, 0, fromX, fromY, goalNode);
    }
Ejemplo n.º 4
0
    private bool GetPositionAStar(AStarCost aStar, Vector2Int to, ref List <AStarCost> routeAStarList, ref List <AStarCost> cacheAStarCostList, MapPresenter mapPresenter, CharacterPresenter characterPresenter)
    {
        List <AStarCost> aroundAStarList = new List <AStarCost>();

        foreach (Vector2Int direction in _directions)
        {
            var p = aStar.position + direction;
            //cacheにあればOpenしない
            if (cacheAStarCostList.Any(aster => aster.position == p) || !mapPresenter.IsCanMoveAStar(direction, aStar.position, characterPresenter.status.type))
            {
                continue;
            }

            AStarCost nowNode = new AStarCost
            {
                position = p,
                cost     = aStar.cost + 1
            };
            nowNode.estimateCost = EstimateCost(nowNode.position, to);
            nowNode.distance     = DistanceCost(nowNode.position, to);
            nowNode.score        = nowNode.cost + nowNode.estimateCost;

            cacheAStarCostList.Add(nowNode);
            aroundAStarList.Add(nowNode);
            //目的地についたら
            if (nowNode.position == to)
            {
                routeAStarList.Add(nowNode);
                return(true);
            }
        }

        if (aroundAStarList.Count < 1)
        {
            return(false);
        }

        cacheAStarCostList.First(_ => _.position == aStar.position).status = 2;

        var nextAroundAStar = aroundAStarList.OrderBy(a => a.score).ThenBy(a => a.distance);

        foreach (var nextAStar in nextAroundAStar)
        {
            if (GetPositionAStar(nextAStar, to, ref routeAStarList, ref cacheAStarCostList, mapPresenter,
                                 characterPresenter))
            {
                routeAStarList.Add(nextAStar);
                return(true);
            }
        }
        return(false);
    }
Ejemplo n.º 5
0
    private Vector2Int[] GetDestinationWayList(Vector2Int from, Vector2Int to, MapPresenter mapPresenter, CharacterPresenter characterPresenter)
    {
        AStarCost nowNode = new AStarCost();

        nowNode.position = from;

        nowNode.cost         = 0;
        nowNode.estimateCost = EstimateCost(nowNode.position, to);
        nowNode.score        = nowNode.cost + nowNode.estimateCost;

        var cacheAStarCostList = new List <AStarCost>()
        {
            nowNode
        };
        var routeAStarList = new List <AStarCost>();

        GetPositionAStar(nowNode, to, ref routeAStarList, ref cacheAStarCostList, mapPresenter, characterPresenter);
        if (routeAStarList.Count > 0)
        {
            routeAStarList.Reverse();
            return(routeAStarList.Select(_ => _.position).ToArray());
        }
        return(new Vector2Int[] { });
    }
Ejemplo n.º 6
0
 public AStarNode2D(AStarCost aStarCost, float cost, int x, int y, AStarNode goalNode = null, AStarNode parent = null) : base(parent, goalNode, cost)
 {
     _x         = x;
     _y         = y;
     _aStarCost = aStarCost;
 }