public Form1() { InitializeComponent(); InitializeComponent2(); m_ND = new CNavigationData(); m_PathFinding = new CAStarPathFinding(); m_pUnits = new CUnit[MAX_UNIT]; Random rnd = new Random(); for (int i = 0; i < MAX_UNIT; ++i) { m_pUnits[i] = new CUnit(); if (i == 0) { m_pUnits[i].EnableTracking(true); //m_pUnits[i].StartTracking(true); //주석을 해제 하면 자동 추적한다 } else { m_pUnits[i].EnableTracking(false); m_pUnits[i].SetTarget(m_pUnits[0]); } } }
CUnit[] mUnits = null; //화면에 표시되는 유닛들 public Form1() { InitializeComponent(); InitializeComponent2(); mNavigationData = new CNavigationData(); mAStar = new CAStarPathFinding(); mUnits = new CUnit[MAX_UNIT]; for (int i = 0; i < MAX_UNIT; ++i) { mUnits[i] = new CUnit(); if (i == 0) { //가정 첫번째 유닛을 추적자로 설정 mUnits[i].EnableTracking(true); //m_pUnits[i].StartTracking(true); //주석을 해제 하면 자동 추적한다 } else { mUnits[i].EnableTracking(false); mUnits[i].SetTarget(mUnits[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); } } } }
//매프레임 호출되어 유닛을 이동시킴 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); } } } }