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

📄 gameobject.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
字号:
// Object.cpp: implementation of the CObject class.
//
//////////////////////////////////////////////////////////////////////


//#include "MapEdit.h"
#include "GameObject.h"

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



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGameObject::CGameObject()
{
	m_objType = OT_UnDefined;
	m_nHeight = 0;
	m_nWidth = 0;
	m_nX = 0;
	m_nY = 0;
	m_nTextureIndex = -1;
	m_nIndex = -1;
	m_bActive = false;
//	m_pTexture = NULL;	// DELETE by vigame, 2007-7-8
	m_nHp = 10000;	// To set to a biggish value, so we "can't" destory it.
}

CGameObject::~CGameObject()
{
	//	SAFE_DELETE(m_pTexture);	// DELETED by vigame, 2007-7-8, we release texture at ~CObjectTexture()	
	m_pTexture = NULL;
}

// Overload the operator //////////////////////////////////////////////

bool CGameObject::operator <(const CGameObject &otherObject) const
{
	return ((m_nY + m_nHeight) < (otherObject.m_nY + otherObject.m_nHeight));
}

bool CGameObject::operator >(const CGameObject &otherObject) const
{
	return ((m_nY + m_nHeight) > (otherObject.m_nY +otherObject.m_nHeight));
}

// Get Size ///////////////////////////////////////////////////////////

int CGameObject::GetHeight()
{
	return m_nHeight;
}

int CGameObject::GetWidth()
{
	return m_nWidth;
}

// Draw /////////////////////////////////////////////////////////////////////

void CGameObject::DrawSprite(LPD3DXSPRITE &pSprite, CONST RECT *pSrcRect, CONST D3DXVECTOR3 *pPosition)
{
	if (NULL != pSprite)
	{
		if (SUCCEEDED(pSprite->Begin(D3DXSPRITE_ALPHABLEND)))
		{
			pSprite->Draw(m_pTexture, pSrcRect, NULL, pPosition, 0xffffffff);
			pSprite->End();
		}
	}
}

void CGameObject::Draw(LPDIRECT3DTEXTURE9 &pTexture, LPDIRECT3DDEVICE9 &pDevice, int x, int y)
{
	Draw(m_pTexture, pDevice, x, y, m_nWidth, m_nHeight, D3DCOLOR_ARGB(255, 255, 255, 255));
}

void CGameObject::Draw(LPDIRECT3DDEVICE9 &pDevice)
{
	Draw(m_pTexture, pDevice, m_nX, m_nY, m_nWidth, m_nHeight, D3DCOLOR_ARGB(255, 255, 255, 255));
}

void CGameObject::Draw(LPDIRECT3DDEVICE9 &pDevice, int x, int y)
{
	Draw(m_pTexture, pDevice, x, y, m_nWidth, m_nHeight, D3DCOLOR_ARGB(255, 255, 255, 255));
}

void CGameObject::Draw(LPDIRECT3DTEXTURE9 &pTexture, LPDIRECT3DDEVICE9 &pDevice,	
					   int x, int y, int width, int height, DWORD diffuse)
{
	if ((NULL != pTexture) && (NULL != pDevice))
	{
		CUSTOMVERTEX vertex[4];
		memset(vertex, 0, sizeof(vertex));

		// Set picture position
		vertex[0].x = (float)x;
		vertex[0].y = (float)y;
		vertex[0].z = 0.5f;
		vertex[1].x = (float)(x + width);
		vertex[1].y = (float)y;
		vertex[1].z = 0.5f;
		vertex[2].x = (float)(x + width);
		vertex[2].y = (float)(y + height);
		vertex[2].z = 0.5f;
		vertex[3].x = (float)x;
		vertex[3].y = (float)(y + height);
		vertex[3].z = 0.5f;

		// Set rhw
		vertex[0].rhw = 0.5f;
		vertex[1].rhw = 0.5f;
		vertex[2].rhw = 0.5f;
		vertex[3].rhw = 0.5f;

		// Set diffuse;
		vertex[0].diffuse = diffuse;
		vertex[1].diffuse = diffuse;
		vertex[2].diffuse = diffuse;
		vertex[3].diffuse = diffuse;

		// Set UV position
		vertex[1].tu = 1.0f;
		vertex[2].tu = 1.0f;
		vertex[2].tv = 1.0f;
		vertex[3].tv = 1.0f;

		// Draw object
		pDevice->SetTexture(0 , pTexture);
		pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
		pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, (LPVOID)vertex , sizeof(CUSTOMVERTEX));
	}
}

// Hit test ////////////////////////////////////////////////////////////

BOOL CGameObject::HitTest(CGameObject &inObject)
{
	RECT rect1, rect2, destRect;
	if (OT_Bonus != m_objType)
	{
		rect1.left = m_nX + m_nWidth / 4;
		rect1.right = m_nX + (m_nWidth / 4) *3 ;
		rect1.top = m_nY + (m_nHeight / 4) * 3;
		rect1.bottom = m_nY + m_nHeight;
	}
	else
	{
		rect1.left = m_nX;
		rect1.right = m_nX + m_nWidth;
		rect1.top = m_nY;
		rect1.bottom = m_nY + m_nHeight;
	}
	
	rect2.left = inObject.m_nX;
	rect2.right = inObject.m_nX + inObject.m_nWidth;
	rect2.top = inObject.m_nY;
	rect2.bottom = inObject.m_nY + inObject.m_nHeight;

	// If the object is fense, we should reduce the size in Collision Detection
	// If the object is computer ship, we should change its size a little
	if (OT_Boat_Computer == inObject.m_objType)
	{
		rect2.top = rect2.top + inObject.m_nHeight / 4;
	}
	else if (OT_Island == inObject.m_objType)
	{
		rect2.top = rect2.top + inObject.m_nHeight / 2;
		rect2.left += 30;
		rect2.right -= 30;
	}
	else if (OT_Reef == inObject.m_objType)
	{
		rect2.top += 10;
		rect2.left += 6;
		rect2.right -= 6;
	}
	else
	{
		rect2.top = rect2.top + (inObject.m_nHeight / 4) * 3;
		rect2.left += 6;
		rect2.right -= 6;
	}

	return ::IntersectRect(&destRect, &rect1, &rect2);
}

// Create texture //////////////////////////////////////////////////////
/* DELETED by vigame, 2007-7-8
BOOL CGameObject::CreateTexture(LPDIRECT3DDEVICE9 pDevice, LPCTSTR inFileName, DWORD inColorKey)
{
	if (OT_UnDefined == m_objType)
	{
		return FALSE;
	}

	// To figure out the frames we should create
	int nFrameCount = 0;
	switch (m_objType)
	{
	case OT_Boat_Player:
		{
			nFrameCount = 12;
			break;
		}
	default:
		{
			nFrameCount = 1;
			break;
		}
	}

	m_pTexture = new CObjectTexture [nFrameCount];

	// Create texture now
	int i;
	for (i = 0; i < nFrameCount; i++)
	{
		m_pTexture[i].Create(pDevice, inFileName, inColorKey);
	}

	// Get the size of texture
	m_nWidth = m_pTexture[0].GetWidth();
	m_nHeight = m_pTexture[0].GetHeight();

	return TRUE;
}

*/

⌨️ 快捷键说明

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