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

游戏引擎

开发平台:

Visual C++

  1. // Bullet.cpp: implementation of the CBullet class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Bullet.h"
  6. #include "texture.h"
  7. #include "math.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CBullet::CBullet()
  12. {
  13. m_bulletType=M16;
  14. }
  15. CBullet::~CBullet()
  16. {
  17. }
  18. bool CBullet::InitBullet(VERTEX  startPos,float rotx,float roty,NORMAL direction,unsigned int *texResource,int type)
  19. {
  20. m_pTexResource=texResource;
  21. m_bulletType=type;
  22.     m_CurPos=startPos;
  23. m_oldPos=startPos;
  24. m_rotX=rotx;
  25. m_rotY=roty;
  26. m_rotZ=0;
  27.     m_direction = direction;
  28.     m_life=350;
  29.     switch(m_bulletType)
  30. {
  31. case M16:
  32.      m_Speed=3.0f;
  33. break;
  34. case RIFLE:
  35.      m_Speed=3.0f;
  36. break;
  37. case ROCKET:
  38. // m_rocket.InitParticle(startPos,m_pTexResource,BULLET_P,direction);
  39.      m_Speed=2.0f;
  40. break;
  41. }
  42. return true;
  43. }
  44. bool CBullet::InitBullet(VERTEX  startPos,float rotx,float roty,unsigned int *texResource,int type)
  45. {
  46. m_pTexResource=texResource;
  47.     NORMAL direction;
  48.     direction.nx=-cosf(rotx*0.0174533f)*sinf(roty*0.0174533f);
  49.     direction.ny=-sinf(rotx*0.0174533f);
  50.     direction.nz=-cosf(rotx*0.0174533f)*cosf(roty*0.0174533f);
  51.     InitBullet(startPos,rotx,roty,direction,m_pTexResource,type);
  52. return true;
  53. }
  54. void CBullet::UpdateBullet()
  55. {
  56. m_oldPos=m_CurPos;
  57. m_CurPos.xpos+=m_direction.nx*m_Speed;
  58. m_CurPos.ypos+=m_direction.ny*m_Speed;
  59. m_CurPos.zpos+=m_direction.nz*m_Speed;
  60.     if(m_cHmap.CollideCheck(m_oldPos,m_CurPos,2))
  61. {
  62. m_life=-1;
  63. }
  64.     else 
  65. {
  66. if(m_bulletType==M16)DrawM16Bullet();
  67. if(m_bulletType==RIFLE)DrawRifleBullet();
  68. if(m_bulletType==ROCKET)DrawRocketBullet();
  69. m_life--;
  70. }
  71. }
  72. void CBullet::DrawRifleBullet()
  73. {
  74. glEnable(GL_TEXTURE_2D);
  75. glDisable(GL_CULL_FACE);
  76.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[0] );   
  77. glBlendFunc(GL_ONE,GL_ONE);
  78. glEnable(GL_BLEND);
  79. float size=0.1f;
  80.     glPushMatrix();
  81. glTranslatef(m_CurPos.xpos,m_CurPos.ypos,m_CurPos.zpos);
  82. glRotatef(m_rotY,  0.0f,1.0f,0.0f);
  83. glRotatef(-m_rotX,  1.0f,0.0f,0.0f);
  84. glBegin(GL_TRIANGLE_STRIP);
  85.     glTexCoord2f(0,0);
  86.     glVertex3f(-size,-size,0);
  87.     glTexCoord2f(1,0);
  88.     glVertex3f(size,-size,0);     
  89.     glTexCoord2f(0,1);
  90.         glVertex3f(-size,size,0);
  91.     glTexCoord2f(1,1);
  92.     glVertex3f(size,size,0);
  93. glEnd();
  94.     glPopMatrix();
  95. glDisable(GL_BLEND);
  96. glEnable(GL_CULL_FACE);
  97. }
  98. void CBullet::DrawM16Bullet()
  99. {
  100. glDisable(GL_CULL_FACE);
  101. glEnable(GL_TEXTURE_2D);
  102.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[0]  );    
  103. float size=0.05f;
  104.     glPushMatrix();
  105. glTranslatef(m_CurPos.xpos,m_CurPos.ypos,m_CurPos.zpos);
  106. glRotatef(m_rotY,  0.0f,1.0f,0.0f);
  107. // glRotatef(m_rotZ,  0.0f,1.0f,0.0f);
  108. glRotatef(-m_rotX,  1.0f,0.0f,0.0f);
  109. glBegin(GL_TRIANGLE_FAN);
  110.     glTexCoord2f(0,1);
  111.     glVertex3f(0,0,-size*100);
  112.     glTexCoord2f(0,0);
  113.     glVertex3f(-size,0,0);
  114.     glTexCoord2f(1,0);
  115.     glVertex3f(size,0,0);
  116.     glTexCoord2f(0,0);
  117.     glVertex3f(0,size,0);
  118.     glTexCoord2f(1,0);
  119.     glVertex3f(-size,0,0);
  120. glEnd();
  121.     glPopMatrix();
  122. glEnable(GL_CULL_FACE);
  123. glDisable(GL_BLEND);
  124. }
  125. void  CBullet::DrawRocketBullet()
  126. {
  127.  //   m_rocket.DrawParticles(m_CurPos,m_direction);
  128. }
  129. void  CBullet::DrawExplosion()
  130. {
  131. glEnable(GL_TEXTURE_2D);
  132. glEnable(GL_ALPHA_TEST);
  133. glDisable(GL_CULL_FACE);
  134.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[3] );    
  135. glBlendFunc(GL_ONE,GL_ONE);
  136. glEnable(GL_BLEND);  
  137. float size=3.0f;
  138.     glPushMatrix();
  139. glTranslatef(m_oldPos.xpos,m_oldPos.ypos,m_oldPos.zpos);
  140. glRotatef(m_rotY,  0.0f,1.0f,0.0f);
  141. glBegin(GL_TRIANGLE_STRIP);
  142.     glTexCoord2f(0,0);
  143.     glVertex3f(-size,0,0);
  144.     glTexCoord2f(1,0);
  145.     glVertex3f(size,0,0);     
  146.     glTexCoord2f(0,1);
  147.         glVertex3f(-size,2*size,0);
  148.     glTexCoord2f(1,1);
  149.     glVertex3f(size,2*size,0);
  150. glEnd();
  151.     glPopMatrix();
  152. glDisable(GL_BLEND);
  153. glEnable(GL_CULL_FACE);
  154. }
  155. void  CBullet::DrawGunFire()
  156. {
  157. glEnable(GL_TEXTURE_2D);
  158. glEnable(GL_ALPHA_TEST);
  159. glDisable(GL_CULL_FACE);
  160. glBlendFunc(GL_ONE,GL_ONE);
  161. glEnable(GL_BLEND);
  162.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[3]);    
  163. float size=0.05f;
  164.     glPushMatrix();
  165. glTranslatef(m_oldPos.xpos,m_oldPos.ypos,m_oldPos.zpos);
  166. glRotatef(m_rotY,  0.0f,1.0f,0.0f);
  167. glRotatef(-m_rotX,  1.0f,0.0f,0.0f);
  168. glBegin(GL_TRIANGLE_STRIP);
  169.     glTexCoord2f(0,0);
  170.     glVertex3f(-size,-size,0);
  171.     glTexCoord2f(1,0);
  172.     glVertex3f(size,-size,0);     
  173.     glTexCoord2f(0,1);
  174.         glVertex3f(-size,size,0);
  175.     glTexCoord2f(1,1);
  176.     glVertex3f(size,size,0);
  177. glEnd();
  178.     glPopMatrix();
  179. glDisable(GL_BLEND);
  180. glEnable(GL_CULL_FACE);
  181. }