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

射击游戏

开发平台:

Visual C++

  1. // PlayerShip.cpp: implementation of the PlayerShip class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "PlayerShip.h"
  6. #include "Exploder.h"
  7. #include "MachineGun.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. Force *xengine ;
  12. Force *zengine ; 
  13. Resistance * ship_resistance;
  14. PlayerShip::PlayerShip()
  15. {
  16. static bool init = false;
  17. if(!init)
  18. {
  19. xengine = (Force*)g_tree->AddObject(  &Force("xengine"),(NodeObject*)NULL);
  20. zengine = (Force*)g_tree->AddObject(  &Force("zengine"),(NodeObject*)NULL);
  21. ship_resistance = (Resistance*)g_tree->AddObject ( &Resistance("shipResistance"),(NodeObject*)NULL);
  22. init = true;
  23. }
  24. mesh = (MeshObject*)g_tree->AddObject(&MeshObject("playership",VECTOR3(0,0,0)),"base");
  25. wake = (Particle_Emit*)g_tree->AddObject(&Particle_Emit("wake",5.f,0.05f,"Fire\wake.bmp",
  26. 0xff2266ff,0xff664400,VECTOR3(0.0f,0.0f,-5.0f)),mesh);
  27. mesh->LoadMeshData("x_wings.x");
  28. mesh->SetMass(100.f);
  29. TransitionController::SetRotation(mesh,VECTOR3(0.f,0.f,0.f));
  30. gun1 = new MachineGun(mesh->GetName(),-1.f);
  31. gun2 = new MachineGun(mesh->GetName(),1.f);
  32. explode = new Exploder(15.f,10,20.f);
  33. explode2 = new Exploder(1.f,50,300.f);
  34. expBengin = new Exploder(10.f,20,10.f);
  35. shell = 100;
  36. xengine->SetPower (200.f);
  37. zengine->SetPower (100.f);
  38. xengine->AddObject(mesh);
  39. zengine->AddObject(mesh);
  40. ship_resistance->AddObject(mesh);
  41. ship_resistance->SetPower(0.03f);
  42. this->deadStayTimer = new FrameTimer(80);
  43. active = true;
  44. }
  45. void PlayerShip::Reset()
  46. {
  47. mesh->SetVisible (true);
  48. wake->SetVisible (true);
  49. gun1->emit->SetVisible(true);
  50. gun2->emit->SetVisible(true);
  51. TransitionController::SetPosition(mesh,VECTOR3());
  52. TransitionController::SetRotation(mesh,VECTOR3());
  53. mesh->SetRotateVelocity(VECTOR3());
  54. active = true;
  55. wake->SetSize(5.f);
  56. wake->SetLifeTime(0.05f);
  57. shell = 100;
  58. explode ->Reset();
  59. explode2 ->Reset();
  60. expBengin->Reset();
  61. }
  62. void PlayerShip::Fire()
  63. {
  64. static float t = 0;
  65. if(g_timer->GetTime () - t >0.10f&&active)
  66. {
  67. g_playershotsound->PlaySound();
  68. this->gun2->Fire();
  69. this->gun1->Fire();
  70. t = g_timer->GetTime ();
  71. }
  72. }
  73. void PlayerShip::Bob()
  74. {
  75. }
  76. void PlayerShip::MoveLeft()
  77. {
  78. if(!active)
  79. return;
  80. VECTOR3 rv = TransitionController::GetRotation(mesh);
  81. if(rv.z>PI/12)
  82. {
  83. xengine->SetDirection(VECTOR3(-1.f,0.f,0.f));
  84. xengine->Enable();
  85. }
  86. else
  87. {
  88. xengine->Disable();
  89. }
  90. if(rv.z<PI/6)
  91. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,2.f));
  92. else
  93. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
  94. }
  95. void PlayerShip::MoveRight()
  96. {
  97. if(!active)
  98. return;
  99. VECTOR3 rv = TransitionController::GetRotation(mesh);
  100. if(rv.z<-PI/12)
  101. {
  102. xengine->SetDirection(VECTOR3(1.f,0.f,0.f));
  103. xengine->Enable();
  104. }
  105. else
  106. {
  107. xengine->Disable();
  108. }
  109. if(rv.z>-PI/6)
  110. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,-2.f));
  111. else
  112. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
  113. }
  114. void PlayerShip::RotateBack()
  115. {
  116. if(!active)
  117. return;
  118. VECTOR3 rv = TransitionController::GetRotation(mesh);
  119. if(rv.z > 0.1f)
  120. {
  121. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,-2.f));
  122. }
  123. else if(rv.z <-0.1f)
  124. {
  125. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,2.f));
  126. }else
  127. {
  128. mesh->SetRotateVelocity(VECTOR3(0.f,0.f,0.f));
  129. }
  130. }
  131. void PlayerShip::MoveForward()
  132. {
  133. if(!active)
  134. return;
  135. zengine->SetDirection(VECTOR3(0.f,0.f,1.f));
  136. wake->SetSize(8.f);
  137. zengine->Enable();
  138. }
  139. void PlayerShip::MoveBackward()
  140. {
  141. if(!active)
  142. return;
  143. zengine->SetDirection(VECTOR3(0.f,0.f,-1.f));
  144. wake->SetSize(4.f);
  145. zengine->Enable();
  146. }
  147. void PlayerShip::ZengineStop()
  148. {
  149. zengine->Disable();
  150. wake->SetSize(5.f);
  151. }
  152. void PlayerShip::XengineStop()
  153. {
  154. xengine->Disable();
  155. }
  156. void PlayerShip::DoFrame()
  157. {
  158. if(deadStayTimer->isRunning())
  159. {
  160. deadStayTimer->update();
  161. mesh->SetRotateVelocity(VECTOR3(0.1f,0.f,-1.f));
  162. if(deadStayTimer->reach())
  163. {
  164. Die();
  165. deadStayTimer->stopTimer();
  166. Reset();
  167. }
  168. }
  169. VECTOR3 pv = TransitionController::GetPosition(mesh);
  170. if(pv.x>50.f)
  171. {
  172. pv.x = 50.f;
  173. TransitionController::SetPosition(mesh,pv);
  174. }
  175. if(pv.x<-50.f)
  176. {
  177. pv.x = -50.f;
  178. TransitionController::SetPosition(mesh,pv);
  179. }
  180. if(pv.z<-30.f)
  181. {
  182. pv.z = -30.f;
  183. TransitionController::SetPosition(mesh,pv);
  184. }
  185. if(pv.z>100.f)
  186. {
  187. pv.z = 100.f;
  188. TransitionController::SetPosition(mesh,pv);
  189. }
  190. wake->Emit(VECTOR3(0,0,-100));
  191. }
  192. void PlayerShip::Damage(int value)
  193. {
  194. shell -= value;
  195. if(shell<=0&&active)
  196. {
  197. deadStayTimer->startTimer();
  198. this->expBengin->Explode(mesh->GetWorldPos());
  199. wake->SetSize(10.f);
  200. wake->SetLifeTime(0.2f);
  201. active = false;
  202. }
  203. }
  204. void PlayerShip::Die()
  205. {
  206. g_expsound->PlaySound();
  207. this->explode->Explode(mesh->GetWorldPos());
  208. this->explode2->Explode(mesh->GetWorldPos());
  209. mesh->SetVisible (false);
  210. wake->SetVisible (false);
  211. gun1->emit->SetVisible(false);
  212. gun2->emit->SetVisible(false);
  213. }
  214. void PlayerShip::PowerUp()
  215. {
  216. gun1->PowerUp();
  217. gun2->PowerUp();
  218. }
  219. PlayerShip::~PlayerShip()
  220. {
  221. delete explode;
  222. delete explode2;
  223. delete expBengin;
  224. delete deadStayTimer;
  225. }