Get it on Google Play


Wm뮤 :: A Star

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


2021. 11. 6. 00:22 Unity/C#

 


    private int[,] _node_checker;
    private float[,] _worth_p_worth2 ;
    private Vector2 _now_pos;
    private int[,] _parent_dir ;
    private int map_wid = 1000;
    private float[] _check_pos_len;
    void Awake()
    {
        _worth_p_worth2 = new float[map_wid, map_wid];
        _node_checker = new int[map_wid, map_wid];
        _check_pos_len = new float[] { 1, 1.41421f, 1, 1.41421f, 1, 1.41421f, 1, 1.41421f };
        
        _parent_dir = new int[map_wid, map_wid];
        _now_pos = transform.position;
    }
    public int sc_8search(Vector2 pos)
    {//sc_8search(x,y);
        int _return = 0, _x = (int)pos.x, _y = (int)pos.y;
        int check_x, check_y, i, _t;


        for (i = 0; i < 8; i++)
        {
            check_x = _x + _check_pos_x[i];
            check_y = _y + _check_pos_y[i];
            if ((check_x < 0) || (check_x >= map_wid)
            || (check_y < 0) || (check_y >= map_wid))
            {
                continue;
            }

            if ((check_x == (int)move_point.x) && (check_y == (int)move_point.y))
            {
                _parent_dir[check_x, check_y] = i;
                return 2;
            }
            if (_node_checker[check_x, check_y] <= 0)
            {
                float f_t = _worth[_x, _y] + _check_pos_len[i];
                if (f_t < _worth[check_x, check_y])
                {
                    _worth[check_x, check_y] = f_t;
                    if (_worth2[check_x, check_y] == -4)
                    {
                        _worth2[check_x, check_y] = Vector2.Distance(new Vector2(check_x, check_y), move_point);
                    }
                    _worth_p_worth2[check_x, check_y] = _worth[check_x, check_y] + _worth2[check_x, check_y];

                    _parent_dir[check_x, check_y] = i;
                    _return = 1;
                }
                _node_checker[check_x, check_y] = -1;
            }
        }
        _node_checker[_x, _y] = 2;
        return _return;
    }
    public void a_star(Vector2 pos)
    {
        for (int k = 0; k < 10000; k++)
        {
            //if(sc_8search_len(_now_x,_now_y,8)==2)
            if (sc_8search(pos) == 2)
            {
                break;
            }
            bool _t = true;
            for (int i = 0; i < map_wid; i++)
            {
                for (int j = 0; j < map_wid; j++)
                {
                    if (_node_checker[i, j] == -1)
                    {
                        if (_t)
                        {
                            _now_pos.x = i;
                            _now_pos.y = j;
                            _t = false;
                        }
                        else
                        {
                            if (_worth_p_worth2[i, j] < _worth_p_worth2[(int)_now_pos.x, (int)_now_pos.y])
                            {
                                _now_pos.x = i;
                                _now_pos.y = j;
                            }
                        }
                    }
                }
            }
            //if (_t)
            {
                //break;
            }
        }
    }

 

 

너무 오래전에 짜서 개 난잡하게 되어있다... 후 언제 정리하지

구조가 거지같아서 그렇지 성능은 꽤나 최적화 되어있던걸로 기억한다

'Unity > C#' 카테고리의 다른 글

C# 메일  (0) 2021.11.18
유니티 기울기 관련  (0) 2021.10.23
유니티 RectTransform  (0) 2021.10.16
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입