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

游戏引擎

开发平台:

Visual C++

  1. // 3dE.cpp: implementation of the C3dE class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "3dE.h"
  6. #include "heightmap.h"
  7. #include "math.h"
  8. #include "audio.h"
  9. #include "imgtext.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. C3dE::C3dE()
  14. {
  15. m_bAttacked=false;
  16.     m_bAttacking=false;
  17. m_Height=8;
  18. m_baseHeight=255;
  19.     m_eyePos=VERTEX(0,0,0);
  20. m_RotX=0;
  21. m_RotY=180;
  22. m_RotZ=0;
  23. m_RunVector=NORMAL(0,0,0);
  24. m_JumpSpeed=0.4f;
  25. m_Acceleration=0.8f;
  26. m_MaxRunSpeed=1.5f;
  27.     m_Decrease=0.8f;
  28.     m_AirDecrease=0.0001f;
  29. m_Gravity=0.02f;
  30. m_bFlying=false;
  31. m_maxDifferent=2;
  32.     m_YBiasAngle=0;
  33. m_YBias=0;
  34.     m_ZBiasAngle=0;
  35. m_bBiasLeft=true;
  36. m_gunYBias=0;
  37. m_gunZBias=0;
  38. m_gunState=0;
  39. m_maxFOVAngle=60;
  40. m_minFOVAngle=15;
  41. m_curFOVAngle=60;
  42. m_orgFOVAngle=60;
  43. m_bZoomed=false;
  44. m_bZoomOut=false;
  45. m_iZoomAngle=1;
  46. m_mouseSpeed=0.1f;
  47. }
  48. C3dE::~C3dE()
  49. {
  50. }
  51. bool C3dE::Init3dExplorer()
  52. {
  53.     Reset3dExplorerPos();
  54. return true;
  55. }
  56. void C3dE::Reset3dExplorerPos()
  57. {
  58.     m_eyePos.xpos=m_cGS.m_fViewerPosX;
  59.     m_eyePos.zpos=m_cGS.m_fViewerPosZ;
  60.     m_eyePos.ypos=m_cHmap.GetHeight(m_eyePos.xpos,m_eyePos.zpos)+m_Height;
  61. m_baseHeight=m_eyePos.ypos-m_Height;
  62.     m_RotY=m_cGS.m_fViewerRotY;
  63. m_RotX=0;
  64. m_cHmap.m_myHealth=1;
  65. m_bAttacking=false;
  66. m_curFOVAngle=60;
  67. m_orgFOVAngle=60;
  68. m_bZoomed=false;
  69. m_bZoomOut=false;
  70. m_iZoomAngle=10;
  71.     ChangeFOVAngle();
  72.     UpdateHeightmap();
  73. }
  74. void C3dE::ProcessInput()
  75. {
  76. if(m_cHmap.m_myHealth<=0)
  77. {
  78. m_RotX=0;
  79. m_RotY+=0.2f;
  80.      UpdateHeightmap();
  81.         if(m_cHmap.m_myHealth!=-10)
  82. {
  83.             m_cHmap.m_myHealth=-10;
  84. m_iZoomAngle=60;
  85. ChangeFOVAngle();
  86. m_bAttacking=false;
  87.      int sound=SOUND_DEATH_1+rand()%3;
  88. CAudio::SetSoundPos(sound,&m_eyePos);
  89. CAudio::Play(sound,1,false,false);
  90. }
  91.         return;
  92. }
  93.     //////////////////////////////////////////////////////////
  94. /////////////// mouse rotate /////////////////////////////
  95.     m_RotY+=(400-m_cInput.m_mousePosX)*m_mouseSpeed;// /mouse Speed
  96.     m_RotX-=(300-m_cInput.m_mousePosY)*m_mouseSpeed;
  97. // Center mouse
  98. SetCursorPos(400,300);
  99. ///////////////////////////////////////////////////////////
  100. ///////////////  Run //////////////////////////////////////
  101. // Keyboard input
  102. if (m_cInput.m_keys[m_cGS.m_iKeyForward] && (!m_bFlying))
  103. {
  104.         m_RunVector.nz-=m_Acceleration;
  105. if(m_RunVector.nz<-m_MaxRunSpeed)m_RunVector.nz=-m_MaxRunSpeed;
  106. ////////////////Y bias
  107.      if (m_YBiasAngle >= 359)
  108.      m_YBiasAngle = 0;
  109.      else
  110.       m_YBiasAngle+= 8;
  111. m_YBias=sinf(m_YBiasAngle*0.0174533f )/4.0f;
  112. }
  113. else
  114. {
  115. if(! m_cInput.m_keys[m_cGS.m_iKeyBackward] && m_RunVector.nz<0 )
  116. {
  117. if(m_bFlying) m_RunVector.nz+=m_AirDecrease;
  118. else  m_RunVector.nz+=m_Decrease;
  119.     if(m_RunVector.nz>0)m_RunVector.nz=0;
  120.     
  121. }
  122. }
  123. if (m_cInput.m_keys[m_cGS.m_iKeyBackward] && (!m_bFlying))
  124. {
  125.         m_RunVector.nz+=m_Acceleration;
  126. if(m_RunVector.nz>m_MaxRunSpeed)m_RunVector.nz=m_MaxRunSpeed;
  127. }
  128. else
  129. {
  130.     if (! m_cInput.m_keys[m_cGS.m_iKeyForward] && m_RunVector.nz>0)
  131. {
  132. if(m_bFlying) m_RunVector.nz-=m_AirDecrease;
  133. else  m_RunVector.nz-=m_Decrease;
  134.     if(m_RunVector.nz<0)m_RunVector.nz=0;
  135. }
  136. }
  137. if (m_cInput.m_keys[m_cGS.m_iKeyLeft]&& (!m_bFlying))
  138. {
  139.         m_RunVector.nx-=m_Acceleration;
  140. if(m_RunVector.nx<-m_MaxRunSpeed)m_RunVector.nx=-m_MaxRunSpeed;
  141. }
  142. else
  143. {
  144.      if (!m_cInput.m_keys[m_cGS.m_iKeyRight] && m_RunVector.nx<0)
  145. {
  146.             m_RunVector.nx+=m_Decrease;
  147.      if(m_RunVector.nx>0)m_RunVector.nx=0;
  148. }
  149. }
  150. if (m_cInput.m_keys[m_cGS.m_iKeyRight]&& (!m_bFlying) )
  151. {
  152.         m_RunVector.nx+=m_Acceleration;
  153. if(m_RunVector.nx>m_MaxRunSpeed)m_RunVector.nx=m_MaxRunSpeed;
  154. }
  155. else
  156. {
  157.     if (! m_cInput.m_keys[m_cGS.m_iKeyLeft] && m_RunVector.nx>0)
  158. {
  159.             m_RunVector.nx-=m_Decrease;
  160.      if(m_RunVector.nx<0)m_RunVector.nx=0;
  161. }
  162. }
  163.     /////////////////////////////////////////////////////////////////
  164. //////////////////// Jump  //////////////////////////////////////
  165. if (m_cInput.m_keys[m_cGS.m_iKeyJump] && (!m_bFlying))
  166. {
  167.         m_RunVector.ny=m_JumpSpeed;
  168.         m_bFlying=true;
  169. }
  170. /////////////////////////////////////////////////////////////////
  171. //////////////////// Fire ///////////////////////////////////////
  172. static int  mouseStick=0;
  173. static bool bContinue=false;
  174.     if(m_cInput.m_keys[m_cGS.m_iKeyFire] )
  175. {
  176. m_bAttacking=true;
  177. m_gunState=1;
  178.         m_gunZBias+=60;
  179.         mouseStick++;
  180. }
  181. else 
  182. {
  183. m_bAttacking=false;
  184. m_gunYBias+=4;
  185. if(mouseStick>0 && mouseStick<5)
  186. {
  187. CAudio::Play(SOUND_GUN_SINGLE,1,false);
  188. }
  189. mouseStick=0;
  190. bContinue=false;
  191. }
  192. if(mouseStick>=5)
  193. bContinue=true;
  194.     if(bContinue)
  195.     CAudio::Play(SOUND_GUN_MULTI,1,false);
  196. /////////////////////////////////////////////////////////////////
  197. ///////////////////// Zoom //////////////////////////////////////
  198. if(m_cInput.m_keys[m_cGS.m_iKeyZoom])
  199. {
  200. m_cInput.m_keys[m_cGS.m_iKeyZoom]=false;
  201.         if(m_curFOVAngle==m_minFOVAngle)
  202. {
  203. m_iZoomAngle=10;
  204. m_orgFOVAngle=m_maxFOVAngle;
  205. }
  206.         if(m_curFOVAngle==m_maxFOVAngle)
  207. {
  208. m_iZoomAngle=-15;
  209. m_orgFOVAngle=m_minFOVAngle;
  210. }
  211. }
  212. /////////////////////////////////////////////////////////////////
  213. ////////////////////
  214. if (m_cInput.m_keys[VK_NEXT])
  215. {
  216. m_Height+=1;
  217. }
  218. if (m_cInput.m_keys[VK_PRIOR])
  219. {
  220. m_Height-=1;
  221. }
  222.     //////////// backup my position
  223. VERTEX oldPos=m_eyePos;
  224. ///////////////////////////////////////////
  225. /////// Now , Update eyePos
  226. ///////////////////////////////////////////
  227.     m_eyePos.xpos +=m_RunVector.nz*sinf(m_RotY*0.0174533f)+m_RunVector.nx*cosf(m_RotY*0.0174533f);
  228. m_eyePos.ypos +=m_RunVector.ny + m_YBias;
  229. m_eyePos.zpos +=m_RunVector.nz*cosf(m_RotY*0.0174533f)-m_RunVector.nx*sinf(m_RotY*0.0174533f);
  230.     //////// check new position
  231. m_baseHeight=m_eyePos.ypos-m_Height;
  232.     float  newHeight=m_cHmap.GetHeight(&m_eyePos);
  233. if(m_bFlying)
  234. {
  235.         m_RunVector.ny-=m_Gravity;
  236. if(m_baseHeight<newHeight)
  237. {
  238.             m_baseHeight=newHeight;
  239. m_eyePos.ypos=newHeight+m_Height;
  240. m_RunVector.ny=0;
  241.             m_bFlying=false;
  242. }
  243. return;
  244. }
  245. m_baseHeight=newHeight;
  246.     m_eyePos.ypos=m_baseHeight+m_Height;
  247. ///////////////////////////
  248. static int index=0;
  249. static int step=0;
  250.     
  251. if(! m_cHmap.CollideBuilding(&oldPos,&m_eyePos))
  252.     m_cHmap.CollideTreeBody(&m_eyePos) ;
  253. if(m_eyePos.xpos !=oldPos.xpos  || m_eyePos.zpos !=oldPos.zpos) 
  254. {
  255. if(index==0)CAudio::Play(SOUND_RUN_0+step,1,true);
  256. if(m_bZoomed )m_iZoomAngle=15;
  257. }
  258. else
  259. {
  260. // if(m_orgFOVAngle==m_minFOVAngle && !m_bZoomed)m_iZoomAngle=-10; //need ?//????????????
  261. }
  262. index++;
  263. if(index>20)
  264. {
  265. index=0; 
  266. step++;
  267. if(step==4)step=0;
  268. }
  269.     //////////////////////////////
  270. CAudio::SetListenerPos(&m_eyePos,m_RotY);
  271. ChangeFOVAngle();
  272. UpdateHeightmap();
  273. }
  274. void C3dE::TransformWorld(int step)
  275. {
  276. //////////////////////////////////////////
  277. if(step==0)
  278. {
  279. glRotatef(float(m_RotX), 1.0f, 0.0f, 0.0f);  
  280.         glRotatef(float(m_ZBiasAngle), 0.0f, 0.0f, 1.0f);
  281.         glRotatef(360.0f - m_RotY, 0.0f, 1.0f, 0.0f);
  282. glTranslated(0,  0, 0);
  283. }
  284. else
  285. {
  286.       glTranslated(-m_eyePos.xpos, -m_eyePos.ypos, -m_eyePos.zpos);
  287. }
  288. }
  289. void C3dE::UpdateHeightmap()
  290. {
  291. m_cHmap.m_ViewPos.xpos=m_eyePos.xpos;
  292. m_cHmap.m_ViewPos.ypos=m_eyePos.ypos;
  293. m_cHmap.m_ViewPos.zpos=m_eyePos.zpos;
  294. if(m_RotX>=360)m_RotX-=360;
  295. if(m_RotY>=360)m_RotY-=360;
  296. if(m_RotX<0)m_RotX+=360;
  297. if(m_RotY<0)m_RotY+=360;
  298. m_cHmap.m_ViewRotX    =m_RotX;
  299. m_cHmap.m_ViewRotY    =m_RotY;
  300. m_cHmap.m_bAttack=m_bAttacking;
  301.     m_cHmap.UpdateHeightmap();
  302. }
  303. void C3dE::ChangeFOVAngle()
  304. {
  305. if(m_iZoomAngle==0)return;
  306.     m_curFOVAngle+=m_iZoomAngle;
  307. if(m_curFOVAngle>m_maxFOVAngle)
  308. {
  309. m_curFOVAngle=m_maxFOVAngle;
  310. m_iZoomAngle=0;
  311. m_mouseSpeed=0.1f;
  312. m_bZoomed=false;
  313. }
  314. if(m_curFOVAngle<m_minFOVAngle)
  315. {
  316. m_curFOVAngle=m_minFOVAngle;
  317. m_iZoomAngle=0;
  318. m_mouseSpeed=0.02f;
  319. m_bZoomed=true;
  320. }
  321. glViewport(0, 0, CGameSetting::m_iScrWidth, CGameSetting::m_iScrHeight);
  322. glMatrixMode(GL_PROJECTION);
  323. glLoadIdentity();
  324. gluPerspective(m_curFOVAngle,float(CGameSetting::m_iScrWidth)/CGameSetting::m_iScrHeight, 0.5f, 100000);
  325. glMatrixMode(GL_MODELVIEW);
  326. glLoadIdentity();
  327. CImgText::SetViewFOVAngle(m_curFOVAngle/2);
  328. }