#include "GameStart.h"
CCamera g_Camera;
CObject g_Object;
CMesh *g_pPlane;
CMesh *g_pfloor;
CMesh *g_cloud;
D3DXMATRIXA16 matWorld[5];
BoundingSphere *g_pCollideShpere;
BoundingSphere *g_pCollideplane;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; //顶点缓冲区对象
ID3DXFont* g_pFontCount = NULL;
RECT rectCount;
int count = 0;
HRESULT GS_Init(LPDIRECT3DDEVICE9 &g_pd3dDevice)
{
D3DXVECTOR3 vector;
rectCount.right = 450;
rectCount.left = 400;
rectCount.top = 300;
rectCount.bottom = 320;
g_cloud = new CMesh[5];
g_pCollideShpere = new BoundingSphere[5];
g_pCollideplane = new BoundingSphere;
for (int i = 0 ;i < 5 ; i++)
{
g_cloud[i].GetDevice(g_pd3dDevice);
D3DXMatrixTranslation(&matWorld[i],i*20,0, -i * 40);
vector = D3DXVECTOR3(matWorld[i]._41,matWorld[i]._42,matWorld[i]._43);
g_pCollideShpere[i].SetBoundingSphere(vector,1.0f);
}
g_cloud[0].Create("guided3.x");
g_cloud[1].Create("guided3.x");
g_cloud[2].Create("guided3.X");
g_cloud[3].Create("guided3.x");
g_cloud[4].Create("1.x");
//飞机模型
g_pPlane = new CMesh;
g_pPlane->GetDevice(g_pd3dDevice);
g_pPlane->Create("airplane.x");
g_Object.SetVelocity(0,0,-6.0f);
//if(FAILED(g_Object.SetVertice(g_pd3dDevice, 5.1f , 4.0f , 1.3f ,0x00ff8844)))
if(FAILED(g_Object.SetVertice(g_pd3dDevice, 4.5f ,0x00ff8844)))
{
return E_FAIL;
}
vector = g_Object.GetPosition();
g_pCollideplane->SetBoundingSphere(vector,5.0f);
g_pfloor = new CMesh;
g_pfloor->GetDevice(g_pd3dDevice);
g_pfloor->Create("seafloor.x");
if(FAILED(D3DXCreateFont(g_pd3dDevice,15,15,0,0,0,0,0,0,0,"Arial",&g_pFontCount)))
return E_FAIL;
//创建坐标缓冲区
CUSTOMVERTEX vertices[] =
{
{ -40.0f,-10.0f,0.0f, 0x00ff0000, },
{ 40.0f, -10.0f,0.0f, 0x00ff0000, },
{ -40.0f,-10.0f,0.0f, 0x0000ff00, },
{ -40.0f, 30.0f,0.0f, 0x0000ff00, },
{ -40.0f,30.0f,0.0f, 0x000000ff, },
{ 40.0f, 30.0f,0.0f, 0x000000ff, },
{ 40.0f, 30.0f,0.0f, 0x0000ffff, },
{ 40.0f,-10.0f,0.0f, 0x0000ffff, }
};
if( FAILED( g_pd3dDevice->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
//填充顶点缓冲区
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();
return S_OK;
}
void GS_Render(LPDIRECT3DDEVICE9 &g_pd3dDevice)
{
GS_UpDate();
D3DXMATRIXA16 matW;
for (int i = 0 ; i < 5 ;i++)
{
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld[i]);
g_cloud[i].Render();
}
//显示地板
D3DXMatrixTranslation(&matW,0,0, 0);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matW);
g_pfloor->Render();
//显示飞机
//g_Object.m_ShowCollider = false; //是否显示碰撞框架
g_Object.UpDate(g_pd3dDevice,0.01f);
g_pPlane->Render();
//显示碰撞次数
char strText [20];
int charcount = sprintf(strText,"Count:%d",count);
g_pFontCount->DrawText(NULL, strText, charcount, &rectCount, DT_SINGLELINE|DT_NOCLIP|DT_CENTER|DT_VCENTER, 0xff00ffff);
g_Camera.UpData(g_pd3dDevice);
//显示边框
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false);
D3DXMATRIXA16 matWorld;
D3DXVECTOR3 objectpos = g_Object.GetPosition();
D3DXMatrixTranslation(&matWorld,0, 0, objectpos.z);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld);
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 4 );
}
void GS_UpDate()
{
g_pCollideplane->ReplaceCentre(g_Object.GetPosition());
D3DXVECTOR3 camerapos = g_Camera.GetCameraPos();
srand((unsigned)time(NULL) * 10);
for(int i = 0; i < 5 ; i++)
{
if((camerapos.z - matWorld[i]._43) < -60)
{
if(rand()%2 == 1)
matWorld[i]._41 = rand() % 50 + i*10;
else
matWorld[i]._41 = -rand() % 50 - i*10;
matWorld[i]._43 = camerapos.z - 200;
}
matWorld[i]._43 += 0.1;
//碰撞检测及其相应的操作
g_pCollideShpere[i].ReplaceCentre(matWorld[i]);
if(g_pCollideShpere[i].Overlaps(g_pCollideplane))
{
count++;
if(rand()%2 == 1)
matWorld[i]._41 = rand() % 50 + i*10;
else
matWorld[i]._41 = -rand() % 50 - i*10;
matWorld[i]._43 = camerapos.z - 200;
}
}
g_Camera.GetLinkPosition(g_Object.GetPosition());
/*
D3DXVECTOR3 pos = g_Object.GetPosition();
if(pos.y < -10)
{
MessageBox(NULL, "飞机坠毁", "massage" , MB_OK);
exit(0);
}
*/
}
void GS_Clean()
{
if(g_pFontCount!=NULL)
g_pFontCount->Release();
//释放顶点缓冲区对象
if( g_pVB != NULL )
g_pVB->Release();
for (int i = 0 ;i < 5 ; i++)
{
g_cloud[i].Destory();
}
delete [] g_cloud;
delete [] g_pCollideShpere;
delete g_pCollideplane;
}
void GS_Reset()
{
}
LRESULT GS_MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
g_Object.m_key[wParam] = 1;
break;
case WM_KEYUP:
g_Object.m_key[wParam] = 0;
break;
}
g_Camera.MsgProc( hWnd, msg, wParam, lParam );
return FALSE;
}