📄 childview.cpp
字号:
// ChildView.cpp : implementation of the CChildView class
//
#include "stdafx.h"
#include "MirRobot.h"
#include "ChildView.h"
#include "MainFrm.h"
#include "MirGameMap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChildView
CChildView::CChildView()
{
//pMapData=NULL;
m_MonList.RemoveAll();
m_MovePoints.RemoveAll();
scale=1;
m_bSizeChanged=TRUE;
m_pDC=NULL;
m_pBitmap=NULL;
m_pDC=new CDC;
m_pBitmap=new CBitmap;
InCompute=FALSE;
::InitializeCriticalSection(&m_CS);
}
CChildView::~CChildView()
{
::DeleteCriticalSection(&m_CS);
}
BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
return TRUE;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SetBkMode(TRANSPARENT);
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
CFont font;
font.CreatePointFont(9*10,"宋体");
dc.SelectObject(font);
CMirGameMap*pMap=((CMainFrame*)AfxGetMainWnd())->GetGameMap();
CSize size=pMap->GetMapSize();
CRect r;
GetWindowRect(r);
CPoint p=pMap->GetPersonInfo();
CRect rMap;
rMap.top=(p.y-(r.Height()/scale)/2);
rMap.bottom=(p.y+(r.Height()/scale)/2)+2;
rMap.left=(p.x-(r.Width()/scale)/2);
rMap.right=(p.x+(r.Width()/scale)/2)+2;
CBrush brBlack(RGB(100,100,100));
CBrush brWhite(RGB(255,255,255));
CBrush brRed(RGB(255,0,0));
CBrush brBlue(RGB(0,0,255));
CPen penBlack(PS_SOLID,1,RGB(100,100,100));
CPen penWhite(PS_SOLID,1,RGB(255,255,255));
CPen penRed(PS_SOLID,1,RGB(255,0,0));
CPen penBlue(PS_SOLID,1,RGB(0,0,255));
long i,j;
// 画背景
if(m_bSizeChanged)
{
m_pDC->DeleteDC();
m_pBitmap->DeleteObject();
m_pDC->CreateCompatibleDC(NULL);
m_pBitmap->CreateCompatibleBitmap(&dc,r.Width(),r.Height());
m_pDC->SelectObject(m_pBitmap);
}
if(m_Position!=p || m_bSizeChanged)
{
m_pDC->FillSolidRect(0,0,r.Width(),r.Height(),RGB(255,255,255));
for(i=rMap.left;i<rMap.right;i++)
for(j=rMap.top;j<rMap.bottom;j++)
{
if(i>0 && j>0 && i<size.cx &&j<size.cy)
{
if(pMap->TestMap(i,j))
{
//m_pDC->SelectObject(brWhite);
//m_pDC->SelectObject(penWhite);
m_pDC->SetPixel(i-rMap.left,j-rMap.top,RGB(255,255,255));
}
else
{
//m_pDC->SelectObject(brBlack);
//m_pDC->SelectObject(penBlack);
m_pDC->SetPixel(i-rMap.left,j-rMap.top,RGB(100,100,100));
}
//m_pDC->Rectangle((i-rMap.left)*scale,(j-rMap.top)*scale,
// (i-rMap.left+1)*scale,(j-rMap.top+1)*scale);
//m_pDC->SetPixel(i-rMap.left,j-rMap.top,RGB(100,100,100));
}
}
m_Position=p;
m_bSizeChanged=FALSE;
}
dc.BitBlt(0,0,r.Width(),r.Height(),m_pDC,0,0,SRCCOPY);
// 画自己
i=p.x;j=p.y;
dc.SelectObject(brBlue);
dc.SelectObject(penBlue);
dc.Rectangle((i-rMap.left)*scale-1,(j-rMap.top)*scale-1,
(i-rMap.left+1)*scale+1,(j-rMap.top+1)*scale+1);
//dc.SelectObject(brBlack);
//dc.SelectObject(penBlack);
//dc.Rectangle((i-rMap.left)*scale,(j-rMap.top)*scale,
// (i-rMap.left+1)*scale,(j-rMap.top+1)*scale);
// 画地图上的其他玩家
Enter();
dc.SelectObject(brBlue);
dc.SelectObject(penBlue);
POSITION pos;
tag_MonListItem*pItem;
pos=m_MonList.GetHeadPosition();
while(pos!=NULL)
{
pItem=(tag_MonListItem*)m_MonList.GetNext(pos);
if(pItem!=NULL)
{
if(rMap.PtInRect(pItem->pos))
{
i=pItem->pos.x;
j=pItem->pos.y;
//dc.Rectangle((i-rMap.left)*scale,(j-rMap.top)*scale,
// (i-rMap.left+1)*scale,(j-rMap.top+1)*scale);
m_pDC->SetPixel(i-rMap.left,j-rMap.top,RGB(255,0,0));
}
}
}
dc.SelectObject(brBlue);
dc.SelectObject(penBlue);
tag_MovePoint*pMP;
if(!m_MovePoints.IsEmpty())
{
pos=m_MovePoints.GetHeadPosition();
while(pos!=NULL)
{
pMP=(tag_MovePoint*)m_MovePoints.GetNext(pos);
if(pMP!=NULL)
{
dc.MoveTo((pMP->newPos.x-rMap.left)*scale+scale/2,
(pMP->newPos.y-rMap.top)*scale+scale/2);
dc.LineTo((pMP->oldPos.x-rMap.left)*scale+scale/2,
(pMP->oldPos.y-rMap.top)*scale+scale/2);
}
}
}
Leave();
}
void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
CPoint MapPos;
CPoint p=((CMainFrame*)AfxGetMainWnd())->GetGameMap()->GetPersonInfo();
CRect r;
GetWindowRect(r);
MapPos.x=point.x/scale + p.x-(r.Width()/scale)/2;
MapPos.y=point.y/scale + p.y-(r.Height()/scale)/2;
CString s;
s.Format("%d,%d",MapPos.x,MapPos.y);
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(1,s);
tag_MonListItem*pItem;
POSITION pos;
pos=m_MonList.GetHeadPosition();
//((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(2,"<NONE>");
BOOL bFlag=FALSE;
while(pos!=NULL)
{
pItem=(tag_MonListItem*)m_MonList.GetNext(pos);
if(pItem!=NULL)
{
if(MapPos==pItem->pos)
{
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(2,pItem->name);
bFlag=TRUE;
}
}
}
if(!bFlag)
{
if(((CMainFrame*)AfxGetMainWnd())->GetGameMap()->TestMap(MapPos.x,MapPos.y))
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(2,"目标可以到达!");
else
((CMainFrame*)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(2,"目标是障碍物!");
}
CWnd::OnMouseMove(nFlags, point);
}
void CChildView::OnSize(UINT nType, int cx, int cy)
{
CWnd ::OnSize(nType, cx, cy);
m_bSizeChanged=TRUE;
}
//#define _pArray(x,y) (pArray[x+y*size.cx])
//#define _MinSet(a,b) {a=(a<b?a:b);}
//#define SetMin(a,b) {if(b>a)a=b;}
#define pMap (((CMainFrame*)AfxGetMainWnd())->GetGameMap())
int xofs[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int yofs[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int xofs2[] = { 0, 2, 2, 2, 0, -2, -2, -2 };
int yofs2[] = { -2, -2, 0, 2, 2, 2, 0, -2 };
int m2l[]={0,2,4,6,1,3,5,7};
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
CPoint MapPos;
CPoint p=((CMainFrame*)AfxGetMainWnd())->GetGameMap()->GetPersonInfo();
CRect r;
GetWindowRect(r);
MapPos.x=point.x/scale + p.x-(r.Width()/scale)/2;
MapPos.y=point.y/scale + p.y-(r.Height()/scale)/2;
MoveTo(MapPos);
CWnd ::OnLButtonDown(nFlags, point);
}
VOID CChildView::Enter()
{
EnterCriticalSection(&m_CS);
}
VOID CChildView::Leave()
{
LeaveCriticalSection(&m_CS);
}
void CChildView::MoveTo(CPoint Target)
{
tag_MovePoint*pMP;
CPtrList tmpList[2];
CSize MapSize=pMap->GetMapSize();
long i,j,k,l,m,cx,cy;
cx=MapSize.cx;
cy=MapSize.cy;
long*pArray=new long[cx*cy];
CPoint Current=pMap->GetPersonInfo();
CPoint*pnt;
Enter();
while(!m_MovePoints.IsEmpty())
{
pMP=(tag_MovePoint*)m_MovePoints.RemoveHead();
delete pMP;
}
if(pMap->TestMap(Target.x,Target.y))
{
for(i=0;i<cx*cy;i++)
pArray[i]=cx*cy;
pArray[Current.x+Current.y*cx]=0;
long depth=0;
long x=Current.x;
long y=Current.y;
//InitSearchRoadArray(x,y,0,0,pArray,cx,cy);
pnt=new CPoint(Current);
tmpList[0].AddHead(pnt);
for(k=0;k<cx*cy;k++)
{
while(!tmpList[k%2].IsEmpty())
{
pnt=(CPoint*)tmpList[k%2].RemoveHead();
i=pnt->x;
j=pnt->y;
delete pnt;
if(pArray[i+j*cx]==k)
{
for(m=0;m<8;m++)
{
l=m2l[m];
if(pMap->TestMap(i+xofs[l],j+yofs[l]))
{
if(pArray[(i+xofs[l])+(j+yofs[l])*cx]>k+1)
{
pArray[(i+xofs[l])+(j+yofs[l])*cx]=k+1;
if(i+xofs[l]==Target.x && j+yofs[l]==Target.y)
goto _init_finish;
tmpList[(k+1)%2].AddHead(new CPoint(i+xofs[l],j+yofs[l]));
}
if(pMap->TestMap(i+xofs2[l],j+yofs2[l]))
{
if(pArray[(i+xofs2[l])+(j+yofs2[l])*cx]>k+1)
{
pArray[(i+xofs2[l])+(j+yofs2[l])*cx]=k+1;
if(i+xofs2[l]==Target.x && j+yofs2[l]==Target.y)
goto _init_finish;
tmpList[(k+1)%2].AddHead(new CPoint(i+xofs2[l],j+yofs2[l]));
}
}
}
}
}
}
}
_init_finish:
long Step=0;
bool bFlag=true;
while(Current!=Target)
{
bFlag=true;
Step=pArray[Target.x+Target.y*cx];
for(i=2;i>0;i--)
{
for(m=0;m<8;m++)
{
j=m2l[m];
if(pArray[(Target.x+xofs[j]*i)+(Target.y+yofs[j]*i)*cx]==Step-1)
{
if(pMap->TestMap(Target.x+xofs[j]*i,Target.y+yofs[j]*i))
{
if( (i==1) || (pMap->TestMap(Target.x+xofs[j],Target.y+yofs[j])) )
{
pMP=new tag_MovePoint;
pMP->direct=(j+4)%8;
pMP->isRun=(i==2);
pMP->oldPos.x=Target.x;
pMP->oldPos.y=Target.y;
pMP->newPos.x=Target.x+xofs[j]*i;
pMP->newPos.y=Target.y+yofs[j]*i;
Target.x=pMP->newPos.x;
Target.y=pMP->newPos.y;
m_MovePoints.AddHead(pMP);
bFlag=false;
goto __exit_for;
}
}
}
}
}
__exit_for:
if(bFlag)
{
if(m_MovePoints.IsEmpty())
{
MessageBox("自动行走失败!\n\n原因:未知.","移动命令错误",MB_OK|MB_ICONERROR);
break;
}
else
{
pMP=(tag_MovePoint*)m_MovePoints.GetHead();
if(pMP!=NULL)
{
pArray[Target.x+Target.y*cx]=0xffffff;
Target.x=pMP->oldPos.x;
Target.y=pMP->oldPos.y;
m_MovePoints.RemoveHead();
delete pMP;
//MessageBox("后退");
}
}
}
}
//MessageBox("路径已完成");
if(!m_MovePoints.IsEmpty())
{
//MessageBox("开始行走");
pMP=(tag_MovePoint*)m_MovePoints.GetHead();
if(pMP->isRun)
((CMainFrame*)AfxGetMainWnd())->OnRunDirect((pMP->direct%8));
else
((CMainFrame*)AfxGetMainWnd())->OnWalkDirect((pMP->direct%8));
}
Leave();
RedrawWindow();
}
delete []pArray;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -