Mission.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:9k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // Mission.cpp: implementation of the CMission class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Mission.h"
  6. #include "texmanager.h"
  7. #include "AudioManager.h"
  8. #include "imgtext.h"
  9. #include "MediaPlayer.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CMission::CMission()
  14. {
  15. m_3dExplorer=NULL;
  16. m_cTerrain=NULL;
  17. m_cSkyBox=NULL;
  18. m_cHmap=NULL;
  19. m_cInfoPanel=NULL;
  20. m_cLensFlare=NULL;
  21. m_cSprites=NULL;
  22.     m_cBuilding=NULL;
  23. m_cPlant=NULL;
  24. m_pAmbientSound=NULL;
  25. m_pAmmoManager=NULL;
  26.     m_bActivate=false;
  27. m_bResource=false;
  28. }
  29. CMission::~CMission()
  30. {
  31. DeleteMission();
  32. }
  33. bool  CMission::LoadMission()
  34. {
  35. if(m_bResource)return true;
  36. CGameSetting::LoadMissionConfig();
  37.     ///////////////////////////////////
  38. CTexManager cTexManager;
  39.     //////
  40. if(!cTexManager.CreateMissionResource())
  41.         MessageBox(0, "Create textures error", "Error", MB_OK | MB_ICONERROR);
  42.     //////////////Cursor and background
  43. m_texTest = cTexManager.GetTextureID(TEX_MISSION_TEST_0);
  44. /////////////////////////////////////
  45. m_cHmap         = new CHeightmap;
  46. m_3dExplorer    = new C3dE;
  47. m_cTerrain      = new CTerrain;
  48. m_cSkyBox       = new CSkyBox;
  49. m_cInfoPanel    = new CInfoPanel;
  50. if(CGameSetting::m_bLensFlare)
  51.     m_cLensFlare    = new CLensFlare;
  52. m_cSprites      = new CSpriteManager;
  53.     m_cBuilding     = new CBuildingManager;
  54. m_cPlant        = new CPlantManager;
  55. m_pAmbientSound = new CAmbientSounds;
  56. m_pAmmoManager  = new CAmmoManager;
  57. m_cHmap->m_cFrustumCull.InitFrustumCull(40,CGameSetting.m_iVisibleDist);
  58. if(!m_cSkyBox->InitSkyBox(10000,7000,10000,0,-1200,0))
  59. {
  60. MessageBox(0, "cSkyBox error", "Error", MB_OK | MB_ICONERROR);
  61. return FALSE;
  62. }    
  63. if(!m_3dExplorer->Init3dExplorer())
  64. {
  65. MessageBox(0, "read heightmap failed", "Error", MB_OK | MB_ICONERROR);
  66. return FALSE;
  67. }
  68. if(!m_cTerrain->InitTerrain())
  69. {
  70. MessageBox(0, "Init Terrain failed heightmapscene.cpp", "Error", MB_OK | MB_ICONERROR);
  71. return FALSE;
  72. }
  73. if(m_cLensFlare!=NULL)
  74. {
  75.      if(!m_cLensFlare->InitLensFlare())
  76. {
  77.      MessageBox(0, "Init LensFlare failed", "Error", MB_OK | MB_ICONERROR);
  78.      return FALSE;
  79. }
  80. }
  81. if(!m_cSprites->InitSpriteManager())
  82. {
  83. MessageBox(0, "Init Terrain failed", "Error", MB_OK | MB_ICONERROR);
  84. return FALSE;
  85. }
  86. if(!m_cBuilding->InitBuilding())
  87. {
  88. MessageBox(0, "Init building failed", "Error", MB_OK | MB_ICONERROR);
  89. return FALSE;
  90. }
  91. if(!m_cPlant->InitPlantManager())
  92. {
  93. MessageBox(0, "Init plant failed", "Error", MB_OK | MB_ICONERROR);
  94. return FALSE;
  95. }
  96. if(!m_cInfoPanel->InitInfoPanel())
  97. {
  98. MessageBox(0, "Init infopanel failed", "Error", MB_OK | MB_ICONERROR);
  99. return FALSE;
  100. }
  101. if(!m_pAmmoManager->InitAmmoManager())
  102. {
  103. MessageBox(0, "Init ammo failed", "Error", MB_OK | MB_ICONERROR);
  104. return FALSE;
  105. }
  106. ///////////////////////////////////////
  107. ///////// sound 
  108.     CAudioManager  cAudioManager;
  109. cAudioManager.CreateMissionResource();
  110.     /////////////////////
  111.     m_bActivate=true;
  112. m_bResource=true;
  113.     return true;
  114. }
  115. void  CMission::RunMission()
  116. {
  117. if(m_bActivate)return;
  118.     m_pAmbientSound->PlaySounds();
  119. m_3dExplorer->ProcessInput();
  120. m_3dExplorer->TransformWorld(0); 
  121. m_cSkyBox->DrawSkyBox(); 
  122.     m_3dExplorer->TransformWorld(1);
  123. glColor3f(1,1,1);
  124.      ///////////////now render scene
  125. glEnable(GL_CULL_FACE);
  126.     glEnable(GL_LIGHTING);
  127. GLfloat light0_position[]={1.0f,0.5f,-0.8f,0.0f};
  128. glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  129.     glEnable(GL_FOG);
  130.     m_cTerrain->RenderTerrain();
  131.     if(!CInput::m_keys['U'])
  132. {
  133. m_cBuilding->RenderBuilding();
  134. }
  135.     //////////////////////////////////
  136.     glDisable(GL_LIGHTING);
  137. glDisable(GL_CULL_FACE);
  138.     if(!CInput::m_keys['T'])
  139. {
  140. m_cPlant->RenderPlant();
  141. }
  142. glEnable(GL_CULL_FACE);
  143.     glEnable(GL_LIGHTING);
  144.     if(!CInput::m_keys['Y'])
  145. {
  146.     m_cSprites->RenderSprites();
  147. }
  148.     glDisable(GL_LIGHTING);
  149. glDisable(GL_CULL_FACE);
  150.     glDisable(GL_FOG);
  151.     if(m_cLensFlare!=NULL)m_cLensFlare->RenderLensFlare();
  152. m_cBuilding->RenderSmoke();
  153.     m_pAmmoManager->RenderAmmoManager();
  154. /////////////////////////////////////
  155.     ///////////////////Information panel
  156. glLoadIdentity();
  157.     if(m_cHmap->m_myHealth>0)m_cInfoPanel->DrawInfoPanel();
  158. MissionCompleted();
  159. //////////////////////////////////////
  160. if(CInput::m_keys[VK_ESCAPE])
  161. {
  162. CInput::m_keys[VK_ESCAPE]=false;
  163. CGameSetting::m_iGameState=GAME_TEMP_MENU;
  164. return;
  165. }
  166. if(CInput::m_keys[VK_F4])
  167. {
  168. CInput::m_keys[VK_ESCAPE]=false;
  169. CGameSetting::m_iGameState=GAME_DEAD;
  170. return;
  171. }
  172. ////////////////////////////////////
  173. }
  174. void  CMission::PauseMission()
  175. {
  176.     m_bActivate=false;
  177. RestoreFOVAngle();
  178. }
  179. void  CMission::ResumeMission()
  180. {
  181.     m_bActivate=true;
  182.     ///////// restore something
  183. m_cHmap->m_cFrustumCull.InitFrustumCull(40,CGameSetting.m_iVisibleDist);
  184.     m_pAmbientSound->ResetSounds();
  185. }
  186. void CMission::MissionCompleted()
  187. {
  188. /////////////Mission failed
  189. if(m_cHmap->m_myHealth<=0)
  190. {
  191.      glColor3f(0.5f,0.0f,0.0f);
  192.      glBlendFunc(GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR);
  193.         glEnable(GL_BLEND);
  194.         glDisable(GL_TEXTURE_2D);
  195.      glBegin(GL_QUADS);
  196.         glVertex3f(-200, 100 ,CImgText::GetTextDist());
  197.         glVertex3f( 200, 100 ,CImgText::GetTextDist());
  198.         glVertex3f( 200, -100 ,CImgText::GetTextDist());
  199.         glVertex3f(-200, -100 ,CImgText::GetTextDist());
  200.      glEnd();
  201.         glDisable(GL_BLEND);
  202.     glColor3f(1,0,0);
  203.     CImgText::PrintString(400,260,"Mission Failed !",true,18,0);
  204.     glColor3f(0,1,0);
  205.     CImgText::PrintString(400,300,"Press space key to restart mission",true,16,0);
  206.     CImgText::PrintString(400, 330,"Press enter key to end mission",true,16,0);
  207.        ////////////////////////////
  208.      if(CInput::m_keys[VK_SPACE])
  209. {
  210.     CInput::m_keys[VK_SPACE]=false;
  211. RestartMission();     
  212. }
  213.      if(CInput::m_keys[13])
  214. {
  215.      CInput::m_keys[13]=false;
  216. RestoreFOVAngle();
  217.      CGameSetting::m_iGameState=GAME_MAIN_MENU;
  218. }
  219. }
  220. ////////////Mission completed
  221. if(m_cSprites->IsAllDeath())
  222. {
  223.      glColor3f(0.5f,0.0f,0.0f);
  224.      glBlendFunc(GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR);
  225.         glEnable(GL_BLEND);
  226.         glDisable(GL_TEXTURE_2D);
  227.      glBegin(GL_QUADS);
  228.         glVertex3f(-200, 100 ,CImgText::GetTextDist());
  229.         glVertex3f( 200, 100 ,CImgText::GetTextDist());
  230.         glVertex3f( 200, -100 ,CImgText::GetTextDist());
  231.         glVertex3f(-200, -100 ,CImgText::GetTextDist());
  232.      glEnd();
  233.         glDisable(GL_BLEND);
  234.     glColor3f(1,1,1);
  235.     CImgText::PrintString(400,260,"Mission Completed !",true,18,0);
  236.     glColor3f(0,1,0);
  237.     CImgText::PrintString(400, 300,"Press space key to restart mission",true,16,0);
  238.     CImgText::PrintString(400, 330,"Press enter key to end mission",true,16,0);
  239.        ////////////////////////////
  240.      if(CInput::m_keys[VK_SPACE])
  241. {
  242.     CInput::m_keys[VK_SPACE]=false;
  243. RestartMission();     
  244. }
  245.      if(CInput::m_keys[13])
  246. {
  247.      CInput::m_keys[13]=false;
  248. RestoreFOVAngle();
  249.      CGameSetting::m_iGameState=GAME_MAIN_MENU;
  250. }
  251. }
  252. }
  253. void CMission::RestartMission()
  254. {
  255.     m_cSprites->ResetAllSprite();
  256. m_3dExplorer->Reset3dExplorerPos();
  257. CMediaPlayer::Stop();
  258. CMediaPlayer::Play();
  259. }
  260. void  CMission::RestoreFOVAngle()
  261. {
  262. CImgText::SetViewFOVAngle(30);
  263. glViewport(0, 0, CGameSetting::m_iScrWidth, CGameSetting::m_iScrHeight);
  264. glMatrixMode(GL_PROJECTION);
  265. glLoadIdentity();
  266. gluPerspective(60,float(CGameSetting::m_iScrWidth)/CGameSetting::m_iScrHeight, 0.5f, 100000);
  267. glMatrixMode(GL_MODELVIEW);
  268. glLoadIdentity();
  269. }
  270. void  CMission::DeleteMission()
  271. {
  272. if(!m_bResource)return;
  273. //////// Texture
  274. CTexManager cTexManager;
  275. cTexManager.DeleteMissionResource();
  276. ////////////////////////
  277.     CAudioManager  cAudioManager;
  278. cAudioManager.DeleteMissionResource();
  279. ///////////////////////////////////
  280. if(m_3dExplorer!=NULL) 
  281. {
  282. delete m_3dExplorer;
  283. m_3dExplorer=NULL;
  284. }
  285. if(m_cTerrain !=NULL)  
  286. {
  287. delete m_cTerrain;
  288. m_cTerrain=NULL;
  289. }
  290. if(m_cSkyBox !=NULL) 
  291. {
  292. delete m_cSkyBox;
  293. m_cSkyBox=NULL;
  294. }
  295. if(m_cHmap !=NULL)   
  296. {
  297. delete m_cHmap;
  298. m_cHmap=NULL;
  299. }
  300. if(m_cInfoPanel !=NULL)   
  301. {
  302. delete m_cInfoPanel;
  303. m_cInfoPanel=NULL;
  304. }
  305. if(m_cLensFlare!=NULL) 
  306. {
  307. delete m_cLensFlare;
  308. m_cLensFlare=NULL;
  309. }
  310. if(m_cSprites!=NULL)  
  311.   {
  312. delete m_cSprites;
  313. m_cSprites=NULL;
  314. }
  315.     if(m_cBuilding!=NULL)  
  316. {
  317. delete m_cBuilding;
  318. m_cBuilding=NULL;
  319. }
  320. if(m_cPlant!=NULL)  
  321. {
  322. delete m_cPlant;
  323. m_cPlant=NULL;
  324. }
  325. if(m_pAmbientSound!=NULL)  
  326. {
  327. delete m_pAmbientSound;
  328. m_pAmbientSound=NULL;
  329. }
  330. if(m_pAmmoManager!=NULL)  
  331. {
  332. delete m_pAmmoManager;
  333. m_pAmmoManager=NULL;
  334. }
  335. /////////////////////////////////////
  336.     m_bActivate=false;
  337. m_bResource=false;
  338. }