Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include "precompiled_header"
  2. #include "Deflector.h"
  3. #include "Ship.h"
  4. #include "Bullet.h"
  5. #include "../View/ViewManager.h"
  6. #include "../LevelState.h"
  7. #include "IDeflectionStrategy.h"
  8.  
  9. /* notes
  10. deflection shield
  11. - stores size and position
  12. - drains energy
  13. - moves with player
  14. - activate - turns dll on and off
  15. - queries for position
  16. levelstate
  17. - require flipping the collision call to ship version
  18. ship
  19. - override collide to deflect first
  20. */
  21.  
  22. Deflector::Deflector(Ship *_ship, LevelState *ls) :
  23. ship(_ship), state(ls),
  24. consumptionRate(.5f), strategyNumber(0),
  25. deflectionStrategy(nullptr), dll(nullptr)
  26. {
  27. SetActive(true);
  28. SetDimensions(ship->GetDimensions()*2);
  29. SetTexture("Resources\\Images\\emptyroundedbox.png");
  30. SetPosition(ship->GetPosition());
  31. ViewManager::GetInstance().AddObject(this, 1);
  32. }
  33.  
  34. void Deflector::DeactivateStrategy()
  35. {
  36. if(deflectionStrategy)
  37. {
  38. deflectionStrategy->Destroy();
  39. deflectionStrategy = nullptr;
  40. }
  41. if(dll)
  42. {
  43. FreeLibrary(dll);
  44. dll = nullptr;
  45. }
  46. }
  47.  
  48. Deflector::~Deflector(void)
  49. {
  50. DeactivateStrategy();
  51. ViewManager::GetInstance().RemoveObject(this);
  52. }
  53.  
  54. void Deflector:: Deflect(Bullet *_projectile)
  55. {
  56. // consume power
  57. ship->SetBattery(ship->GetBattery() - consumptionRate);
  58.  
  59. // flip the projectile back at the enemy
  60. float heading = _projectile->GetHeading();
  61. if(deflectionStrategy)
  62. deflectionStrategy->Deflect(heading);
  63. else
  64. heading += 1;
  65. _projectile->SetHeading(heading);
  66.  
  67. // ***********************************************************************
  68. // requires friend relationship
  69. std::vector<Bullet *> &playerBullets = state->playerBullets;
  70. std::vector<Bullet *> &enemyBullets = state->enemyBullets;
  71. // ***********************************************************************
  72.  
  73. for(unsigned int loop=0;loop<enemyBullets.size();)
  74. {
  75. if(enemyBullets[loop] == _projectile)
  76. {
  77. // switch which container it is in
  78. playerBullets.push_back(enemyBullets[loop]);
  79. enemyBullets.erase(enemyBullets.begin() + loop);
  80. return;
  81. }
  82. else ++loop;
  83. }
  84. }
  85.  
  86. void Deflector::Heartbeat(float _delta)
  87. {
  88. // check if we still have enough power to deflect
  89. float energyLeft = ship->GetBattery();
  90. if(energyLeft < consumptionRate)
  91. SetActive(false);
  92. else
  93. SetPosition(ship->GetPosition());
  94. }
  95.  
  96. typedef IDeflectionStrategy* (*sptr)();
  97. void Deflector::ActivateStrategy(const char *DLL_filename)
  98. {
  99. char str[255];
  100.  
  101. // unload the old strategy & dll if there is one
  102. DeactivateStrategy();
  103.  
  104. // try to load the library
  105. dll = LoadLibrary(DLL_filename);
  106.  
  107. // failure
  108. if(!dll)
  109. {
  110. sprintf(str, "****** Unable to load DLL: %s ******\n", DLL_filename);
  111. OutputDebugString(str);
  112. return;
  113. }
  114.  
  115. // success
  116. sptr p = reinterpret_cast<sptr>(GetProcAddress(dll, "CreateStrategy"));
  117. if(p)
  118. deflectionStrategy = p();
  119. else
  120. {
  121. sprintf(str, "*** Unable to find CreateStrategy function in %s ***\n", DLL_filename);
  122. OutputDebugString(str);
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement