ROCKET.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 4k
Category:

OpenCV

Development Platform:

Visual C++

  1. #include "rocket.h"
  2. CRocket::CRocket()
  3. {
  4. velocity = CVector(0.0, 0.0, 120.0);
  5. acceleration = CVector(0.0, 0.0, 0.0);
  6. distanceTravel = 0.0;
  7. size = 1.0f;
  8. isExplosion = false;
  9. explosion = NULL;
  10. explosionTex = new CTexture;
  11. Load();
  12. }
  13. CRocket::~CRocket()
  14. {
  15. if (explosion != NULL)
  16. {
  17. explosion->KillSystem();
  18. delete [] explosion;
  19. explosion = NULL;
  20. }
  21. if (explosionTex != NULL)
  22. {
  23. delete explosionTex;
  24. explosionTex = NULL;
  25. }
  26. }
  27. void CRocket::SetupExplosionTexture()
  28. {
  29. glGenTextures(1, &explosionTex->texID);
  30. glBindTexture(GL_TEXTURE_2D, explosionTex->texID);
  31. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  32. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  33. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  34. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  35. glTexImage2D(GL_TEXTURE_2D, 0, 4, explosionTex->width, explosionTex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
  36. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, explosionTex->width, explosionTex->height, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
  37. }
  38. void CRocket::OnAnimate(scalar_t deltaTime)
  39. {
  40. float cosYaw = (float)cos(DEG2RAD(direction));
  41. float sinYaw = (float)sin(DEG2RAD(direction)); 
  42. float sinPitch = (float)sin(DEG2RAD(pitch));
  43. float speed = velocity.z * deltaTime;
  44. position.x += float(cosYaw)*speed;
  45. position.y += float(sinPitch)*speed;
  46. position.z += float(sinYaw)*speed;
  47. distanceTravel += position.Length();
  48. if (isExplosion)
  49. explosion->Update(deltaTime);
  50. if (!isExplosion)
  51. {
  52. if (distanceTravel >= 500000.0f)
  53. {
  54. isExplosion = true;
  55. velocity = CVector(0.0, 0.0, 0.0);
  56. explosion = new CExplosion(10, position, 0.1, explosionTex->texID);
  57. }
  58. }
  59. }
  60. void CRocket::OnCollision(CObject *collisionObject)
  61. {
  62. if (!isExplosion)
  63. {
  64. if (typeid(*collisionObject) == typeid(CTerrain))
  65. {
  66. if (((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size >= position.y)
  67. {
  68. isExplosion = true;
  69. velocity = CVector(0.0, 0.0, 0.0);
  70. explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
  71. PlaySound();
  72. }
  73. // 爆炸
  74. }
  75. if (typeid(*collisionObject) == typeid(COgroEnemy))
  76. {
  77. isExplosion = true;
  78. velocity = CVector(0.0, 0.0, 0.0);
  79. explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
  80. PlaySound();
  81. }
  82. if (typeid(*collisionObject) == typeid(CSodEnemy))
  83. {
  84. isExplosion = true;
  85. velocity = CVector(0.0, 0.0, 0.0);
  86. explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
  87. PlaySound();
  88. }
  89. }
  90. }
  91. void CRocket::OnDraw(CCamera *camera)
  92. {
  93. // 如果火箭没有爆炸,则绘制火箭模型
  94. if (!isExplosion)
  95. {
  96. glEnable(GL_TEXTURE_2D);
  97. glColor3f(1.0, 1.0, 1.0);
  98. glTranslatef(position.x, position.y, position.z);
  99. glRotatef(-direction, 0.0, 1.0, 0.0);
  100. glScalef(0.025f, 0.025f, 0.025f);
  101. RenderFrame(0);
  102. glDisable(GL_TEXTURE_2D);
  103. }
  104. // 绘制爆炸
  105. else
  106. {
  107. glDisable(GL_FOG);
  108. explosion->Render();
  109. glEnable(GL_FOG);
  110. }
  111. }
  112. void CRocket::Load()
  113. {
  114. CMD2Model::Load("models\rocketair.md2", "models\rocket.pcx");
  115. explosionTex->LoadTexture("explosion.bmp");
  116. SetupExplosionTexture();
  117. }
  118. void CRocket::Unload()
  119. {
  120. }
  121. void CRocket::OnPrepare()
  122. {
  123. // 进行该实体与其他对象之间的碰撞检测操作
  124. if (!isExplosion)
  125. ProcessCollisions(FindRoot());
  126. if (isExplosion)
  127. {
  128. if (explosion->IsDead() && !audioSys->GetPerformance()->IsPlaying(entitySound->GetSegment(), NULL))
  129. {
  130. explosion->KillSystem();
  131. delete explosion;
  132. explosion = NULL;
  133. isExplosion = false;
  134. isDead = true;
  135. }
  136. }
  137. }