예제 #1
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);
    }