CShootingAnimation.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "StdInc.h"
  2. #include <boost/math/constants/constants.hpp>
  3. #include "CShootingAnimation.h"
  4. #include "../../lib/BattleState.h"
  5. #include "CBattleInterface.h"
  6. #include "CCreatureAnimation.h"
  7. #include "../CGameInfo.h"
  8. #include "../../lib/CTownHandler.h"
  9. #include "CMovementStartAnimation.h"
  10. #include "CReverseAnimation.h"
  11. #include "CSpellEffectAnimation.h"
  12. #include "CClickableHex.h"
  13. bool CShootingAnimation::init()
  14. {
  15. if( !CAttackAnimation::checkInitialConditions() )
  16. return false;
  17. const CStack * shooter = attackingStack;
  18. if(!shooter || myAnim()->getType() == 5)
  19. {
  20. endAnim();
  21. return false;
  22. }
  23. // Create the projectile animation
  24. double projectileAngle; //in radians; if positive, projectiles goes up
  25. double straightAngle = 0.2; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  26. int fromHex = shooter->position;
  27. projectileAngle = atan2(static_cast<double>(abs(dest - fromHex) / GameConstants::BFIELD_WIDTH), static_cast<double>(abs(dest - fromHex) % GameConstants::BFIELD_WIDTH));
  28. if(fromHex < dest)
  29. projectileAngle = -projectileAngle;
  30. // Get further info about the shooter e.g. relative pos of projectile to unit.
  31. // If the creature id is 149 then it's a arrow tower which has no additional info so get the
  32. // actual arrow tower shooter instead.
  33. const CCreature *shooterInfo = shooter->getCreature();
  34. if (shooterInfo->idNumber == 149)
  35. {
  36. int creID = CGI->creh->factionToTurretCreature[owner->siegeH->town->town->typeID];
  37. shooterInfo = CGI->creh->creatures[creID];
  38. }
  39. SProjectileInfo spi;
  40. spi.creID = shooter->getCreature()->idNumber;
  41. spi.stackID = shooter->ID;
  42. spi.reverse = !shooter->attackerOwned;
  43. spi.step = 0;
  44. spi.frameNum = 0;
  45. if(vstd::contains(CGI->creh->idToProjectileSpin, shooterInfo->idNumber))
  46. spi.spin = CGI->creh->idToProjectileSpin[shooterInfo->idNumber];
  47. else
  48. {
  49. tlog2 << "Warning - no projectile spin for spi.creID " << shooterInfo->idNumber << std::endl;
  50. spi.spin = false;
  51. }
  52. SPoint xycoord = CClickableHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  53. SPoint destcoord;
  54. // The "master" point where all projectile positions relate to.
  55. static const SPoint projectileOrigin(181, 252);
  56. if (attackedStack)
  57. {
  58. destcoord = CClickableHex::getXYUnitAnim(dest, false, attackedStack, owner);
  59. destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
  60. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  61. if (projectileAngle > straightAngle)
  62. {
  63. //upper shot
  64. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->upperRightMissleOffsetX;
  65. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->upperRightMissleOffsetY;
  66. }
  67. else if (projectileAngle < -straightAngle)
  68. {
  69. //lower shot
  70. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->lowerRightMissleOffsetX;
  71. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->lowerRightMissleOffsetY;
  72. }
  73. else
  74. {
  75. //straight shot
  76. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX;
  77. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY;
  78. }
  79. double animSpeed = 23.0 * owner->getAnimSpeed(); // flight speed of projectile
  80. spi.lastStep = static_cast<int>(sqrt(static_cast<double>((destcoord.x - spi.x) * (destcoord.x - spi.x) + (destcoord.y - spi.y) * (destcoord.y - spi.y))) / animSpeed);
  81. if(spi.lastStep == 0)
  82. spi.lastStep = 1;
  83. spi.dx = (destcoord.x - spi.x) / spi.lastStep;
  84. spi.dy = (destcoord.y - spi.y) / spi.lastStep;
  85. spi.catapultInfo = 0;
  86. }
  87. else
  88. {
  89. // Catapult attack
  90. // These are the values for equations of this kind: f(x) = ax^2 + bx + c
  91. static const std::vector<SCatapultSProjectileInfo*> trajectoryCurves = boost::assign::list_of<SCatapultSProjectileInfo*>(new SCatapultSProjectileInfo(4.309, -3.198, 569.2, -296, 182))
  92. (new SCatapultSProjectileInfo(4.710, -3.11, 558.68, -258, 175))(new SCatapultSProjectileInfo(5.056, -3.003, 546.9, -236, 174))
  93. (new SCatapultSProjectileInfo(4.760, -2.74, 526.47, -216, 215))(new SCatapultSProjectileInfo(4.288, -2.496, 508.98, -223, 274))
  94. (new SCatapultSProjectileInfo(3.683, -3.018, 558.39, -324, 176))(new SCatapultSProjectileInfo(2.884, -2.607, 528.95, -366, 312))
  95. (new SCatapultSProjectileInfo(3.783, -2.364, 501.35, -227, 318));
  96. static std::map<int, int> hexToCurve = boost::assign::map_list_of<int, int>(29, 0)(62, 1)(95, 2)(130, 3)(182, 4)(12, 5)(50, 6)(183, 7);
  97. std::map<int, int>::iterator it = hexToCurve.find(dest.hex);
  98. if (it == hexToCurve.end())
  99. {
  100. tlog1 << "For the hex position " << dest.hex << " is no curve defined.";
  101. endAnim();
  102. return false;
  103. }
  104. else
  105. {
  106. int curveID = it->second;
  107. spi.catapultInfo = trajectoryCurves[curveID];
  108. double animSpeed = 3.318 * owner->getAnimSpeed();
  109. spi.lastStep = static_cast<int>((spi.catapultInfo->toX - spi.catapultInfo->fromX) / animSpeed);
  110. spi.dx = animSpeed;
  111. spi.dy = 0;
  112. spi.x = xycoord.x + projectileOrigin.x + shooterInfo->rightMissleOffsetX + 17.;
  113. spi.y = xycoord.y + projectileOrigin.y + shooterInfo->rightMissleOffsetY + 10.;
  114. // Add explosion anim
  115. int xEnd = static_cast<int>(spi.x + spi.lastStep * spi.dx);
  116. int yEnd = static_cast<int>(spi.catapultInfo->calculateY(xEnd));
  117. owner->addNewAnim( new CSpellEffectAnimation(owner, "SGEXPL.DEF", xEnd - 126, yEnd - 105));
  118. }
  119. }
  120. // Set starting frame
  121. if(spi.spin)
  122. {
  123. spi.frameNum = 0;
  124. }
  125. else
  126. {
  127. double pi = boost::math::constants::pi<double>();
  128. spi.frameNum = static_cast<int>(((pi / 2.0 - projectileAngle) / (2.0 * pi) + 1/(static_cast<double>(2*(owner->idToProjectile[spi.creID]->ourImages.size()-1)))) * (owner->idToProjectile[spi.creID]->ourImages.size()-1));
  129. }
  130. // Set projectile animation start delay which is specified in frames
  131. spi.animStartDelay = shooterInfo->attackClimaxFrame;
  132. owner->projectiles.push_back(spi);
  133. //attack animation
  134. shooting = true;
  135. if(projectileAngle > straightAngle) //upper shot
  136. group = CCreatureAnim::SHOOT_UP;
  137. else if(projectileAngle < -straightAngle) //lower shot
  138. group = CCreatureAnim::SHOOT_DOWN;
  139. else //straight shot
  140. group = CCreatureAnim::SHOOT_FRONT;
  141. return true;
  142. }
  143. void CShootingAnimation::nextFrame()
  144. {
  145. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  146. {
  147. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it->first);
  148. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it->first);
  149. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  150. return;
  151. }
  152. CAttackAnimation::nextFrame();
  153. }
  154. void CShootingAnimation::endAnim()
  155. {
  156. CBattleAnimation::endAnim();
  157. delete this;
  158. }
  159. CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, SBattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  160. : CAttackAnimation(_owner, attacker, _dest, _attacked), catapultDamage(_catapultDmg), catapult(_catapult)
  161. {
  162. }