示例#1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //맵 데이터를 파싱
            if (mNavigationData != null)
            {
                if (!mNavigationData.Load("map.txt"))
                {
                    MessageBox.Show("failed to loading map data");
                }
                else
                {
                    for (int i = 0; i < MAX_UNIT; ++i)
                    {
                        if (!mUnits[i].IsTracking())
                        {
                            mUnits[i].RandomPos(mNavigationData);
                        }
                    }
                }
            }

            //맵의 셀개수를 기반으로 클라이언트 사이즈를 조정합니다. 윈도우의 크기는 (셀의 가로크기 * 셀사이즈) * (셀의 세로크기 * 셀사이즈) 가됩니다.
            this.ClientSize = new System.Drawing.Size(mNavigationData.GetWidth() * CELL_SIZE, mNavigationData.GetHeight() * CELL_SIZE);

            mTimer          = new Timer();
            mTimer.Interval = 1;
            mTimer.Tick    += new EventHandler(Update);
            mTimer.Enabled  = true;

            //맵을 비트맵에 그리기 위해서 맵의 픽셀크기 만큼의 비트맵을 만듭니다.
            mBitmap         = new Bitmap(mNavigationData.GetWidth() * CELL_SIZE, mNavigationData.GetWidth() * CELL_SIZE);
            mGraphicsBuffer = Graphics.FromImage(mBitmap);
        }
示例#2
0
 public void RandomPos(CNavigationData ND)
 {
     while (true)
     {
         int cx = m_RND.Next(ND.GetWidth());
         int cy = m_RND.Next(ND.GetHeight());
         if (ND.IsValidPos(cx, cy))
         {
             m_iPos[0] = cx;
             m_iPos[1] = cy;
             break;
         }
     }
 }
示例#3
0
        //맵에서 임의의 위치를 선택해서 현재위치를 설정하기
        public void RandomPos(CNavigationData ND)
        {
            //유효한 임의의 위치를 구할때까지 반복
            while (true)
            {
                //임의의 위치를 구하고
                int cx = m_RND.Next(ND.GetWidth());
                int cy = m_RND.Next(ND.GetHeight());

                //해당 위치가 이동가능한 위치면, 유닛 좌표를 해당위치로 설정한후 while 종료
                if (ND.IsValidPos(cx, cy))
                {
                    m_iPos[0] = cx;
                    m_iPos[1] = cy;
                    break;
                }
            }
        }
示例#4
0
        private void DrawMap()
        {
            if (m_ND == null)
            {
                return;
            }

            int cx, cy;

            for (int y = 0, py = 0; y < m_ND.GetHeight(); ++y, py += 16)
            {
                for (int x = 0, px = 0; x < m_ND.GetWidth(); ++x, px += 16)
                {
                    cx = x * 16;
                    cy = y * 16;
                    if (m_ND.IsValidPos(x, y))
                    {
                        m_Buf.FillRectangle(Brushes.Yellow, (float)cx, (float)cy, 16.0f, 16.0f);
                    }
                    else
                    {
                        m_Buf.FillRectangle(Brushes.Red, (float)cx, (float)cy, 16.0f, 16.0f);
                    }
                }
            }

            for (int i = 0; i < MAX_UNIT; ++i)
            {
                cx = m_pUnits[i].GetX() * 16;
                cy = m_pUnits[i].GetY() * 16;

                if (m_pUnits[i].IsTracking())
                {
                    m_Buf.FillRectangle(Brushes.Blue, (float)cx, (float)cy, 16.0f, 16.0f);
                }
                else
                {
                    m_Buf.FillRectangle(Brushes.Green, (float)cx, (float)cy, 16.0f, 16.0f);
                }
            }
            Graphics g = CreateGraphics();

            g.DrawImage(m_Bitmap, 0.0f, 0.0f);
        }
示例#5
0
        public void Update(CNavigationData ND, CAStarPathFinding FF)
        {
            if (!m_bMove)
            {
                if (m_Target != null)
                {//
                    int dx = m_iPos[0] - m_Target.GetX();
                    int dy = m_iPos[1] - m_Target.GetY();

                    if (m_bTracking)
                    {//타겟을 추적하는 모드라면
                        if (m_bStartTracking)
                        {
                            if (Math.Abs(dx) > 3 || Math.Abs(dy) > 3)
                            {
                                CNaviNode pStart = CNaviNode.Create(m_iPos[0], m_iPos[1]);
                                CNaviNode pEnd   = CNaviNode.Create(m_Target.GetX(), m_Target.GetY());
                                m_vecPath = new List <CNaviNode>();
                                if (!FF.FindPath(pStart, pEnd, ref m_vecPath, ND))
                                {
                                }
                                else
                                {
                                    m_bMove = true;
                                }
                            }
                        }
                    }
                    else
                    {//타겟을 회피하는 모드다
                        if (Math.Abs(dx) < 4 && Math.Abs(dy) < 4 && !m_bMove)
                        {
                            while (true)
                            {
                                int cx = m_RND.Next(ND.GetWidth());
                                int cy = m_RND.Next(ND.GetHeight());
                                if (ND.IsValidPos(cx, cy))
                                {
                                    m_vecPath = new List <CNaviNode>();
                                    CNaviNode pStart = CNaviNode.Create(m_iPos[0], m_iPos[1]);
                                    CNaviNode pEnd   = CNaviNode.Create(cx, cy);
                                    if (!FF.FindPath(pStart, pEnd, ref m_vecPath, ND))
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        m_bMove = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (m_vecPath == null)
                {
                    m_bMove = false;
                }
                else
                {
                    int curindex = m_vecPath.Count - 1;
                    if (curindex < 0)
                    {
                        m_bMove   = false;
                        m_vecPath = null;
                    }
                    else
                    {
                        m_iPos[0] = m_vecPath[curindex].x;
                        m_iPos[1] = m_vecPath[curindex].y;
                        m_vecPath.RemoveAt(curindex);
                    }
                }
            }
        }
示例#6
0
        //매프레임 호출되어 유닛을 이동시킴
        public void Update(CNavigationData ND, CAStarPathFinding FF)
        {
            //현재 이동중이지 않을 경우
            if (!m_bMove)
            {
                //묙표유닛이 존재한다면
                if (m_Target != null)
                {//
                    //묙표유닛과 자신과의 거리를 구한다
                    int dx = m_iPos[0] - m_Target.GetX();
                    int dy = m_iPos[1] - m_Target.GetY();

                    if (m_bTracking)
                    {//타겟을 자동추적하는 모드라면
                        //추적중이라면
                        if (m_bStartTracking)
                        {
                            //목표와의 거리가 3셀이상이면
                            if (Math.Abs(dx) > 3 || Math.Abs(dy) > 3)
                            {
                                //나의 위치를 시작점, 목표위치를 끝점으로해서
                                CNaviNode pStart = CNaviNode.Create(m_iPos[0], m_iPos[1]);
                                CNaviNode pEnd   = CNaviNode.Create(m_Target.GetX(), m_Target.GetY());

                                //경로를 구하고
                                m_vecPath = new List <CNaviNode>();
                                if (!FF.FindPath(pStart, pEnd, ref m_vecPath, ND))
                                {
                                }
                                else
                                {
                                    //경로가 구해졌으면 유닛이동을 활성화
                                    m_bMove = true;
                                }
                            }
                        }
                    }
                    else
                    {//타겟을 회피하는 모드다
                        //목표와의 거리가 4셀 이하이고, 현재 이동중이지 않으면
                        if (Math.Abs(dx) < 4 && Math.Abs(dy) < 4 && !m_bMove)
                        {
                            //무한반복
                            while (true)
                            {
                                //맵에서 임의의 위치를 하나 선택하고
                                int cx = m_RND.Next(ND.GetWidth());
                                int cy = m_RND.Next(ND.GetHeight());

                                //해당 위치가 이동가능한곳이면
                                if (ND.IsValidPos(cx, cy))
                                {
                                    //유닛의 현재 위치를 시작점, 위에서 선택한 임의의 위치를 끝점으로 해서
                                    CNaviNode pStart = CNaviNode.Create(m_iPos[0], m_iPos[1]);
                                    CNaviNode pEnd   = CNaviNode.Create(cx, cy);

                                    //경로를 구하고
                                    m_vecPath = new List <CNaviNode>();
                                    if (!FF.FindPath(pStart, pEnd, ref m_vecPath, ND))
                                    {
                                        //경로가 구해지지 않았으면 while 루프를 다시 반복
                                        continue;
                                    }
                                    else
                                    {
                                        //경로가 구해졌으면 유닛을 이동상태로 설정하고, while 루프를 종료
                                        m_bMove = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else //유닛이 현재 이동중인경우면
            {
                //경로가 존재하지 않으면, 이동모드를 중지
                if (m_vecPath == null)
                {
                    m_bMove = false;
                }
                else
                {
                    int curindex = m_vecPath.Count - 1;

                    //경로의 목표점에 도달했으면
                    if (curindex < 0)
                    {
                        //이동모드를 중지하고, 경로는 클리어
                        m_bMove   = false;
                        m_vecPath = null;
                    }
                    else
                    {
                        //경로의 현재 점을 유닛의 위치로 설정하고, 사용한 좌표는 경로 목록에서 제거하기
                        m_iPos[0] = m_vecPath[curindex].x;
                        m_iPos[1] = m_vecPath[curindex].y;
                        m_vecPath.RemoveAt(curindex);
                    }
                }
            }
        }