CBattleAnimations.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * CBattleAnimations.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CBattleAnimations.h"
  12. #include <boost/math/constants/constants.hpp>
  13. #include "CBattleInterfaceClasses.h"
  14. #include "CBattleInterface.h"
  15. #include "CBattleProjectileController.h"
  16. #include "CBattleSiegeController.h"
  17. #include "CBattleFieldController.h"
  18. #include "CBattleEffectsController.h"
  19. #include "CBattleStacksController.h"
  20. #include "CCreatureAnimation.h"
  21. #include "../CGameInfo.h"
  22. #include "../CMusicHandler.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../Graphics.h"
  25. #include "../gui/CAnimation.h"
  26. #include "../gui/CCursorHandler.h"
  27. #include "../gui/CGuiHandler.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CStack.h"
  30. #include "../../lib/CTownHandler.h"
  31. #include "../../lib/mapObjects/CGTownInstance.h"
  32. CBattleAnimation::CBattleAnimation(CBattleInterface * _owner)
  33. : owner(_owner),
  34. ID(_owner->stacksController->animIDhelper++),
  35. initialized(false)
  36. {
  37. logAnim->trace("Animation #%d created", ID);
  38. }
  39. bool CBattleAnimation::tryInitialize()
  40. {
  41. assert(!initialized);
  42. if ( init() )
  43. {
  44. initialized = true;
  45. return true;
  46. }
  47. return false;
  48. }
  49. bool CBattleAnimation::isInitialized()
  50. {
  51. return initialized;
  52. }
  53. CBattleAnimation::~CBattleAnimation()
  54. {
  55. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  56. for(auto & elem : pendingAnimations())
  57. {
  58. if(elem == this)
  59. elem = nullptr;
  60. }
  61. logAnim->trace("Animation #%d deleted", ID);
  62. }
  63. std::vector<CBattleAnimation *> & CBattleAnimation::pendingAnimations()
  64. {
  65. return owner->stacksController->currentAnimations;
  66. }
  67. std::shared_ptr<CCreatureAnimation> CBattleAnimation::stackAnimation(const CStack * stack)
  68. {
  69. return owner->stacksController->stackAnimation[stack->ID];
  70. }
  71. bool CBattleAnimation::stackFacingRight(const CStack * stack)
  72. {
  73. return owner->stacksController->stackFacingRight[stack->ID];
  74. }
  75. void CBattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  76. {
  77. owner->stacksController->stackFacingRight[stack->ID] = facingRight;
  78. }
  79. bool CBattleAnimation::checkInitialConditions()
  80. {
  81. int lowestMoveID = ID;
  82. CBattleStackAnimation * thAnim = dynamic_cast<CBattleStackAnimation *>(this);
  83. CEffectAnimation * thSen = dynamic_cast<CEffectAnimation *>(this);
  84. for(auto & elem : pendingAnimations())
  85. {
  86. CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem);
  87. // all effect animations can play concurrently with each other
  88. if(sen && thSen && sen != thSen)
  89. continue;
  90. CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(elem);
  91. // if there is high-priority reverse animation affecting our stack then this animation will wait
  92. if(revAnim && thAnim && revAnim && revAnim->stack->ID == thAnim->stack->ID && revAnim->priority)
  93. return false;
  94. if(elem)
  95. vstd::amin(lowestMoveID, elem->ID);
  96. }
  97. return ID == lowestMoveID;
  98. }
  99. CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * owner, const CStack * stack)
  100. : CBattleAnimation(owner),
  101. myAnim(stackAnimation(stack)),
  102. stack(stack)
  103. {
  104. assert(myAnim);
  105. }
  106. void CBattleStackAnimation::shiftColor(const ColorShifter * shifter)
  107. {
  108. assert(myAnim);
  109. myAnim->shiftColor(shifter);
  110. }
  111. void CAttackAnimation::nextFrame()
  112. {
  113. if(myAnim->getType() != group)
  114. {
  115. myAnim->setType(group);
  116. myAnim->onAnimationReset += [&](){ delete this; };
  117. }
  118. if(!soundPlayed)
  119. {
  120. if(shooting)
  121. CCS->soundh->playSound(battle_sound(getCreature(), shoot));
  122. else
  123. CCS->soundh->playSound(battle_sound(getCreature(), attack));
  124. soundPlayed = true;
  125. }
  126. }
  127. CAttackAnimation::~CAttackAnimation()
  128. {
  129. myAnim->setType(CCreatureAnim::HOLDING);
  130. }
  131. bool CAttackAnimation::checkInitialConditions()
  132. {
  133. for(auto & elem : pendingAnimations())
  134. {
  135. CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(elem);
  136. CReverseAnimation * revAnim = dynamic_cast<CReverseAnimation *>(stAnim);
  137. if(revAnim && attackedStack) // enemy must be fully reversed
  138. {
  139. if (revAnim->stack->ID == attackedStack->ID)
  140. return false;
  141. }
  142. }
  143. return CBattleAnimation::checkInitialConditions();
  144. }
  145. const CCreature * CAttackAnimation::getCreature()
  146. {
  147. if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
  148. return owner->siegeController->getTurretCreature();
  149. else
  150. return attackingStack->getCreature();
  151. }
  152. CAttackAnimation::CAttackAnimation(CBattleInterface *_owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  153. : CBattleStackAnimation(_owner, attacker),
  154. shooting(false),
  155. group(CCreatureAnim::SHOOT_FRONT),
  156. soundPlayed(false),
  157. dest(_dest),
  158. attackedStack(defender),
  159. attackingStack(attacker)
  160. {
  161. assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
  162. attackingStackPosBeforeReturn = attackingStack->getPosition();
  163. }
  164. CDefenceAnimation::CDefenceAnimation(StackAttackedInfo _attackedInfo, CBattleInterface * _owner)
  165. : CBattleStackAnimation(_owner, _attackedInfo.defender),
  166. attacker(_attackedInfo.attacker),
  167. rangedAttack(_attackedInfo.indirectAttack),
  168. killed(_attackedInfo.killed),
  169. timeToWait(0)
  170. {
  171. logAnim->debug("Created defence anim for %s", _attackedInfo.defender->getName());
  172. }
  173. bool CDefenceAnimation::init()
  174. {
  175. ui32 lowestMoveID = ID;
  176. for(auto & elem : pendingAnimations())
  177. {
  178. CDefenceAnimation * defAnim = dynamic_cast<CDefenceAnimation *>(elem);
  179. if(defAnim && defAnim->stack->ID != stack->ID)
  180. continue;
  181. CAttackAnimation * attAnim = dynamic_cast<CAttackAnimation *>(elem);
  182. if(attAnim && attAnim->stack->ID != stack->ID)
  183. continue;
  184. CEffectAnimation * sen = dynamic_cast<CEffectAnimation *>(elem);
  185. if (sen && attacker == nullptr)
  186. return false;
  187. if (sen)
  188. continue;
  189. CReverseAnimation * animAsRev = dynamic_cast<CReverseAnimation *>(elem);
  190. if(animAsRev)
  191. return false;
  192. if(elem)
  193. vstd::amin(lowestMoveID, elem->ID);
  194. }
  195. if(ID > lowestMoveID)
  196. return false;
  197. //reverse unit if necessary
  198. if(attacker && owner->getCurrentPlayerInterface()->cb->isToReverse(stack->getPosition(), attacker->getPosition(), stackFacingRight(stack), attacker->doubleWide(), stackFacingRight(attacker)))
  199. {
  200. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, stack->getPosition(), true));
  201. return false;
  202. }
  203. //unit reversed
  204. if(rangedAttack && attacker != nullptr && owner->projectilesController->hasActiveProjectile(attacker)) //delay hit animation
  205. {
  206. return false;
  207. }
  208. // synchronize animation with attacker, unless defending or attacked by shooter:
  209. // wait for 1/2 of attack animation
  210. if (!rangedAttack && getMyAnimType() != CCreatureAnim::DEFENCE)
  211. {
  212. float frameLength = AnimationControls::getCreatureAnimationSpeed(
  213. stack->getCreature(), stackAnimation(stack).get(), getMyAnimType());
  214. timeToWait = myAnim->framesInGroup(getMyAnimType()) * frameLength / 2;
  215. myAnim->setType(CCreatureAnim::HOLDING);
  216. }
  217. else
  218. {
  219. timeToWait = 0;
  220. startAnimation();
  221. }
  222. return true; //initialized successfuly
  223. }
  224. std::string CDefenceAnimation::getMySound()
  225. {
  226. if(killed)
  227. return battle_sound(stack->getCreature(), killed);
  228. else if(stack->defendingAnim)
  229. return battle_sound(stack->getCreature(), defend);
  230. else
  231. return battle_sound(stack->getCreature(), wince);
  232. }
  233. CCreatureAnim::EAnimType CDefenceAnimation::getMyAnimType()
  234. {
  235. if(killed)
  236. {
  237. if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEATH_RANGED) > 0)
  238. return CCreatureAnim::DEATH_RANGED;
  239. else
  240. return CCreatureAnim::DEATH;
  241. }
  242. if(stack->defendingAnim)
  243. return CCreatureAnim::DEFENCE;
  244. else
  245. return CCreatureAnim::HITTED;
  246. }
  247. void CDefenceAnimation::startAnimation()
  248. {
  249. CCS->soundh->playSound(getMySound());
  250. myAnim->setType(getMyAnimType());
  251. myAnim->onAnimationReset += [&](){ delete this; };
  252. }
  253. void CDefenceAnimation::nextFrame()
  254. {
  255. if (timeToWait > 0)
  256. {
  257. timeToWait -= float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000;
  258. if (timeToWait <= 0)
  259. startAnimation();
  260. }
  261. CBattleAnimation::nextFrame();
  262. }
  263. CDefenceAnimation::~CDefenceAnimation()
  264. {
  265. if(killed)
  266. {
  267. if(rangedAttack && myAnim->framesInGroup(CCreatureAnim::DEAD_RANGED) > 0)
  268. myAnim->setType(CCreatureAnim::DEAD_RANGED);
  269. else
  270. myAnim->setType(CCreatureAnim::DEAD);
  271. }
  272. else
  273. {
  274. myAnim->setType(CCreatureAnim::HOLDING);
  275. }
  276. }
  277. CDummyAnimation::CDummyAnimation(CBattleInterface * _owner, int howManyFrames)
  278. : CBattleAnimation(_owner),
  279. counter(0),
  280. howMany(howManyFrames)
  281. {
  282. logAnim->debug("Created dummy animation for %d frames", howManyFrames);
  283. }
  284. bool CDummyAnimation::init()
  285. {
  286. return true;
  287. }
  288. void CDummyAnimation::nextFrame()
  289. {
  290. counter++;
  291. if(counter > howMany)
  292. delete this;
  293. }
  294. bool CMeleeAttackAnimation::init()
  295. {
  296. if(!CAttackAnimation::checkInitialConditions())
  297. return false;
  298. if(!attackingStack || myAnim->isDead())
  299. {
  300. delete this;
  301. return false;
  302. }
  303. bool toReverse = owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStackPosBeforeReturn, attackedStack->getPosition(), stackFacingRight(stack), attackedStack->doubleWide(), stackFacingRight(attackedStack));
  304. if(toReverse)
  305. {
  306. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, attackingStackPosBeforeReturn, true));
  307. return false;
  308. }
  309. // opponent must face attacker ( = different directions) before he can be attacked
  310. if(attackingStack && attackedStack &&
  311. stackFacingRight(attackingStack) == stackFacingRight(attackedStack))
  312. return false;
  313. //reversed
  314. shooting = false;
  315. static const CCreatureAnim::EAnimType mutPosToGroup[] =
  316. {
  317. CCreatureAnim::ATTACK_UP,
  318. CCreatureAnim::ATTACK_UP,
  319. CCreatureAnim::ATTACK_FRONT,
  320. CCreatureAnim::ATTACK_DOWN,
  321. CCreatureAnim::ATTACK_DOWN,
  322. CCreatureAnim::ATTACK_FRONT
  323. };
  324. static const CCreatureAnim::EAnimType mutPosToGroup2H[] =
  325. {
  326. CCreatureAnim::VCMI_2HEX_UP,
  327. CCreatureAnim::VCMI_2HEX_UP,
  328. CCreatureAnim::VCMI_2HEX_FRONT,
  329. CCreatureAnim::VCMI_2HEX_DOWN,
  330. CCreatureAnim::VCMI_2HEX_DOWN,
  331. CCreatureAnim::VCMI_2HEX_FRONT
  332. };
  333. int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
  334. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  335. if(mutPos == -1 && attackingStack->doubleWide())
  336. {
  337. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->getPosition());
  338. }
  339. if (mutPos == -1 && attackedStack->doubleWide())
  340. {
  341. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, attackedStack->occupiedHex());
  342. }
  343. if (mutPos == -1 && attackedStack->doubleWide() && attackingStack->doubleWide())
  344. {
  345. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, attackedStack->occupiedHex());
  346. }
  347. switch(mutPos) //attack direction
  348. {
  349. case 0:
  350. case 1:
  351. case 2:
  352. case 3:
  353. case 4:
  354. case 5:
  355. group = mutPosToGroup[mutPos];
  356. if(attackingStack->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
  357. {
  358. CCreatureAnim::EAnimType group2H = mutPosToGroup2H[mutPos];
  359. if(myAnim->framesInGroup(group2H)>0)
  360. group = group2H;
  361. }
  362. break;
  363. default:
  364. logGlobal->error("Critical Error! Wrong dest in stackAttacking! dest: %d; attacking stack pos: %d; mutual pos: %d", dest.hex, attackingStackPosBeforeReturn, mutPos);
  365. group = CCreatureAnim::ATTACK_FRONT;
  366. break;
  367. }
  368. return true;
  369. }
  370. CMeleeAttackAnimation::CMeleeAttackAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  371. : CAttackAnimation(_owner, attacker, _dest, _attacked)
  372. {
  373. logAnim->debug("Created melee attack anim for %s", attacker->getName());
  374. }
  375. bool CMovementAnimation::init()
  376. {
  377. if( !CBattleAnimation::checkInitialConditions() )
  378. return false;
  379. if(!stack || myAnim->isDead())
  380. {
  381. delete this;
  382. return false;
  383. }
  384. if(stackAnimation(stack)->framesInGroup(CCreatureAnim::MOVING) == 0 ||
  385. stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
  386. {
  387. //no movement or teleport, end immediately
  388. delete this;
  389. return false;
  390. }
  391. //reverse unit if necessary
  392. if(owner->stacksController->shouldRotate(stack, oldPos, nextHex))
  393. {
  394. // it seems that H3 does NOT plays full rotation animation here in most situations
  395. // Logical since it takes quite a lot of time
  396. if (curentMoveIndex == 0) // full rotation only for moving towards first tile.
  397. {
  398. owner->stacksController->addNewAnim(new CReverseAnimation(owner, stack, oldPos, true));
  399. return false;
  400. }
  401. else
  402. {
  403. rotateStack(oldPos);
  404. }
  405. }
  406. if(myAnim->getType() != CCreatureAnim::MOVING)
  407. {
  408. myAnim->setType(CCreatureAnim::MOVING);
  409. }
  410. if (owner->moveSoundHander == -1)
  411. {
  412. owner->moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
  413. }
  414. Point begPosition = owner->stacksController->getStackPositionAtHex(oldPos, stack);
  415. Point endPosition = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  416. timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
  417. begX = begPosition.x;
  418. begY = begPosition.y;
  419. progress = 0;
  420. distanceX = endPosition.x - begPosition.x;
  421. distanceY = endPosition.y - begPosition.y;
  422. if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
  423. {
  424. float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
  425. timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
  426. }
  427. return true;
  428. }
  429. void CMovementAnimation::nextFrame()
  430. {
  431. progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
  432. //moving instructions
  433. myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
  434. myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
  435. CBattleAnimation::nextFrame();
  436. if(progress >= 1.0)
  437. {
  438. // Sets the position of the creature animation sprites
  439. Point coords = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  440. myAnim->pos = coords;
  441. // true if creature haven't reached the final destination hex
  442. if ((curentMoveIndex + 1) < destTiles.size())
  443. {
  444. // update the next hex field which has to be reached by the stack
  445. curentMoveIndex++;
  446. oldPos = nextHex;
  447. nextHex = destTiles[curentMoveIndex];
  448. // request re-initialization
  449. initialized = false;
  450. }
  451. else
  452. delete this;
  453. }
  454. }
  455. CMovementAnimation::~CMovementAnimation()
  456. {
  457. assert(stack);
  458. myAnim->pos = owner->stacksController->getStackPositionAtHex(nextHex, stack);
  459. owner->stacksController->addNewAnim(new CMovementEndAnimation(owner, stack, nextHex));
  460. if(owner->moveSoundHander != -1)
  461. {
  462. CCS->soundh->stopSound(owner->moveSoundHander);
  463. owner->moveSoundHander = -1;
  464. }
  465. }
  466. CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
  467. : CBattleStackAnimation(_owner, _stack),
  468. destTiles(_destTiles),
  469. curentMoveIndex(0),
  470. oldPos(stack->getPosition()),
  471. begX(0), begY(0),
  472. distanceX(0), distanceY(0),
  473. timeToMove(0.0),
  474. progress(0.0),
  475. nextHex(destTiles.front())
  476. {
  477. logAnim->debug("Created movement anim for %s", stack->getName());
  478. }
  479. CMovementEndAnimation::CMovementEndAnimation(CBattleInterface * _owner, const CStack * _stack, BattleHex destTile)
  480. : CBattleStackAnimation(_owner, _stack), destinationTile(destTile)
  481. {
  482. logAnim->debug("Created movement end anim for %s", stack->getName());
  483. }
  484. bool CMovementEndAnimation::init()
  485. {
  486. //if( !isEarliest(true) )
  487. // return false;
  488. if(!stack || myAnim->framesInGroup(CCreatureAnim::MOVE_END) == 0 ||
  489. myAnim->isDead())
  490. {
  491. delete this;
  492. return false;
  493. }
  494. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  495. myAnim->setType(CCreatureAnim::MOVE_END);
  496. myAnim->onAnimationReset += [&](){ delete this; };
  497. return true;
  498. }
  499. CMovementEndAnimation::~CMovementEndAnimation()
  500. {
  501. if(myAnim->getType() != CCreatureAnim::DEAD)
  502. myAnim->setType(CCreatureAnim::HOLDING); //resetting to default
  503. CCS->curh->show();
  504. }
  505. CMovementStartAnimation::CMovementStartAnimation(CBattleInterface * _owner, const CStack * _stack)
  506. : CBattleStackAnimation(_owner, _stack)
  507. {
  508. logAnim->debug("Created movement start anim for %s", stack->getName());
  509. }
  510. bool CMovementStartAnimation::init()
  511. {
  512. if( !CBattleAnimation::checkInitialConditions() )
  513. return false;
  514. if(!stack || myAnim->isDead())
  515. {
  516. delete this;
  517. return false;
  518. }
  519. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  520. myAnim->setType(CCreatureAnim::MOVE_START);
  521. myAnim->onAnimationReset += [&](){ delete this; };
  522. return true;
  523. }
  524. CReverseAnimation::CReverseAnimation(CBattleInterface * _owner, const CStack * stack, BattleHex dest, bool _priority)
  525. : CBattleStackAnimation(_owner, stack),
  526. hex(dest),
  527. priority(_priority)
  528. {
  529. logAnim->debug("Created reverse anim for %s", stack->getName());
  530. }
  531. bool CReverseAnimation::init()
  532. {
  533. if(myAnim == nullptr || myAnim->isDead())
  534. {
  535. delete this;
  536. return false; //there is no such creature
  537. }
  538. if(!priority && !CBattleAnimation::checkInitialConditions())
  539. return false;
  540. if(myAnim->framesInGroup(CCreatureAnim::TURN_L))
  541. {
  542. myAnim->setType(CCreatureAnim::TURN_L);
  543. myAnim->onAnimationReset += std::bind(&CReverseAnimation::setupSecondPart, this);
  544. }
  545. else
  546. {
  547. setupSecondPart();
  548. }
  549. return true;
  550. }
  551. CReverseAnimation::~CReverseAnimation()
  552. {
  553. if( stack && stack->alive() )//don't do that if stack is dead
  554. myAnim->setType(CCreatureAnim::HOLDING);
  555. }
  556. void CBattleStackAnimation::rotateStack(BattleHex hex)
  557. {
  558. setStackFacingRight(stack, !stackFacingRight(stack));
  559. stackAnimation(stack)->pos = owner->stacksController->getStackPositionAtHex(hex, stack);
  560. }
  561. void CReverseAnimation::setupSecondPart()
  562. {
  563. if(!stack)
  564. {
  565. delete this;
  566. return;
  567. }
  568. rotateStack(hex);
  569. if(myAnim->framesInGroup(CCreatureAnim::TURN_R))
  570. {
  571. myAnim->setType(CCreatureAnim::TURN_R);
  572. myAnim->onAnimationReset += [&](){ delete this; };
  573. }
  574. else
  575. delete this;
  576. }
  577. CRangedAttackAnimation::CRangedAttackAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  578. : CAttackAnimation(owner_, attacker, dest_, defender)
  579. {
  580. }
  581. CShootingAnimation::CShootingAnimation(CBattleInterface * _owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  582. : CRangedAttackAnimation(_owner, attacker, _dest, _attacked),
  583. catapultDamage(_catapultDmg),
  584. projectileEmitted(false),
  585. explosionEmitted(false)
  586. {
  587. logAnim->debug("Created shooting anim for %s", stack->getName());
  588. }
  589. bool CShootingAnimation::init()
  590. {
  591. if( !CAttackAnimation::checkInitialConditions() )
  592. return false;
  593. assert(attackingStack);
  594. assert(!myAnim->isDead());
  595. if(!attackingStack || myAnim->isDead())
  596. {
  597. //FIXME: how is this possible?
  598. logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
  599. delete this;
  600. return false;
  601. }
  602. //reverse unit if necessary
  603. if (attackingStack && attackedStack && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  604. {
  605. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  606. return false;
  607. }
  608. setAnimationGroup();
  609. initializeProjectile();
  610. shooting = true;
  611. return true;
  612. }
  613. void CShootingAnimation::setAnimationGroup()
  614. {
  615. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  616. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  617. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  618. static const double straightAngle = 0.2;
  619. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  620. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  621. if (projectileAngle > straightAngle)
  622. group = CCreatureAnim::SHOOT_UP;
  623. else if (projectileAngle < -straightAngle)
  624. group = CCreatureAnim::SHOOT_DOWN;
  625. else
  626. group = CCreatureAnim::SHOOT_FRONT;
  627. }
  628. void CShootingAnimation::initializeProjectile()
  629. {
  630. const CCreature *shooterInfo = getCreature();
  631. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225);
  632. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  633. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  634. if (group == CCreatureAnim::SHOOT_UP)
  635. {
  636. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  637. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  638. }
  639. else if (group == CCreatureAnim::SHOOT_DOWN)
  640. {
  641. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  642. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  643. }
  644. else if (group == CCreatureAnim::SHOOT_FRONT)
  645. {
  646. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  647. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  648. }
  649. else
  650. {
  651. assert(0);
  652. }
  653. owner->projectilesController->createProjectile(attackingStack, attackedStack, shotOrigin, shotTarget);
  654. }
  655. void CShootingAnimation::emitProjectile()
  656. {
  657. owner->projectilesController->emitStackProjectile(attackingStack);
  658. projectileEmitted = true;
  659. }
  660. void CShootingAnimation::nextFrame()
  661. {
  662. for(auto & it : pendingAnimations())
  663. {
  664. CMovementStartAnimation * anim = dynamic_cast<CMovementStartAnimation *>(it);
  665. CReverseAnimation * anim2 = dynamic_cast<CReverseAnimation *>(it);
  666. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  667. return;
  668. }
  669. // animation should be paused if there is an active projectile
  670. if (projectileEmitted)
  671. {
  672. if (owner->projectilesController->hasActiveProjectile(attackingStack))
  673. {
  674. stackAnimation(attackingStack)->pause();
  675. return;
  676. }
  677. else
  678. {
  679. stackAnimation(attackingStack)->play();
  680. emitExplosion();
  681. }
  682. }
  683. CAttackAnimation::nextFrame();
  684. if (!projectileEmitted)
  685. {
  686. const CCreature *shooterInfo = getCreature();
  687. assert(stackAnimation(attackingStack)->isShooting());
  688. // emit projectile once animation playback reached "climax" frame
  689. if ( stackAnimation(attackingStack)->getCurrentFrame() >= shooterInfo->animation.attackClimaxFrame )
  690. {
  691. emitProjectile();
  692. return;
  693. }
  694. }
  695. }
  696. void CShootingAnimation::emitExplosion()
  697. {
  698. if (attackedStack)
  699. return;
  700. if (explosionEmitted)
  701. return;
  702. explosionEmitted = true;
  703. Point shotTarget = owner->stacksController->getStackPositionAtHex(dest, attackedStack) + Point(225, 225) - Point(126, 105);
  704. owner->stacksController->addNewAnim( new CEffectAnimation(owner, catapultDamage ? "SGEXPL.DEF" : "CSGRCK.DEF", shotTarget.x, shotTarget.y));
  705. if(catapultDamage > 0)
  706. {
  707. CCS->soundh->playSound("WALLHIT");
  708. }
  709. else
  710. {
  711. CCS->soundh->playSound("WALLMISS");
  712. }
  713. }
  714. CShootingAnimation::~CShootingAnimation()
  715. {
  716. assert(!owner->projectilesController->hasActiveProjectile(attackingStack));
  717. assert(projectileEmitted);
  718. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  719. if (!projectileEmitted)
  720. {
  721. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  722. emitProjectile();
  723. }
  724. }
  725. CCastAnimation::CCastAnimation(CBattleInterface * owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  726. : CRangedAttackAnimation(owner_, attacker, dest_, defender)
  727. {
  728. if(!dest_.isValid() && defender)
  729. dest = defender->getPosition();
  730. }
  731. bool CCastAnimation::init()
  732. {
  733. if(!CAttackAnimation::checkInitialConditions())
  734. return false;
  735. if(!attackingStack || myAnim->isDead())
  736. {
  737. delete this;
  738. return false;
  739. }
  740. //reverse unit if necessary
  741. if(attackedStack)
  742. {
  743. if(owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), attackedStack->getPosition(), stackFacingRight(attackingStack), attackingStack->doubleWide(), stackFacingRight(attackedStack)))
  744. {
  745. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  746. return false;
  747. }
  748. }
  749. else
  750. {
  751. if(dest.isValid() && owner->getCurrentPlayerInterface()->cb->isToReverse(attackingStack->getPosition(), dest, stackFacingRight(attackingStack), false, false))
  752. {
  753. owner->stacksController->addNewAnim(new CReverseAnimation(owner, attackingStack, attackingStack->getPosition(), true));
  754. return false;
  755. }
  756. }
  757. //TODO: display spell projectile here
  758. static const double straightAngle = 0.2;
  759. Point fromPos;
  760. Point destPos;
  761. // NOTE: two lines below return different positions (very notable with 2-hex creatures). Obtaining via creanims seems to be more precise
  762. fromPos = stackAnimation(attackingStack)->pos.topLeft();
  763. //xycoord = owner->stacksController->getStackPositionAtHex(shooter->getPosition(), shooter);
  764. destPos = owner->stacksController->getStackPositionAtHex(dest, attackedStack);
  765. double projectileAngle = atan2(fabs((double)destPos.y - fromPos.y), fabs((double)destPos.x - fromPos.x));
  766. if(attackingStack->getPosition() < dest)
  767. projectileAngle = -projectileAngle;
  768. if(projectileAngle > straightAngle)
  769. group = CCreatureAnim::VCMI_CAST_UP;
  770. else if(projectileAngle < -straightAngle)
  771. group = CCreatureAnim::VCMI_CAST_DOWN;
  772. else
  773. group = CCreatureAnim::VCMI_CAST_FRONT;
  774. //fall back to H3 cast/2hex
  775. //even if creature have 2hex attack instead of cast it is ok since we fall back to attack anyway
  776. if(myAnim->framesInGroup(group) == 0)
  777. {
  778. if(projectileAngle > straightAngle)
  779. group = CCreatureAnim::CAST_UP;
  780. else if(projectileAngle < -straightAngle)
  781. group = CCreatureAnim::CAST_DOWN;
  782. else
  783. group = CCreatureAnim::CAST_FRONT;
  784. }
  785. //fall back to ranged attack
  786. if(myAnim->framesInGroup(group) == 0)
  787. {
  788. if(projectileAngle > straightAngle)
  789. group = CCreatureAnim::SHOOT_UP;
  790. else if(projectileAngle < -straightAngle)
  791. group = CCreatureAnim::SHOOT_DOWN;
  792. else
  793. group = CCreatureAnim::SHOOT_FRONT;
  794. }
  795. //fall back to normal attack
  796. if(myAnim->framesInGroup(group) == 0)
  797. {
  798. if(projectileAngle > straightAngle)
  799. group = CCreatureAnim::ATTACK_UP;
  800. else if(projectileAngle < -straightAngle)
  801. group = CCreatureAnim::ATTACK_DOWN;
  802. else
  803. group = CCreatureAnim::ATTACK_FRONT;
  804. }
  805. return true;
  806. }
  807. void CCastAnimation::nextFrame()
  808. {
  809. for(auto & it : pendingAnimations())
  810. {
  811. CReverseAnimation * anim = dynamic_cast<CReverseAnimation *>(it);
  812. if(anim && anim->stack->ID == stack->ID && anim->priority)
  813. return;
  814. }
  815. if(myAnim->getType() != group)
  816. {
  817. myAnim->setType(group);
  818. myAnim->onAnimationReset += [&](){ delete this; };
  819. }
  820. CBattleAnimation::nextFrame();
  821. }
  822. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip, bool _alignToBottom)
  823. : CBattleAnimation(_owner),
  824. destTile(BattleHex::INVALID),
  825. x(_x),
  826. y(_y),
  827. dx(_dx),
  828. dy(_dy),
  829. Vflip(_Vflip),
  830. alignToBottom(_alignToBottom)
  831. {
  832. logAnim->debug("Created effect animation %s", _customAnim);
  833. customAnim = std::make_shared<CAnimation>(_customAnim);
  834. }
  835. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::shared_ptr<CAnimation> _customAnim, int _x, int _y, int _dx, int _dy)
  836. : CBattleAnimation(_owner),
  837. destTile(BattleHex::INVALID),
  838. customAnim(_customAnim),
  839. x(_x),
  840. y(_y),
  841. dx(_dx),
  842. dy(_dy),
  843. Vflip(false),
  844. alignToBottom(false)
  845. {
  846. logAnim->debug("Created custom effect animation");
  847. }
  848. CEffectAnimation::CEffectAnimation(CBattleInterface * _owner, std::string _customAnim, BattleHex _destTile, bool _Vflip, bool _alignToBottom)
  849. : CBattleAnimation(_owner),
  850. destTile(_destTile),
  851. x(-1),
  852. y(-1),
  853. dx(0),
  854. dy(0),
  855. Vflip(_Vflip),
  856. alignToBottom(_alignToBottom)
  857. {
  858. logAnim->debug("Created effect animation %s", _customAnim);
  859. customAnim = std::make_shared<CAnimation>(_customAnim);
  860. }
  861. bool CEffectAnimation::init()
  862. {
  863. if(!CBattleAnimation::checkInitialConditions())
  864. return false;
  865. const bool areaEffect = (!destTile.isValid() && x == -1 && y == -1);
  866. std::shared_ptr<CAnimation> animation = customAnim;
  867. animation->preload();
  868. if(Vflip)
  869. animation->verticalFlip();
  870. auto first = animation->getImage(0, 0, true);
  871. if(!first)
  872. {
  873. delete this;
  874. return false;
  875. }
  876. if(areaEffect) //f.e. armageddon
  877. {
  878. for(int i=0; i * first->width() < owner->pos.w ; ++i)
  879. {
  880. for(int j=0; j * first->height() < owner->pos.h ; ++j)
  881. {
  882. BattleEffect be;
  883. be.effectID = ID;
  884. be.animation = animation;
  885. be.currentFrame = 0;
  886. be.x = i * first->width() + owner->pos.x;
  887. be.y = j * first->height() + owner->pos.y;
  888. be.position = BattleHex::INVALID;
  889. owner->effectsController->battleEffects.push_back(be);
  890. }
  891. }
  892. }
  893. else // Effects targeted at a specific creature/hex.
  894. {
  895. const CStack * destStack = owner->getCurrentPlayerInterface()->cb->battleGetStackByPos(destTile, false);
  896. BattleEffect be;
  897. be.effectID = ID;
  898. be.animation = animation;
  899. be.currentFrame = 0;
  900. //todo: lightning anim frame count override
  901. // if(effect == 1)
  902. // be.maxFrame = 3;
  903. be.x = x;
  904. be.y = y;
  905. if(destTile.isValid())
  906. {
  907. Rect tilePos = owner->fieldController->hexPosition(destTile);
  908. if(x == -1)
  909. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  910. if(y == -1)
  911. {
  912. if(alignToBottom)
  913. be.y = tilePos.y + tilePos.h - first->height();
  914. else
  915. be.y = tilePos.y - first->height()/2;
  916. }
  917. // Correction for 2-hex creatures.
  918. if(destStack != nullptr && destStack->doubleWide())
  919. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  920. }
  921. assert(be.x != -1 && be.y != -1);
  922. //Indicate if effect should be drawn on top of everything or just on top of the hex
  923. be.position = destTile;
  924. owner->effectsController->battleEffects.push_back(be);
  925. }
  926. return true;
  927. }
  928. void CEffectAnimation::nextFrame()
  929. {
  930. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  931. for(auto & elem : owner->effectsController->battleEffects)
  932. {
  933. if(elem.effectID == ID)
  934. {
  935. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  936. if(elem.currentFrame >= elem.animation->size())
  937. {
  938. delete this;
  939. return;
  940. }
  941. else
  942. {
  943. elem.x += dx;
  944. elem.y += dy;
  945. }
  946. }
  947. }
  948. }
  949. CEffectAnimation::~CEffectAnimation()
  950. {
  951. auto & effects = owner->effectsController->battleEffects;
  952. for ( auto it = effects.begin(); it != effects.end(); )
  953. {
  954. if (it->effectID == ID)
  955. it = effects.erase(it);
  956. else
  957. it++;
  958. }
  959. }