⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapeditdlg.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	ScreenToClient(&point);

	// Whether the mouse is in select box? 
	if ((point.x < (192 + 640)) && (point.x > 192) &&
		(point.y < (26 + 64)) && (point.y > 26))
	{
		int nPosition = (point.x - 192) / 64;
		CClientDC dc(this);
		CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
		CPen *pOldPen = dc.SelectObject(&pen);
		int tox = 192 + 64 * nPosition;
		dc.MoveTo(tox, 27);
		dc.LineTo(tox + 64, 27);
		dc.LineTo(tox + 64, 23 + 64);
		dc.LineTo(tox, 23 + 64);
		dc.LineTo(tox, 27);
		dc.SelectObject(pOldPen);
	}
}

// Draw object selected square //////////////////////////////////////////

void CMapEditDlg::FlagObjectSelected()
{
	if (m_bSelected)
	{
		CClientDC dc(this);
		CPen pen(PS_SOLID, 1, RGB(0, 255, 0));
		CPen *pOldPen = dc.SelectObject(&pen);
		int tox = m_itrSelected->m_nX + 24;
		int toy = m_itrSelected->m_nY + 120;	
		int lineTox = tox + m_itrSelected->m_nWidth;
		int lineToy = toy + m_itrSelected->m_nHeight;
	
		// Is the object selected out of map view?
		if (tox < 24) tox = 24;
		if (toy < 120) toy = 120;
		if (tox > 24 + MAP_WIDTH) tox = 24 + MAP_WIDTH;
		if (toy > 120 + MAP_HEIGHT) toy = 120 + MAP_HEIGHT;
		if (lineTox < 24) lineTox = 24;
		if (lineToy < 120) lineToy = 120;
		if (lineTox > 24 + MAP_WIDTH) lineTox = 24 + MAP_WIDTH;
		if (lineToy > 120 + MAP_HEIGHT) lineToy = 120 + MAP_HEIGHT;
		
		// Draw square
		dc.MoveTo(tox, toy);
		dc.LineTo(lineTox, toy);
		dc.LineTo(lineTox, lineToy);
		dc.LineTo(tox, lineToy);
		dc.LineTo(tox, toy);
	}
}

// Delete object ////////////////////////////////////////////////////////
// If we want to delete an object, we select it, then use the 
// key "delete" it.
void CMapEditDlg::DeleteObject()
{
	if (m_bSelected)
	{
		m_listMapObject.erase(m_itrSelected);
		m_bSelected = false;
	}
}


BOOL CMapEditDlg::PreTranslateMessage(MSG* pMsg)
{
	if((WM_KEYDOWN == pMsg->message) && (VK_DELETE == pMsg->wParam))
	{
		DeleteObject();
	}

	return CDialog::PreTranslateMessage(pMsg);
}

// Reset ////////////////////////////////////////////////////////////////

void CMapEditDlg::Reset()
{
	SetTimer(1, 100, NULL);
	m_nChange = 0;
	m_selectType = ST_Fense;
	m_bLButtonDown = false;
	m_bSelected = false;
	m_listMapObject.clear();
}

// Create map ///////////////////////////////////////////////////////////

void CMapEditDlg::CreateNewMap()
{
	// Reset
	Reset();

	// Create sea object
	CGameObject objectSea;

	objectSea.m_nTextureIndex = 2;
	objectSea.m_nWidth = m_gameResource.m_textureSea[objectSea.m_nTextureIndex].m_nWidth;
	objectSea.m_nHeight = m_gameResource.m_textureSea[objectSea.m_nTextureIndex].m_nHeight;
	objectSea.m_objType = OT_Sea;
	objectSea.m_pTexture = m_gameResource.m_textureSea[objectSea.m_nTextureIndex].m_pTexture;

	// Add it into the list
	m_listSea.push_back(objectSea);
}


/////////////////////////////////////////////////////////////////////////

void CMapEditDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	list<CGameObject>::iterator itr;
	for (itr = m_listSea.begin(); itr != m_listSea.end(); itr++)
	{
		switch (m_nChange)
		{
		case 0:
			{
			//	itr->m_pTexture = m_gameResource.m_textureSea[0].m_pTexture;
				m_nChange++;
				break;
			}
		case 1:
			{
			//	itr->m_pTexture = m_gameResource.m_textureSea[1].m_pTexture;
				m_nChange++;
				break;
			}
		default:
			{
				itr->m_pTexture = m_gameResource.m_textureSea[2].m_pTexture;
				m_nChange = 0;
				break;
			}
		}
	}

	SendMessage(WM_PAINT, NULL, NULL);
	CDialog::OnTimer(nIDEvent);
}

void CMapEditDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here

}

void CMapEditDlg::OnFense() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Fense;
}

void CMapEditDlg::OnBarbette() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Barbette;
}

void CMapEditDlg::OnWall() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Wall;
}

void CMapEditDlg::OnIsaland() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Island;
}

void CMapEditDlg::OnReef() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Reef;
}

void CMapEditDlg::OnSeaweed() 
{
	// TODO: Add your control notification handler code here
	m_selectType = ST_Seaweed;
}



// When we double-clicked the mouse, we'll add a new object into
// the map, the position is 24,120, and then we can move this object
// to where you want to, but remember, you can only move the object
// in "mapview", :)
void CMapEditDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	// Whether the mouse is in select box? 
	if ((point.x < (192 + 640)) && (point.x > 192) &&
		(point.y < (26 + 64)) && (point.y > 26))
	{
		int nPosition = (point.x - 192) / 64;
		switch (m_selectType)
		{
		case ST_Fense:
			{
				if (nPosition < FENSE_COUNT)
				{
					// Setup fense data
					CFense fense;
					fense.m_objType = OT_Fense;
					fense.m_nHp = 5;
					fense.m_nX = 0;
					fense.m_nY = 0;
					fense.m_nWidth = m_gameResource.m_textureFense[nPosition].m_nWidth;
					fense.m_nHeight = m_gameResource.m_textureFense[nPosition].m_nHeight;
					fense.m_nTextureIndex = nPosition;
					fense.m_pTexture = m_gameResource.m_textureFense[nPosition].m_pTexture;

					// Add it into the object list
					m_listMapObject.push_back(fense);
				}
				break;
			}

		case ST_Reef:
			{
				if (nPosition < REEF_COUNT)
				{
					// Setup feef data
					CReef reef;
					reef.m_objType = OT_Reef;
					reef.m_nHp = 6;
					reef.m_nX = 0;
					reef.m_nY = 0;
					reef.m_nWidth = m_gameResource.m_textureReef[nPosition].m_nWidth;
					reef.m_nHeight = m_gameResource.m_textureReef[nPosition].m_nHeight;
					reef.m_nTextureIndex = nPosition;
					reef.m_pTexture = m_gameResource.m_textureReef[nPosition].m_pTexture;
					
					// Add it into the object list
					m_listMapObject.push_back(reef);
				}
				break;
			}

		case ST_Island:
			{
				if (nPosition < ISLAND_COUNT)
				{
					// Setup fense data
					CIsland island;
					island.m_objType = OT_Island;
					island.m_nHp = 10000;
					island.m_nX = 0;
					island.m_nY = 0;
					island.m_nWidth = m_gameResource.m_textureIsland[nPosition].m_nWidth;
					island.m_nHeight = m_gameResource.m_textureIsland[nPosition].m_nHeight;
					island.m_nTextureIndex = nPosition;
					island.m_pTexture = m_gameResource.m_textureIsland[nPosition].m_pTexture;
					
					// Add it into the object list
					m_listMapObject.push_back(island);
				}
				break;
			}

			case ST_Barbette:
			{
				if (nPosition < BARBETTE_COUNT)
				{
					// Setup batbette data
					CBarbette barbette;
					barbette.m_objType = OT_Barbette;
					barbette.m_nHp = 6;
					barbette.m_nX = 0;
					barbette.m_nY = 0;
					barbette.m_nWidth = m_gameResource.m_textureBarbette[nPosition].m_nWidth;
					barbette.m_nHeight = m_gameResource.m_textureBarbette[nPosition].m_nHeight;
					barbette.m_nTextureIndex = nPosition;
					barbette.m_pTexture = m_gameResource.m_textureBarbette[nPosition].m_pTexture;
					
					// Add it into the object list
					m_listMapObject.push_back(barbette);
				}
				break;
			}

			case ST_Wall:
			{
				if (nPosition < WALL_COUNT)
				{
					// Setup wall data
					CWall wall;
					wall.m_objType = OT_Wall;
					wall.m_nHp = 5+ (nPosition / 2) * 5;
					wall.m_nX = 0;
					wall.m_nY = 0;
					wall.m_nWidth = m_gameResource.m_textureWall[nPosition].m_nWidth;
					wall.m_nHeight = m_gameResource.m_textureWall[nPosition].m_nHeight;
					wall.m_nTextureIndex = nPosition;
					wall.m_pTexture = m_gameResource.m_textureWall[nPosition].m_pTexture;
					
					// Add it into the object list
					m_listMapObject.push_back(wall);
				}
				break;
			}
			
			case ST_Seaweed:
			{
				if (0 == nPosition)
				{
					// Setup seaweed data
					CSeaweed seaweed;
					seaweed.m_objType = OT_Seaweed;
					seaweed.m_nHp = 10000;
					seaweed.m_nX = 0;
					seaweed.m_nY = 0;
					seaweed.m_nWidth = m_gameResource.m_textureSeaweed.m_nWidth;
					seaweed.m_nHeight = m_gameResource.m_textureSeaweed.m_nHeight;
					seaweed.m_nTextureIndex = nPosition;
					seaweed.m_pTexture = m_gameResource.m_textureSeaweed.m_pTexture;
						
					// Add it into the object list
					m_listMapObject.push_back(seaweed);
				}
				break;
			}
		}
	}
	CDialog::OnLButtonDblClk(nFlags, point);
}

void CMapEditDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

	CDialog::OnChar(nChar, nRepCnt, nFlags);
}

// IF we want to move an object, we should select it first
void CMapEditDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	// Whether the mouse is in map view window? 
	if ((point.x < (24 + MAP_WIDTH)) && (point.x > 24) && 
		(point.y < (120 + MAP_HEIGHT)) && (point.y > 120))
	{
		// Search the object list, find the object had selected
		for (m_itrSelected = m_listMapObject.begin(); m_itrSelected != m_listMapObject.end(); m_itrSelected++)
		{
			if ((m_itrSelected->m_nX < point.x - 24) && 
				((m_itrSelected->m_nX + m_itrSelected->m_nWidth) > point.x - 24) &&
				(m_itrSelected->m_nY < point.y - 120) && 
				((m_itrSelected->m_nY + m_itrSelected->m_nHeight) > point.y - 120))
			{	
				m_offset.x = point.x - m_itrSelected->m_nX;
				m_offset.y = point.y - m_itrSelected->m_nY;
				m_bLButtonDown = true;
				m_bSelected = true;
				break;	// Find the object, break
			}
			else
			{
				m_bSelected = false;
			}
		}	
	}
	else
	{
		m_bSelected = false;
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CMapEditDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	// Any thing had slected in map view screen?
	if (m_bLButtonDown && m_bSelected)
	{
		m_itrSelected->m_nX = point.x - m_offset.x;
		m_itrSelected->m_nY = point.y - m_offset.y;
	}

	CDialog::OnMouseMove(nFlags, point);
}

void CMapEditDlg::OnCaptureChanged(CWnd *pWnd) 
{
	// TODO: Add your message handler code here
	
	CDialog::OnCaptureChanged(pWnd);
}

int CMapEditDlg::OnCharToItem(UINT nChar, CListBox* pListBox, UINT nIndex) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CDialog::OnCharToItem(nChar, pListBox, nIndex);
}

void CMapEditDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_bLButtonDown = false;
	CDialog::OnLButtonUp(nFlags, point);
}

void CMapEditDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnClose();
}

void CMapEditDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

int CMapEditDlg::OnCompareItem(int nIDCtl, LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CDialog::OnCompareItem(nIDCtl, lpCompareItemStruct);
}

void CMapEditDlg::OnExit()
{
	::PostQuitMessage(0);
}

void CMapEditDlg::OnHelp()
{
	// TODO: 在此添加命令处理程序代码
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

⌨️ 快捷键说明

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