/Weapons/Electricity.cpp

https://github.com/sterlinghirsh/AsteroidBlaster · C++ · 200 lines · 150 code · 32 blank · 18 comment · 47 complexity · 56712f0c8fa9c7a8b4495f09fc174a6e MD5 · raw file

  1. /**
  2. * Electricity
  3. * This kills the crab.
  4. * @author Sterling Hirsh (shirsh@calpoly.edu)
  5. * @date Valentine's Day <3
  6. */
  7. #include "Weapons/Electricity.h"
  8. #include "Utility/GlobalUtility.h"
  9. #include "Shots/ElectricityShot.h"
  10. #include "Utility/SoundEffect.h"
  11. #include "Text/GameMessage.h"
  12. #include "Network/gamestate.pb.h"
  13. Electricity::Electricity(AsteroidShip* owner, int _index) : Weapon(owner, _index),
  14. timeStartedFiring(0) {
  15. ELECTRICITY_WEAPON_INDEX = index;
  16. shotsFired = 0;
  17. shotsPerSec = 10;
  18. coolDown = 0;
  19. name = "Electricity Gun";
  20. currentFrame = 1; // Start this 1 ahead of lastFiredFame.
  21. lastFiredFrame = 0; // We use these three to handle the audio.
  22. soundPlaying = false;
  23. curAmmo = -1;
  24. purchased = false;
  25. soundHandle = NULL;
  26. shotid = 0;
  27. overheatLevel = 5;
  28. heatPerShot = 0.15;
  29. icon = "PikachusWrathIcon";
  30. r = 1;
  31. g = 1;
  32. b = 1;
  33. }
  34. Electricity::~Electricity() {
  35. // Do nothing.
  36. }
  37. void Electricity::update(double timeDiff) {
  38. Weapon::update(timeDiff);
  39. // Stop sound if we're no longer firing.
  40. if (currentFrame == lastFiredFrame && ship->isFiring) {
  41. if (!soundPlaying) {
  42. // We should play sound.
  43. soundPlaying = true;
  44. soundHandle = SoundEffect::playSoundEffect("ElectricitySound",
  45. ship->position, ship->velocity,
  46. ship == ship->gameState->ship, 0.5f, true);
  47. }
  48. } else if (shotid != 0) {
  49. // If we've got a shot in the world.
  50. if (soundPlaying) {
  51. SoundEffect::stopSoundEffect(soundHandle);
  52. soundPlaying = false;
  53. }
  54. if (ship->gameState->gsm != ClientMode) {
  55. shotsFired = 0;
  56. Object3D* shot = (*ship->custodian)[shotid];
  57. if (shot != NULL)
  58. shot->shouldRemove = true;
  59. }
  60. shotid = 0;
  61. }
  62. ++currentFrame;
  63. if (isOverheated() && this->ship == ship->gameState->ship)
  64. GameMessage::Add("Electricity overheated!", 30, 0, ship->gameState);
  65. }
  66. void Electricity::fire() {
  67. if (!isReady())
  68. return;
  69. lastFiredFrame = currentFrame;
  70. if (shotsFired == 0) {
  71. timeStartedFiring = ship->gameState->getGameTime();
  72. }
  73. int shotsToFire = 1;
  74. if (ship->gameState->gsm != ClientMode) {
  75. double curTime = ship->gameState->getGameTime();
  76. double timeFired = curTime - timeStartedFiring;
  77. shotsToFire = 0;
  78. if (timeFired <= 0.01) {
  79. shotsToFire = 1;
  80. } else {
  81. while (((shotsFired + shotsToFire) / timeFired) <= shotsPerSec) {
  82. shotsToFire++;
  83. }
  84. }
  85. }
  86. ElectricityShot* shot;
  87. if (shotid != 0) {
  88. shot = static_cast<ElectricityShot*>((*ship->custodian)[shotid]);
  89. if (shot == NULL) {
  90. // Oops!
  91. shotid = 0;
  92. } else {
  93. // Update shot.
  94. shot->setPosAndDir(ship->shotOrigin, ship->shotDirection);
  95. shot->setStrength(shotsToFire);
  96. }
  97. }
  98. // If it's client mode, wait for the shot packet to arrive,
  99. // and then add to the game.
  100. if (ship->gameState->gsm != ClientMode) {
  101. // Catch the oops from before.
  102. if (shotid == 0) {
  103. Point3D start = ship->shotOrigin;
  104. shot = new ElectricityShot(start,
  105. ship->shotDirection, index, ship, ship->gameState);
  106. ship->custodian->add(shot);
  107. shotid = shot->id;
  108. }
  109. shotsFired += shotsToFire;
  110. addHeat(heatPerShot * shotsToFire);
  111. } else if (shotsFired == 0) {
  112. shotsFired = 1;
  113. }
  114. ship->setShakeAmount(0.1f);
  115. }
  116. void Electricity::debug() {
  117. printf("Electricity!\n");
  118. }
  119. Point3D Electricity::project(Object3D* object, Vector3D addOn) {
  120. return *object->position;
  121. }
  122. bool Electricity::shouldFire(Point3D* target, Point3D* aim) {
  123. return true;
  124. }
  125. void Electricity::stopSounds() {
  126. if (soundHandle != NULL) {
  127. SoundEffect::stopSoundEffect(soundHandle);
  128. soundHandle = NULL;
  129. soundPlaying = false;
  130. }
  131. }
  132. /**
  133. * This is for the weapon bar.
  134. */
  135. double Electricity::getCoolDownAmount() {
  136. return 1 - clamp(currentHeat / overheatLevel, 0, 1);
  137. }
  138. bool Electricity::saveDiff(const ast::Weapon& old, ast::Weapon* weap) {
  139. bool changed = Weapon::saveDiff(old, weap);
  140. if (shotsFired != old.shotsfired()) {
  141. weap->set_shotsfired(shotsFired);
  142. changed = true;
  143. }
  144. if (timeStartedFiring != old.timestartedfiring()) {
  145. weap->set_timestartedfiring(timeStartedFiring);
  146. changed = true;
  147. }
  148. if (shotid != old.shotid()) {
  149. weap->set_shotid(shotid);
  150. changed = true;
  151. }
  152. return changed;
  153. }
  154. void Electricity::save(ast::Weapon* weap) {
  155. Weapon::save(weap);
  156. weap->set_shotsfired(shotsFired);
  157. weap->set_timestartedfiring(timeStartedFiring);
  158. weap->set_shotid(shotid);
  159. }
  160. void Electricity::load(const ast::Weapon& weap) {
  161. Weapon::load(weap);
  162. if (weap.has_shotsfired())
  163. shotsFired = weap.shotsfired();
  164. if (weap.has_timestartedfiring())
  165. timeStartedFiring = weap.timestartedfiring();
  166. if (weap.has_shotid())
  167. shotid = weap.shotid();
  168. }