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

游戏引擎

开发平台:

Visual C++

  1. // Sprite.cpp: implementation of the CSprite class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Sprite.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CSprite::CSprite()
  10. {
  11. m_RunSpeed=0.1f;
  12. m_bHasWeapon=false;
  13. m_startFrame=0;
  14. m_endFrame=1;
  15. m_percent=0;
  16. m_Health=1.5f;
  17. }
  18. CSprite::~CSprite()
  19. {
  20. }
  21. bool CSprite::InitSprite(int id,VERTEX startPos,float rotY,int totalFrames,
  22.  float height,BOUNDARY_3D boundary,bool bControlled)
  23. {
  24. m_nID=id;
  25. m_nFrames=totalFrames;
  26. m_height=height;
  27. m_bControlled =bControlled;
  28. m_Boundary=boundary;
  29. /*
  30. if(!m_cAmmoManger.InitAmmoManager())
  31. {
  32. MessageBox(0, "cMD2 error", "Error", MB_OK | MB_ICONERROR);
  33. return FALSE;
  34. }*/
  35. m_Camera.UpdateCamera(startPos,0,rotY,0);
  36. m_nState=1-rand()%2;
  37. SetSpriteState(m_nState);
  38. return true;
  39. }
  40. void CSprite::ResetSpriteState(VERTEX resetPos,float rotY,int state)
  41. {
  42. m_nState=state;
  43. m_Camera.UpdateCamera(resetPos,0,rotY,0);
  44. }
  45. void CSprite::DrawSprite()
  46. {
  47. }
  48. void CSprite::UpdateSprite()
  49. {
  50. if(m_nState==0)m_percent+=0.125f;
  51. else m_percent+=0.25f;
  52. if(m_percent>1)
  53. {
  54. m_percent=0;
  55. m_startFrame++;
  56. m_endFrame++;
  57. if(m_startFrame>m_stateEnd)
  58. {
  59.      m_startFrame = m_stateStart;
  60. }
  61.      if(m_endFrame>m_stateEnd)
  62. {
  63.      m_endFrame = m_stateStart;
  64. }    
  65. }
  66. ///////////////////////////////
  67. if(m_nState==RUN && m_Camera.m_CamPos.xpos<2100 && m_Camera.m_CamPos.zpos<3000 )
  68. {
  69.     m_Camera.m_CamPos.xpos+=m_Camera.m_CamDirection.nx*0.4f;
  70.     m_Camera.m_CamPos.zpos+=m_Camera.m_CamDirection.nz*0.4f;
  71.     m_Camera.m_CamPos.ypos=m_cHmap.GetHeight(&m_Camera.m_CamPos)+m_height*0.5f;
  72. }
  73.     CheckSunVisible();
  74. }
  75. /////////////////////////////////////////
  76. void CSprite::SetSpriteState(int state)
  77. {
  78. switch(state)
  79. {
  80. case IDLE1:
  81. m_stateStart  = IDLE1_START;
  82. m_stateEnd  = IDLE1_END;
  83.             CheckState();
  84. break;
  85. case RUN:
  86. m_stateStart  = RUN_START;
  87. m_stateEnd  = RUN_END;
  88.             CheckState();
  89. break;
  90. case SHOT_STAND:
  91. m_stateStart  = SHOT_STAND_START;
  92. m_stateEnd  = SHOT_STAND_END;
  93.             CheckState();
  94. break;
  95. case SHOT_SHOULDER:
  96. m_stateStart  = SHOT_SHOULDER_START;
  97. m_stateEnd  = SHOT_SHOULDER_END;
  98.             CheckState();
  99. break;
  100. case JUMP:
  101. m_stateStart  = JUMP_START;
  102. m_stateEnd  = JUMP_END;
  103.             CheckState();
  104. break;
  105. case IDLE2:
  106. m_stateStart  = IDLE2_START;
  107. m_stateEnd  = IDLE2_END;
  108.             CheckState();
  109. break;
  110. case SHOT_FALLDOWN:
  111. m_stateStart  = SHOT_FALLDOWN_START;
  112. m_stateEnd  = SHOT_FALLDOWN_END;
  113.             CheckState();
  114. break;
  115. case IDLE3:
  116. m_stateStart  = IDLE3_START;
  117. m_stateEnd  = IDLE3_END;
  118.             CheckState();
  119. break;
  120. case IDLE4:
  121. m_stateStart  = IDLE4_START;
  122. m_stateEnd  = IDLE4_END;
  123.             CheckState();
  124. break;
  125. case CROUCH:
  126. m_stateStart  = CROUCH_START;
  127. m_stateEnd  = CROUCH_END;
  128.             CheckState();
  129. break;
  130. case CROUCH_CRAWL:
  131. m_stateStart  = CROUCH_CRAWL_START;
  132. m_stateEnd  =     CROUCH_CRAWL_END;
  133.             CheckState();
  134. break;
  135. case CROUCH_IDLE:
  136. m_stateStart  = CROUCH_IDLE_START;
  137. m_stateEnd  = CROUCH_IDLE_END;
  138.             CheckState();
  139. break;
  140. case CROUCH_DEATH:
  141. m_stateStart  = CROUCH_DEATH_START;
  142. m_stateEnd  = CROUCH_DEATH_END;
  143.             CheckState();
  144. break;
  145. case DEATH_FALLBACK:
  146. m_stateStart  = DEATH_FALLBACK_START;
  147. m_stateEnd  = DEATH_FALLBACK_END;
  148.             CheckState();
  149. break;
  150. case DEATH_FALLFORWARD:
  151. m_stateStart  = DEATH_FALLFORWARD_START;
  152. m_stateEnd  = DEATH_FALLFORWARD_END;
  153.             CheckState();
  154. break;
  155. case DEATH_FALLBACKSLOW:
  156. m_stateStart  = DEATH_FALLBACKSLOW_START;
  157. m_stateEnd  = DEATH_FALLBACKSLOW_END;
  158.             CheckState();
  159. break;
  160. }
  161. }
  162. void CSprite::CheckState()
  163. {
  164. m_startFrame    = m_stateStart+rand()%5;
  165. m_endFrame      = m_startFrame+1 ;
  166. if(m_stateEnd>(m_nFrames-1))
  167. {
  168. m_stateStart=IDLE1_START;
  169. m_stateEnd  =IDLE1_END;
  170.                 m_startFrame=0;
  171. m_endFrame  =1;
  172. }
  173. }
  174. void CSprite::CheckSunVisible()
  175. {
  176. if(!m_cHmap.m_bSunVisible)return;
  177. BOUNDARY_3D boundary;
  178. VERTEX      position=m_Camera.m_CamPos;
  179. boundary.minx = m_Boundary.minx+position.xpos;
  180. boundary.maxx = m_Boundary.maxx+position.xpos;
  181. boundary.miny = m_Boundary.miny+position.ypos + m_height*0.5f;
  182. boundary.maxy = m_Boundary.maxy+position.ypos + m_height*0.5f;
  183. boundary.minz = m_Boundary.minz+position.zpos;
  184. boundary.maxz = m_Boundary.maxz+position.zpos;
  185.     if(m_cMath.IsLineSegmentCutBy3DBoundary(*m_cHmap.m_pViewPos ,
  186.                                      *m_cHmap.m_pSunPos,
  187.     boundary))
  188.         m_cHmap.m_bSunVisible=false;
  189. }