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

游戏引擎

开发平台:

Visual C++

  1. // Sprite.cpp: implementation of the CSprite class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Sprite.h"
  6. #include "input.h"
  7. #include "audio.h"
  8. #include "scrmasker.h"
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12. CSprite::CSprite()
  13. {
  14. m_pSoldier=NULL;
  15. m_pWeapon=NULL;
  16. m_Health=1;
  17.     m_bFindEnemy=false;
  18. m_bDangerous=false;
  19. m_bWounded=false;
  20. m_bHearShot=false;
  21. m_bFire=false;
  22. m_iAlarm=0;
  23. /////////////model control
  24. startFrame=0;
  25. endFrame=39;
  26. curFrame=0;
  27. nextFrame=1;
  28.     percent=0;
  29.     interpolation=0.25f;
  30. bActionFinish=false;
  31. bDrawWeapon=true;
  32. curAction=SMF_STAND;
  33. m_bFire=false;
  34. }
  35. CSprite::~CSprite()
  36. {
  37. }
  38. bool CSprite::InitSprite(int id,VERTEX startPos,float rotY,CSMFLoader *pSoldier,CSMFLoader *pWeapon)
  39. {
  40. m_nID=id;
  41. m_pSoldier=pSoldier;
  42. m_pWeapon=pWeapon;
  43.     
  44.     startPos.ypos=m_cHmap.GetHeight(startPos.xpos,startPos.zpos);
  45. startPos.ypos+=(m_pSoldier->m_boundary.maxy-m_pSoldier->m_boundary.miny)/2;
  46. m_Camera.SetCamera(startPos,rotY);
  47. m_boundary.minx = m_pSoldier->m_boundary.minx + startPos.xpos;
  48. m_boundary.maxx = m_pSoldier->m_boundary.maxx + startPos.xpos;
  49. m_boundary.miny = m_pSoldier->m_boundary.miny + startPos.ypos;
  50. m_boundary.maxy = m_pSoldier->m_boundary.maxy + startPos.ypos;
  51. m_boundary.minz = m_pSoldier->m_boundary.minz + startPos.zpos;
  52. m_boundary.maxz = m_pSoldier->m_boundary.maxz + startPos.zpos;
  53. m_orgPos=startPos;
  54. SetSpriteAction(SMF_STAND);
  55. m_cGunFire.InitGunFire(3.5f,4.0f);
  56. return true;
  57. }
  58. void CSprite::RenderSprite(bool noHarm)
  59. {
  60. if(!m_cHmap.IsInFrustum(&m_orgPos))return;
  61. // if(curAction>=SMF_DEATH1 && bActionFinish)return;
  62.     glPushMatrix();
  63.     glTranslatef(m_orgPos.xpos,m_orgPos.ypos,m_orgPos.zpos);
  64.     glRotatef(m_Camera.m_CamRotY,  0.0f,1.0f,0.0f);
  65. m_pSoldier->Animate(curFrame,nextFrame,percent);
  66. if(bDrawWeapon)m_pWeapon->Animate(curFrame,nextFrame,percent);
  67.     glPopMatrix();
  68. DrawGunFire(noHarm);
  69. CHeightmap::m_numTriangles += 990 ;
  70. /*
  71. glColor3f(1,1,1);
  72. glBegin(GL_LINES);
  73.     glVertex3f(m_Camera.m_CamPos.xpos,m_Camera.m_CamPos.ypos,m_Camera.m_CamPos.zpos);
  74. glVertex3f(m_cHmap.m_ViewPos.xpos,m_cHmap.m_ViewPos.ypos-8,m_cHmap.m_ViewPos.zpos);
  75.     glEnd();
  76. */
  77.  //   DrawSMFBoundary();
  78. }
  79. void CSprite::UpdateSprite(bool freeze,bool noVisible)
  80. {
  81. if(curAction>=SMF_DEATH1 && bActionFinish)return;
  82. if(m_cHmap.m_myHealth<=0)return;
  83.     if(!noVisible)UpdateSpriteState();
  84. else
  85. {
  86. if(curAction!=SMF_STAND)SetSpriteAction(SMF_STAND);
  87. else
  88. {
  89.       if(bActionFinish)
  90.                 SetSpriteAction(SMF_STAND);
  91. }
  92. }
  93. if(!freeze)UpdateSpriteAction();
  94. }
  95. void CSprite::ResetSpriteState()
  96. {
  97. m_Health=1;
  98. bDrawWeapon=true;
  99.     m_bFindEnemy=false;
  100. m_bDangerous=false;
  101. m_bWounded=false;
  102. m_bHearShot=false;
  103. m_bFire=false;
  104. m_iAlarm=0;
  105. m_Camera.SetCameraRotate(float(rand()%360));
  106. SetSpriteAction(SMF_STAND);
  107. }
  108. void CSprite::Injure()
  109. {
  110. float harm=0.2f;
  111. float bias=float(m_cHmap.m_focusPosY)-m_Camera.m_CamPos.ypos;
  112. if(bias<0)bias=-bias;
  113. if(bias<2)harm=(2-bias);
  114. if(harm<0.2f)harm=0.2f;
  115.     if(m_cHmap.m_focusPosY>m_orgPos.ypos)harm=1;
  116.     m_Health-=harm;
  117. m_bWounded=true;  
  118. }
  119. void CSprite::Scan()
  120. {
  121.     VERTEX pos=m_pSoldier->GetPos(SMF_EYE_POS,percent);
  122. m_Camera.m_CamPos.xpos =m_orgPos.xpos + pos.xpos;
  123. m_Camera.m_CamPos.ypos =m_orgPos.ypos + pos.ypos;
  124.     m_Camera.m_CamPos.zpos =m_orgPos.zpos + pos.zpos;
  125. if(m_cHmap.m_bAttack)
  126. {
  127. m_bHearShot=true;
  128.         m_iAlarm=1;
  129. if(m_Camera.GetDistance(VERTEX(float(m_cHmap.m_focusPosX),
  130.                     float(m_cHmap.m_focusPosY),
  131. float(m_cHmap.m_focusPosZ))) < 30 )
  132. m_bDangerous=true;
  133. else 
  134. m_bDangerous=false;
  135. }
  136. else
  137. {
  138.      m_bDangerous=false;
  139. m_bHearShot=false;
  140. }
  141. m_distFromViewer=m_cMath.GetDistance(m_Camera.m_CamPos,m_cHmap.m_ViewPos);
  142. if(m_distFromViewer<25)
  143. {
  144. m_bFindEnemy=true;
  145. m_iAlarm=1;
  146. return;
  147. }
  148. if(m_Camera.IsEnemyInFrustum(m_cHmap.m_ViewPos))
  149. {
  150.         if(!m_cHmap.CollideCheck(m_Camera.m_CamPos,m_cHmap.m_ViewPos,2))
  151. {
  152.             if(m_iAlarm>0)
  153.             m_bFindEnemy=true;
  154. else
  155. {
  156.               int find=rand()%600;
  157. int findout;
  158.                 if(m_distFromViewer>800)findout=1;
  159.              else if(m_distFromViewer>600)findout=2;
  160.               else if(m_distFromViewer>400)findout=3;
  161. else if(m_distFromViewer>200)findout=50;
  162.              else findout=600;
  163. if(find<findout)m_bFindEnemy=true;
  164. else m_bFindEnemy=false;
  165. }
  166. }
  167. else 
  168. m_bFindEnemy=false;
  169. }
  170. if(m_bFindEnemy)m_iAlarm=1;
  171. }
  172. void CSprite::UpdateSpriteState()
  173. {
  174. if(m_Health<=0)
  175. {
  176. if(curAction<SMF_DEATH1)
  177. {
  178. SetSpriteAction(SMF_DEATH1+rand()%2);
  179. int sound=SOUND_DEATH_1+rand()%3;
  180. CAudio::SetSoundPos(sound,&m_orgPos);
  181. CAudio::Play(sound,1,false,false);
  182. }
  183. m_bFire=false;
  184. m_Health--;
  185.   return;
  186. }
  187. /////////////////////////
  188.     if(m_bWounded)
  189. {
  190. m_bWounded=false;
  191.         if(curAction!=SMF_PAIN  && curAction!=SMF_CRPAIN)
  192. {
  193. int sound=SOUND_PAIN_1+rand()%3;
  194. CAudio::SetSoundPos(sound,&m_orgPos);
  195. CAudio::Play(sound,1,false,false);
  196. }
  197.         if(curAction<SMF_CRSTAND)SetSpriteAction(SMF_PAIN);
  198. else SetSpriteAction(SMF_CRPAIN);
  199. return;
  200. }
  201. if(curAction==SMF_PAIN ||curAction==SMF_CRPAIN)
  202. {
  203. if(bActionFinish)
  204. {
  205. m_Camera.SetCamera(m_cHmap.m_ViewPos);
  206.     SetSpriteAction(SMF_CRSTAND);
  207. }
  208. return;
  209. }
  210. ////////////////
  211. Scan();
  212. ////////////////////////
  213. if(curAction==SMF_ATTACK )
  214. {
  215. if(m_bDangerous)
  216. {
  217. m_Camera.SetCamera(m_cHmap.m_ViewPos);
  218.             SetSpriteAction(SMF_CRATTACK);
  219. return;
  220. }
  221.         if(bActionFinish)
  222. if(m_bFindEnemy)
  223. {
  224.      m_Camera.SetCamera(m_cHmap.m_ViewPos);
  225.                 SetSpriteAction(SMF_ATTACK);
  226. return;
  227. }
  228. else
  229. {
  230.                 SetSpriteAction(SMF_STAND);
  231. return;
  232. }
  233. }
  234. else 
  235. return;
  236. }
  237. if(curAction==SMF_CRATTACK )
  238. {
  239.         if(bActionFinish)
  240. if(m_bFindEnemy)
  241. {
  242.      m_Camera.SetCamera(m_cHmap.m_ViewPos);
  243.                 SetSpriteAction(SMF_CRATTACK);
  244. return;
  245. }
  246. else
  247. {
  248.                 SetSpriteAction(SMF_STAND);
  249. return;
  250. }
  251. }
  252. else 
  253. return;
  254. }
  255. if(curAction==SMF_CRSTAND)
  256. {
  257. if(m_bFindEnemy)
  258. {
  259.     m_Camera.SetCamera(m_cHmap.m_ViewPos);
  260.             SetSpriteAction(SMF_CRATTACK);
  261. return;
  262. }    
  263. if(m_bHearShot)
  264. {
  265.     m_Camera.SetCamera(m_cHmap.m_ViewPos);
  266. }
  267. if(bActionFinish)
  268.             SetSpriteAction(SMF_STAND);
  269. return;
  270. }
  271. return;
  272. }
  273. if(curAction==SMF_STAND)
  274. {
  275. if(m_bDangerous)
  276. {
  277. m_Camera.SetCamera(m_cHmap.m_ViewPos);
  278.             SetSpriteAction(SMF_CRSTAND);
  279. return;
  280. }
  281. if(m_bFindEnemy)
  282. {
  283.     m_Camera.SetCamera(m_cHmap.m_ViewPos);
  284.             SetSpriteAction(SMF_ATTACK);
  285. return;
  286. }      
  287. if(m_bHearShot)
  288. {
  289.     m_Camera.SetCamera(m_cHmap.m_ViewPos);
  290. }
  291. if(bActionFinish)
  292.     if(m_iAlarm>0)m_iAlarm--;
  293. else
  294. {
  295.         float angle=float(rand()%200-80);
  296.         m_Camera.SetCameraRotate(angle);
  297. }
  298.             SetSpriteAction(SMF_STAND);
  299. return;
  300. }
  301. }
  302. }
  303. void CSprite::UpdateSpriteAction()
  304. {
  305. /////////////////////////////update action
  306. percent+=interpolation;
  307. if(percent>1)
  308. {
  309. percent-=1;
  310.         curFrame++;
  311.         nextFrame++;
  312. if(nextFrame>endFrame)
  313. {
  314. nextFrame=endFrame;
  315. curFrame =endFrame;
  316.     bActionFinish=true;
  317. }
  318. }
  319. ///////////////////////
  320. }
  321. /////////////////////////////////////////
  322. void CSprite::SetSpriteAction(int iAction)
  323. {
  324. ////// check
  325. if(iAction>(m_pSoldier->numAction-1) || iAction<0)iAction=0;
  326. if(iAction>(m_pWeapon->numAction-1))bDrawWeapon=false;
  327. else bDrawWeapon=true;
  328. curAction=iAction;
  329. ////////////
  330. startFrame=m_pSoldier->pAction[iAction].startFrame;
  331. endFrame  =m_pSoldier->pAction[iAction].endFrame;
  332.     
  333. bActionFinish=false;
  334.     curFrame=startFrame;
  335. nextFrame=curFrame+1;
  336. percent=0;
  337. if(curAction==SMF_ATTACK || curAction==SMF_CRATTACK )m_bFire=true;
  338. else m_bFire=false;
  339. }
  340. bool CSprite::IsInSpriteBoundary(VERTEX pos)
  341. {
  342. if(pos.xpos>m_boundary.maxx || pos.xpos<m_boundary.minx)return false; 
  343. if(pos.ypos>m_boundary.maxy || pos.ypos<m_boundary.miny)return false; 
  344. if(pos.zpos>m_boundary.maxz || pos.zpos<m_boundary.minz)return false; 
  345.     return true;
  346. }
  347. void CSprite::DrawGunFire(bool noHarm)
  348. {
  349.     if(!m_bFire)return;
  350. m_bFire=false;
  351. CAudio::SetSoundPos(SOUND_GUN_RIFLE,&m_orgPos);
  352. CAudio::Play(SOUND_GUN_RIFLE,1,false,false);
  353. VERTEX pos=m_pWeapon->GetPos(SMF_GUN_MOUTH,percent);
  354. float rotate=m_Camera.m_CamRotY*0.0174533f;
  355.     float xpos= pos.xpos*cosf(rotate)+pos.zpos*sinf(rotate);
  356. float zpos=-pos.xpos*sinf(rotate)+pos.zpos*cosf(rotate);
  357.     glPushMatrix();   
  358.     glTranslatef(m_orgPos.xpos + xpos,
  359.          m_orgPos.ypos + pos.ypos,
  360.  m_orgPos.zpos + zpos);
  361.     glRotatef(m_cHmap.m_ViewRotY,  0.0f,1.0f,0.0f);
  362.     glRotatef(-m_cHmap.m_ViewRotX,  1.0f,0.0f,0.0f);
  363.   ///////////////////////////////////////////////////////
  364.     m_cGunFire.DrawGunFire();
  365. //////////////end draw
  366. ///////////////////////////////////////////////////////
  367.     glPopMatrix();
  368. int hit=rand()%14;
  369. float dist=m_cMath.GetDistance(m_Camera.m_CamPos,m_cHmap.m_ViewPos);
  370.     int hitDist;
  371.     if(dist>600)hitDist=3;
  372. else if(dist>300)hitDist=4;
  373. else if(dist>100)hitDist=6;
  374. else if(dist>50)hitDist=8;
  375. else hitDist=10;
  376. if(hit<hitDist)
  377. {
  378. if(noHarm)return;
  379. int sound=SOUND_PAIN_1+rand()%3;
  380.      CAudio::SetSoundPos(sound,&m_cHmap.m_ViewPos);
  381.      CAudio::Play(sound,1,false,false);
  382. CScrMasker::SprayBlood();
  383. m_cHmap.m_myHealth-=0.15f+float(rand()%10)*0.01f;
  384. }
  385. else
  386. {
  387. if(hit>8)
  388.          CAudio::Play(SOUND_BULLET_WHIZ,1,false,false);
  389. }
  390. }
  391. void CSprite::DrawSMFBoundary()
  392. {
  393. glDisable(GL_TEXTURE_2D);
  394. glColor3f(1,1,1);
  395. glBegin(GL_LINE_STRIP);
  396.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
  397.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
  398.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
  399.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
  400.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
  401.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
  402.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
  403.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
  404.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
  405.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
  406. glEnd();
  407. glBegin(GL_LINES);
  408.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
  409.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
  410.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
  411.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
  412.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
  413.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
  414. glEnd();
  415. glColor3f(1,1,1);
  416. }