📄 game.cpp
字号:
g_device->BeginScene();
//绘制赛道
track.Draw();
//绘制圈数检测器
_circler.Draw();
//绘制障碍物
_obstacle.Draw();
//绘制赛车
car1.Draw();
car2.Draw();
car3.Draw();
car4.Draw();
//绘制AI车的保护罩
if(car2._isAlive)
drawBoundSphere(&car2);
if(car3._isAlive)
drawBoundSphere(&car3);
if(car4._isAlive)
drawBoundSphere(&car4);
//生命,圈数显示
if( _pFont )
{
_pFont->DrawText(20, 10, 0xff000000,car1._lifeString);
_pFont->DrawText(20, 30, 0xff000000,car1._circleString);
/*_pFont->DrawText(120, 30, 0xff000000,car2._circleString);
_pFont->DrawText(220, 30, 0xff000000,car3._circleString);
_pFont->DrawText(320, 30, 0xff000000,car4._circleString);*/
_pFont->DrawText(20, 50, 0xff000000,_timeString);
_pFont->DrawText(20,440, 0xff000000,"Esc:to quit");
_pFont->DrawText(300, 440, 0xff000000,car1._speedString);
}
//3,2,1,go
if(_gameState == GameState_Begin)
{
if( _pFont1 )
{
static bool t1 =true;
static bool t2 =false;
static bool t3 =false;
static bool t4 =false;
if(t1)
{
static Timer time1;
if(time1.Wait(1.0f))
{
t2 = true;
t1 = false;
time1.Init();
}
_pFont1->DrawText(300, 150, 0xffee0000,"3");
}
if(t2)
{
static Timer time2;
if(time2.Wait(1.0f))
{
t3 = true;
t2 = false;
time2.Init();
}
_pFont1->DrawText(300, 150, 0xffee0000,"2");
}
if(t3)
{
static Timer time3;
if(time3.Wait(1.0f))
{
t4 = true;
t3 = false;
time3.Init();
}
_pFont1->DrawText(300, 150, 0xffee0000,"1");
}
if(t4)
{
static Timer time4;
if(time4.Wait(1.0f))
{
t1 = true;
t4 = false;
time4.Init();
_gameState =GameState_Run;
}
_pFont1->DrawText(270, 120, 0xffee0000,"go");
}
}
}//3,2,1,go
if( (_timer.GetLastTime() - _timeLimit) >= d3d::EPSILON )
{
static Timer time5;
if(time5.Wait(1.0f))
{
time5.Init();
_gameState =GameState_Over;
}
if( _gameState != GameState_Over)
_pFont2->DrawText(250, 200, 0xffee0000,"Time up!");
}
if(car1._circleNum >= CIRCLE)
{
static Timer time6;
if(time6.Wait(3.0f))
{
time6.Init();
_gameState =GameState_Over;
}
_freeView = true;
_gameViewType = FREE;
if( _gameState != GameState_Over)
{
_pFont2->DrawText(250, 200, 0xffee0000,"You Win!");
}
}
if(_gameState == GameState_Over)
{
_pFont2->DrawText(250, 200, 0xffee0000,"Replay? Y/N");
}
g_device->EndScene();
g_device->Present(0, 0, 0, 0);
return true;
}
bool Game::GameDisplay(float timeDelta)
{
if(_gameState == GameState_ReBegin )
{
GameShutDown();
//初始化游戏
GameInit();
_gameState = GameState_Begin;
}
if( g_device )
{
//更新世界
UpdateWorld(timeDelta);
// 渲染世界
RenderWorld();
if(_gameState == GameState_Run)
{
_timer.Begin();
//游戏控制
GameControl(timeDelta);
}
}
return true;
}
void Game::setViewToCar(const Car &car)
{
_camera._pos = car._pos;
_camera._look = car._look;
_camera._right = car._right;
_camera._up = car._up;
_camera.pitch(D3DX_PI * 0.25f);
_camera.walk(-50.0f);
_camera.fly(50.0f/*5.0f*/);
_camera.saveViewMatrix();
_camera.setViewTransform();
}
void Game::setViewToTop(void)
{
_camera._pos = D3DXVECTOR3(0.0f, 500.0f, 0.0f);
_camera._look = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
_camera._right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
_camera._up = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
_camera.saveViewMatrix();
_camera.setViewTransform();
}
void Game::setViewFree(float timeDelta)
{
if( KEYDOWN('W') )
_camera.walk(40.0f * timeDelta);
if( KEYDOWN('S') )
_camera.walk(-40.0f * timeDelta);
if( KEYDOWN('A') )
_camera.strafe(-40.0f * timeDelta);
if( KEYDOWN('D') )
_camera.strafe(40.0f * timeDelta);
if( KEYDOWN('R') )
_camera.fly(40.0f * timeDelta);
if( KEYDOWN('F') )
_camera.fly(-40.0f * timeDelta);
if( KEYDOWN('I') )
_camera.pitch(-1.0f * timeDelta);
if( KEYDOWN('K') )
_camera.pitch(1.0f * timeDelta);
if( KEYDOWN('J') )
_camera.yaw(-1.0f * timeDelta);
if( KEYDOWN('L') )
_camera.yaw(1.0f * timeDelta);
if( KEYDOWN('N') )
_camera.roll(1.0f * timeDelta);
if( KEYDOWN('M') )
_camera.roll(-1.0f * timeDelta);
_camera.saveViewMatrix();
_camera.setViewTransform();
}
void Game::checkObstacle(float timeDelta)
{
for(int i =0; i<_obstacle.NUM; i++)
{
//与车碰撞检测
//玩家赛车
if( car1._isAlive && collisionCheck( _obstacle._arribute[i]._bBox, car1._bBox) )
{
car1.Kill();
_obstacle._arribute[i].Kill();
}
//AI车的边界球碰撞(AI车守球保护不受影响)
if( car2._isAlive && collisionCheck(_obstacle._arribute[i]._bBox, car2._bSphere) )
{
_obstacle._arribute[i].Kill();
}
if( car3._isAlive && collisionCheck( _obstacle._arribute[i]._bBox, car3._bSphere))
{
_obstacle._arribute[i].Kill();
}
if( car4._isAlive && collisionCheck( _obstacle._arribute[i]._bBox, car4._bSphere))
{
_obstacle._arribute[i].Kill();
}
//与子弹碰撞检测
if( car1._bullet._isAlive && collisionCheck(car1._bullet._bBox, _obstacle._arribute[i]._bBox) )
{
_obstacle._arribute[i].Kill();
car1._bullet.Kill();
car1._haveBullet = true;
}
if( car2._bullet._isAlive && collisionCheck(car2._bullet._bBox, _obstacle._arribute[i]._bBox) )
{
_obstacle._arribute[i].Kill();
car2._bullet.Kill();
car2._haveBullet = true;
}
if( car3._bullet._isAlive && collisionCheck(car3._bullet._bBox, _obstacle._arribute[i]._bBox) )
{
_obstacle._arribute[i].Kill();
car3._bullet.Kill();
car3._haveBullet = true;
}
if( car4._bullet._isAlive && collisionCheck(car4._bullet._bBox, _obstacle._arribute[i]._bBox) )
{
_obstacle._arribute[i].Kill();
car4._bullet.Kill();
car4._haveBullet = true;
}
}
}
//-----------------------------------------------------------------------------
//
// 全局函数
//-----------------------------------------------------------------------------
//盒子与盒子碰撞
bool collisionCheck(d3d::BoundingBox bBox1, d3d::BoundingBox bBox2)
{
//通过中心点判断不碰撞的情况
float a = D3DXVec3Length( &(bBox1._Nvxz[4]-bBox2._Nvxz[4]) );
if( a > (bBox1._xzlmax+bBox2._xzlmax) )
return false;
//检查bBox1的点是否在bBox2内
for(int i=0; i<4; i++)
{
if( bBox2.isPointInside( bBox1._Nvxz[i]) )
return true;
}
//检查bBox2的点是否在bBox1内
for(int i=0; i<4; i++)
{
if( bBox1.isPointInside( bBox2._Nvxz[i]) )
return true;
}
return false;
}
//球与球碰撞
bool collisionCheck(d3d::BoundingSphere bSphere1, d3d::BoundingSphere bSphere2)
{
D3DXVECTOR3 v = (bSphere1._Ncenter - bSphere2._Ncenter);
float a = D3DXVec3Length( &v );
float b = bSphere1._radius + bSphere2._radius;
if( a <= b)
{
return true;
}
else
return false;
}
//盒子与球碰撞(不健壮!当盒子比球大得多时,检测不精确)
bool collisionCheck(d3d::BoundingBox bBox, d3d::BoundingSphere bSphere)
{
float a = D3DXVec3Length( &(bBox._Nvxz[4]-bSphere._Ncenter) );
if( a > (bBox._xzlmax+bSphere._radius) )
return false;
for(int i=0; i<4; i++)
{
if( bSphere.isPointInside( bBox._Nvxz[i]) )
return true;
}
return false;
}
//点与点
bool ptopCheck( D3DXVECTOR3 v1, D3DXVECTOR3 v2, float dis)
{
float a = D3DXVec3Length( &(v1 - v2) );
if(a > dis)
return false;
else
return true;
}
//检测车是否在赛道内
bool isCarInTrack( Car &car)
{
D3DXVECTOR3 c_pos;
car.GetPosition( &c_pos );
float a = D3DXVec3Length( &c_pos );
if( (a >144) && (a<198) )
return true;
else
return false;
}
//检测子弹是否在赛道内
bool isBulletInTrack( Bullet &bullet)
{
D3DXVECTOR3 b_pos;
bullet.GetPosition( &b_pos );
float a = D3DXVec3Length( &b_pos );
if( (a >141) && (a<200) )
return true;
else
return false;
}
void drawBoundBox(Mymesh* mesh)
{
D3DXMATRIX world = mesh->GetPosMtrls();
g_device->SetTransform(D3DTS_WORLD, &world);
ID3DXMesh* BoxMesh = 0;
D3DXCreateBox(
g_device,
mesh->_bBox._max.x - mesh->_bBox._min.x,
mesh->_bBox._max.y - mesh->_bBox._min.y,
mesh->_bBox._max.z - mesh->_bBox._min.z,
&BoxMesh,
0);
// Draw bounding volume in blue and at 10% opacity
D3DMATERIAL9 blue = d3d::BLUE_MTRL;
blue.Diffuse.a = 0.10f; // 10% opacity
g_device->SetMaterial(&blue);
g_device->SetTexture(0, 0); // disable texture
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
BoxMesh->DrawSubset(0);
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
d3d::Release<ID3DXMesh*>(BoxMesh);
}
void drawBoundSphere(Mymesh* mesh)
{
D3DXMATRIX world = mesh->GetPosMtrls();
g_device->SetTransform(D3DTS_WORLD, &world);
ID3DXMesh* SphereMesh = 0;
D3DXCreateSphere(
g_device,
mesh->_bSphere._radius,
20,
20,
&SphereMesh,
0);
D3DMATERIAL9 white = d3d::WHITE_MTRL;
white.Diffuse.a = 0.10f; // 10% opacity
g_device->SetMaterial(&white);
g_device->SetTexture(0, 0); // disable texture
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
g_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
SphereMesh->DrawSubset(0);
g_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
d3d::Release<ID3DXMesh*>(SphereMesh);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -