Enemy.cpp
资源名称:g.rar [点击查看]
上传用户:laitongbao
上传日期:2021-02-20
资源大小:8176k
文件大小:3k
源码类别:

射击游戏

开发平台:

Visual C++

  1. // Enemy.cpp: implementation of the Enemy class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Enemy.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. #include "Exploder.h"
  10. #include "LaserGun.h"
  11. #include "playership.h"
  12. #include "MachineGun.h"
  13. #include "math.h"
  14. Enemy1::Enemy1(VECTOR3 pos0,VECTOR3 v0,int bonus)//bonus是否死后会掉奖励
  15. {
  16. mesh = (MeshObject*)g_tree->AddObject(new MeshObject("enemy1",pos0),"base");
  17. mesh->SetVelocity(v0);
  18. mesh->LoadMeshData("enemy1.x");
  19. this->bonus = bonus;
  20. expInner = new Exploder(6.0f,40,15.f);
  21. expOuter = new Exploder(1.0f,50,100.f);
  22. expBengin = new Exploder(3.f,10,10.f);
  23. this->gun1 = new LaserGun(this->mesh ,3.f);
  24. this->gun2 = new LaserGun(this->mesh ,-3.f);
  25. this->deadStayTimer = new FrameTimer(30);
  26. this->alive = true;
  27. this->active = true;
  28. shell = 10;
  29. t0 = 0;
  30. angle = atanf(v0.x/-v0.z);
  31. whichGunToFire = true;
  32. }
  33. void Enemy1::Damage(int dam)
  34. {
  35. this->shell -= dam;
  36. if(shell<=0&&active)
  37. {
  38. deadStayTimer->startTimer();
  39. expBengin->Explode(TransitionController::GetWorldPosition(mesh));
  40. mesh->SetRotateVelocity(VECTOR3(-2.f,0.f,-0.5f));
  41. active = false;
  42. points += 100;//得分
  43. }
  44. }
  45. void Enemy1::TurnLeft()
  46. {
  47. if(!active)
  48. return;
  49. angle -= g_timer->GetFrameTime()*0.1f;
  50. TransitionController::SetRotation(mesh,VECTOR3(0.f,-angle,0.f));
  51. VECTOR3 v;
  52. VECTOR3 currentspeed = mesh->GetVelocity();
  53. v.x = currentspeed.Length()*sinf(angle);
  54. v.z = -currentspeed.Length()*cosf(angle);
  55. mesh->SetVelocity(v);
  56. }
  57. void Enemy1::TurnRight()
  58. {
  59. if(!active)
  60. return;
  61. angle += g_timer->GetFrameTime()*0.2f;
  62. TransitionController::SetRotation(mesh,VECTOR3(0.f,-angle,0.f));
  63. VECTOR3 v;
  64. VECTOR3 currentspeed = mesh->GetVelocity();
  65. v.x = currentspeed.Length()*sinf(angle);
  66. v.z = -currentspeed.Length()*cosf(angle);
  67. mesh->SetVelocity(v);
  68. }
  69. void Enemy1::DoFrame()
  70. {
  71. //go toward player
  72. VECTOR3 v = g_pship->mesh->GetWorldPos() - mesh->GetWorldPos();
  73. float current_angle = atanf(v.x/-v.z);
  74. if(current_angle < angle)
  75. {
  76. TurnLeft();
  77. }else
  78. {
  79. TurnRight();
  80. }
  81. //fire
  82. if(g_timer->GetTime () - t0 >1.0f&&active&&current_angle - angle<0.1f&&current_angle - angle>-0.1f)
  83. {
  84. VECTOR3 direction = TransitionController::GetWorldPosition(g_pship->mesh)-TransitionController::GetWorldPosition(mesh);
  85. if(direction.Length()<300.f)//fire when get closed
  86. {
  87. //g_enemyshotsound->PlaySound();
  88. if(whichGunToFire)
  89. this->gun1->Fire(direction);
  90. else
  91. this->gun2->Fire(direction);
  92. }
  93. whichGunToFire = !whichGunToFire;
  94. t0 = g_timer->GetTime ();
  95. }
  96. //dying
  97. if(deadStayTimer->isRunning())
  98. {
  99. deadStayTimer->update();
  100. if(deadStayTimer->reach())
  101. {
  102. g_expsound->PlaySound();
  103. this->expInner->Explode(TransitionController::GetWorldPosition(mesh));
  104. this->expOuter->Explode(TransitionController::GetWorldPosition(mesh));
  105. deadStayTimer->stopTimer();
  106. this->alive = false;
  107. }
  108. }
  109. }
  110. Enemy1::~Enemy1()
  111. {
  112. delete gun1;
  113. delete gun2;
  114. delete expOuter;
  115. delete expInner;
  116. g_tree->DeleteObject(mesh);
  117. mesh = NULL;
  118. }