mapdoc.cpp

来自「一个另类的坦克大战源程序」· C++ 代码 · 共 573 行 · 第 1/2 页

CPP
573
字号
// MapDoc.cpp : implementation of the CMapDoc class
//

#include "stdafx.h"
#include "MapEdit.h"

#include "MapDoc.h"
#include "MapProperty.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////
//白石=1,黑石=2,泥=3,草地=4
//
#include "id_table.inc"

/////////////////////////////////////////////////////////////////////////////
// CMapDoc

IMPLEMENT_DYNCREATE(CMapDoc, CDocument)

BEGIN_MESSAGE_MAP(CMapDoc, CDocument)
	//{{AFX_MSG_MAP(CMapDoc)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapDoc construction/destruction

CMapDoc::CMapDoc()
{
	// TODO: add one-time construction code here
}

CMapDoc::~CMapDoc()
{
	m_Grp_terrain.Destroy();
	for(int i=0;i<10;i++)
		m_epg_obstacle[i].Destroy();
}

BOOL CMapDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	INT	nWidth = 32 ;
	INT nHeight = 32;
	if(!m_map.Create(nWidth,nHeight))
	{
		AfxMessageBox("Create map failed");
		return FALSE;
	}

	if(!m_Grp_terrain.Load(TERRAIN_FILE))
	{
		AfxMessageBox("Load TERRAIN_FILE failed");
		return FALSE;
	}

	CString strFileName;
	int i=0;
	strFileName = g_obstacle_name_table[i];
	while(strFileName.GetLength()!=0)
	{
		strFileName.Format("%s.epg",g_obstacle_name_table[i]);
		if(!m_epg_obstacle[i].Load(strFileName))
		{
			AfxMessageBox("Load object epg failed");
			return FALSE;
		}
		strFileName = g_obstacle_name_table[++i];
	}
	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CMapDoc serialization

void CMapDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar << m_map.m_dwID << m_map.Width() << m_map.Height() ;
		for(int h=0;h<m_map.Height();h++)
		{
			for(int w=0;w<m_map.Width();w++)
			{
				ar << GetTerrainIndex(w,h);
			}
		}
		//ar.Write((void*)m_map.TerrainMap(),m_map.Width()*m_map.Height()*sizeof(TERRAIN_ID));
		ar.Write((void*)m_map.ObstacleMap(),m_map.Width()*m_map.Height()*4*sizeof(OBSTACLE_ID));
	}
	else
	{
		// TODO: add loading code here
		DWORD id ;
		ar >> id;
		if(id!=MAP_ID)
			return ;
		INT32 width,height;
		ar >> width >>height;
		m_map.Create(width,height);
		for(int h=0;h<m_map.Height();h++)
		{
			for(int w=0;w<m_map.Width();w++)
			{
				TERRAIN_INDEX terrain_index ;
				ar >> terrain_index;
				m_map.TerrainMap()[w+h*width] = g_terrain_id_table[terrain_index];
			}
		}
		//ar.Read((void*)m_map.TerrainMap(),m_map.Width()*m_map.Height()*sizeof(TERRAIN_ID));
		ar.Read((void*)m_map.ObstacleMap(),m_map.Width()*m_map.Height()*4*sizeof(OBSTACLE_ID));
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMapDoc diagnostics

#ifdef _DEBUG
void CMapDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMapDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMapDoc commands

void CMapDoc::OnFileNew() 
{
	CMapProperty dlgMap;
	if(dlgMap.DoModal()!=IDOK)
		return ;
	
	UINT nWidth = dlgMap.m_uWidth;
	UINT nHeight = dlgMap.m_uHeight;
	TERRAIN_ID terrain_id;
	terrain_id = g_terrain_id_table[dlgMap.m_nTerrain];

	if(!m_map.Create(nWidth,nHeight,TerrainIndex(terrain_id)))
	{
		AfxMessageBox("Create map failed");
		return ;
	}

	/*
	nWidth *= TERRAIN_WIDTH;
	nHeight *= TERRAIN_HEIGHT;
	if(!m_Surface_map.Create(nWidth,nHeight,SF_CLIP))
	{
		AfxMessageBox("Create surface failed");
		return ;
	}
	m_Surface_map.Clear(0);
	for(int h=0;h<m_map.Height();h++)
		for(int w=0;w<m_map.Width();w++)
			m_Surface_map.Blt(
				m_Grp_terrain,
				GetTerrain(w,h),
				w*TERRAIN_WIDTH,
				h*TERRAIN_HEIGHT
			);
	*/
	UpdateAllViews(NULL);
}

TERRAIN_INDEX CMapDoc::GetTerrainIndex(int p_nx, int p_ny)
{
	if(p_nx < 0 || p_nx >= m_map.Width() || p_ny < 0 || p_ny >= m_map.Height())
	{
		return INVALID_TERRAIN_INDEX;
	}

	return TerrainIndex(m_map.TerrainMap()[p_nx + p_ny * m_map.Width()]);
}

TERRAIN_ID CMapDoc::GetTerrainID(int p_nx, int p_ny)
{
	if(p_nx < 0 || p_nx >= m_map.Width() || p_ny < 0 || p_ny >= m_map.Height())
	{
		return TERRAIN_ID(INVALID_TERRAIN_ID);
	}
	return m_map.TerrainMap()[p_nx + p_ny * m_map.Width()];
}

TERRAIN_INDEX CMapDoc::TerrainIndex(TERRAIN_ID p_terrain_id)
{
	TERRAIN_INDEX terrain_index=0;
	TERRAIN_ID terrain_id=g_terrain_id_table[terrain_index];

	while(terrain_id.TileID!=0)
	{
		if(p_terrain_id==terrain_id)
			return terrain_index ;
		terrain_id=g_terrain_id_table[++terrain_index];
	}
	return INVALID_TERRAIN_INDEX;
}

BOOL CMapDoc::TestTerrain(int p_nx, int p_ny, TERRAIN_ID p_terrain_id)
{
	BOOL bCanSet = TRUE;
	TERRAIN_ID terrain_id;
	int x,y;

	x=p_nx, y=p_ny;

	terrain_id = GetTerrainID(x,y);
	if(terrain_id==INVALID_TERRAIN_ID){
		return FALSE;
	}

	if(TerrainIndex(p_terrain_id)==INVALID_TERRAIN_INDEX)
		return FALSE;

	//_______________________________________________
	x=p_nx-1, y=p_ny-1;

	terrain_id = GetTerrainID(x,y);
	if(terrain_id!=INVALID_TERRAIN_ID){
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;

		terrain_id.CornerID[CORNER_RIGHT_BOTTOM]=p_terrain_id.CornerID[CORNER_LEFT_TOP];
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;
	}

	//___________________________
	x=p_nx, y=p_ny-1;

	terrain_id = GetTerrainID(x,y);
	if(terrain_id!=INVALID_TERRAIN_ID){
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;

		terrain_id.CornerID[CORNER_RIGHT_BOTTOM]=p_terrain_id.CornerID[CORNER_RIGHT_TOP];
		terrain_id.CornerID[CORNER_LEFT_BOTTOM]=p_terrain_id.CornerID[CORNER_LEFT_TOP];
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;
	}

	//__________________________________
	x=p_nx+1, y=p_ny-1;

	terrain_id = GetTerrainID(x,y);
	if(terrain_id!=INVALID_TERRAIN_ID){
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;

		terrain_id.CornerID[CORNER_LEFT_BOTTOM]=p_terrain_id.CornerID[CORNER_RIGHT_TOP];
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;
	}
	


	//__________________________________________________
	x=p_nx-1, y=p_ny;

	terrain_id = GetTerrainID(x,y);
	if(terrain_id!=INVALID_TERRAIN_ID){
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;
		terrain_id.CornerID[CORNER_RIGHT_TOP]=p_terrain_id.CornerID[CORNER_LEFT_TOP];
		terrain_id.CornerID[CORNER_RIGHT_BOTTOM]=p_terrain_id.CornerID[CORNER_LEFT_BOTTOM];
		if(TerrainIndex(terrain_id)==INVALID_TERRAIN_INDEX)
			return FALSE;
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?