BattleAnimationClasses.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * BattleAnimationClasses.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 "BattleAnimationClasses.h"
  12. #include "BattleInterface.h"
  13. #include "BattleProjectileController.h"
  14. #include "BattleSiegeController.h"
  15. #include "BattleFieldController.h"
  16. #include "BattleEffectsController.h"
  17. #include "BattleStacksController.h"
  18. #include "CreatureAnimation.h"
  19. #include "../CGameInfo.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../gui/CCursorHandler.h"
  23. #include "../gui/CGuiHandler.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CStack.h"
  26. BattleAnimation::BattleAnimation(BattleInterface & owner)
  27. : owner(owner),
  28. ID(owner.stacksController->animIDhelper++),
  29. initialized(false)
  30. {
  31. logAnim->trace("Animation #%d created", ID);
  32. }
  33. bool BattleAnimation::tryInitialize()
  34. {
  35. assert(!initialized);
  36. if ( init() )
  37. {
  38. initialized = true;
  39. return true;
  40. }
  41. return false;
  42. }
  43. bool BattleAnimation::isInitialized()
  44. {
  45. return initialized;
  46. }
  47. BattleAnimation::~BattleAnimation()
  48. {
  49. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  50. for(auto & elem : pendingAnimations())
  51. {
  52. if(elem == this)
  53. elem = nullptr;
  54. }
  55. logAnim->trace("Animation #%d deleted", ID);
  56. }
  57. std::vector<BattleAnimation *> & BattleAnimation::pendingAnimations()
  58. {
  59. return owner.stacksController->currentAnimations;
  60. }
  61. std::shared_ptr<CreatureAnimation> BattleAnimation::stackAnimation(const CStack * stack) const
  62. {
  63. return owner.stacksController->stackAnimation[stack->ID];
  64. }
  65. bool BattleAnimation::stackFacingRight(const CStack * stack)
  66. {
  67. return owner.stacksController->stackFacingRight[stack->ID];
  68. }
  69. void BattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  70. {
  71. owner.stacksController->stackFacingRight[stack->ID] = facingRight;
  72. }
  73. BattleStackAnimation::BattleStackAnimation(BattleInterface & owner, const CStack * stack)
  74. : BattleAnimation(owner),
  75. myAnim(stackAnimation(stack)),
  76. stack(stack)
  77. {
  78. assert(myAnim);
  79. }
  80. void AttackAnimation::nextFrame()
  81. {
  82. assert(myAnim->getType() == group);
  83. if(myAnim->getType() != group)
  84. {
  85. myAnim->setType(group);
  86. myAnim->onAnimationReset += [&](){ delete this; };
  87. }
  88. if(!soundPlayed)
  89. {
  90. playSound();
  91. soundPlayed = true;
  92. }
  93. }
  94. AttackAnimation::~AttackAnimation()
  95. {
  96. myAnim->setType(ECreatureAnimType::HOLDING);
  97. }
  98. const CCreature * AttackAnimation::getCreature() const
  99. {
  100. if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
  101. return owner.siegeController->getTurretCreature();
  102. else
  103. return attackingStack->getCreature();
  104. }
  105. AttackAnimation::AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  106. : BattleStackAnimation(owner, attacker),
  107. group(ECreatureAnimType::SHOOT_FRONT),
  108. soundPlayed(false),
  109. dest(_dest),
  110. defendingStack(defender),
  111. attackingStack(attacker)
  112. {
  113. assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
  114. attackingStackPosBeforeReturn = attackingStack->getPosition();
  115. }
  116. bool HittedAnimation::init()
  117. {
  118. CCS->soundh->playSound(battle_sound(stack->getCreature(), wince));
  119. myAnim->playOnce(ECreatureAnimType::HITTED);
  120. myAnim->onAnimationReset += [&](){ delete this; };
  121. return true;
  122. }
  123. HittedAnimation::HittedAnimation(BattleInterface & owner, const CStack * stack)
  124. : BattleStackAnimation(owner, stack)
  125. {
  126. }
  127. DefenceAnimation::DefenceAnimation(BattleInterface & owner, const CStack * stack)
  128. : BattleStackAnimation(owner, stack)
  129. {
  130. }
  131. bool DefenceAnimation::init()
  132. {
  133. CCS->soundh->playSound(battle_sound(stack->getCreature(), defend));
  134. myAnim->playOnce(ECreatureAnimType::DEFENCE);
  135. myAnim->onAnimationReset += [&](){ delete this; };
  136. return true; //initialized successfuly
  137. }
  138. ECreatureAnimType::Type DeathAnimation::getMyAnimType()
  139. {
  140. if(rangedAttack && myAnim->framesInGroup(ECreatureAnimType::DEATH_RANGED) > 0)
  141. return ECreatureAnimType::DEATH_RANGED;
  142. else
  143. return ECreatureAnimType::DEATH;
  144. }
  145. bool DeathAnimation::init()
  146. {
  147. CCS->soundh->playSound(battle_sound(stack->getCreature(), killed));
  148. myAnim->playOnce(getMyAnimType());
  149. myAnim->onAnimationReset += [&](){ delete this; };
  150. return true;
  151. }
  152. DeathAnimation::DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged):
  153. BattleStackAnimation(owner, stack),
  154. rangedAttack(ranged)
  155. {
  156. }
  157. DeathAnimation::~DeathAnimation()
  158. {
  159. if(rangedAttack && myAnim->framesInGroup(ECreatureAnimType::DEAD_RANGED) > 0)
  160. myAnim->setType(ECreatureAnimType::DEAD_RANGED);
  161. else
  162. myAnim->setType(ECreatureAnimType::DEAD);
  163. }
  164. DummyAnimation::DummyAnimation(BattleInterface & owner, int howManyFrames)
  165. : BattleAnimation(owner),
  166. counter(0),
  167. howMany(howManyFrames)
  168. {
  169. logAnim->debug("Created dummy animation for %d frames", howManyFrames);
  170. }
  171. bool DummyAnimation::init()
  172. {
  173. return true;
  174. }
  175. void DummyAnimation::nextFrame()
  176. {
  177. counter++;
  178. if(counter > howMany)
  179. delete this;
  180. }
  181. bool MeleeAttackAnimation::init()
  182. {
  183. assert(attackingStack);
  184. assert(!myAnim->isDeadOrDying());
  185. if(!attackingStack || myAnim->isDeadOrDying())
  186. {
  187. delete this;
  188. return false;
  189. }
  190. logAnim->info("CMeleeAttackAnimation::init: stack %s -> stack %s", stack->getName(), defendingStack->getName());
  191. static const ECreatureAnimType::Type mutPosToGroup[] =
  192. {
  193. ECreatureAnimType::ATTACK_UP,
  194. ECreatureAnimType::ATTACK_UP,
  195. ECreatureAnimType::ATTACK_FRONT,
  196. ECreatureAnimType::ATTACK_DOWN,
  197. ECreatureAnimType::ATTACK_DOWN,
  198. ECreatureAnimType::ATTACK_FRONT
  199. };
  200. static const ECreatureAnimType::Type mutPosToGroup2H[] =
  201. {
  202. ECreatureAnimType::VCMI_2HEX_UP,
  203. ECreatureAnimType::VCMI_2HEX_UP,
  204. ECreatureAnimType::VCMI_2HEX_FRONT,
  205. ECreatureAnimType::VCMI_2HEX_DOWN,
  206. ECreatureAnimType::VCMI_2HEX_DOWN,
  207. ECreatureAnimType::VCMI_2HEX_FRONT
  208. };
  209. int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
  210. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  211. if(mutPos == -1 && attackingStack->doubleWide())
  212. {
  213. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->getPosition());
  214. }
  215. if (mutPos == -1 && defendingStack->doubleWide())
  216. {
  217. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, defendingStack->occupiedHex());
  218. }
  219. if (mutPos == -1 && defendingStack->doubleWide() && attackingStack->doubleWide())
  220. {
  221. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->occupiedHex());
  222. }
  223. switch(mutPos) //attack direction
  224. {
  225. case 0:
  226. case 1:
  227. case 2:
  228. case 3:
  229. case 4:
  230. case 5:
  231. group = mutPosToGroup[mutPos];
  232. if(attackingStack->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
  233. {
  234. ECreatureAnimType::Type group2H = mutPosToGroup2H[mutPos];
  235. if(myAnim->framesInGroup(group2H)>0)
  236. group = group2H;
  237. }
  238. break;
  239. default:
  240. logGlobal->error("Critical Error! Wrong dest in stackAttacking! dest: %d; attacking stack pos: %d; mutual pos: %d", dest.hex, attackingStackPosBeforeReturn, mutPos);
  241. group = ECreatureAnimType::ATTACK_FRONT;
  242. break;
  243. }
  244. return true;
  245. }
  246. void MeleeAttackAnimation::nextFrame()
  247. {
  248. size_t currentFrame = stackAnimation(attackingStack)->getCurrentFrame();
  249. size_t totalFrames = stackAnimation(attackingStack)->framesInGroup(group);
  250. if ( currentFrame * 2 >= totalFrames )
  251. {
  252. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  253. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  254. }
  255. AttackAnimation::nextFrame();
  256. }
  257. void MeleeAttackAnimation::playSound()
  258. {
  259. CCS->soundh->playSound(battle_sound(getCreature(), attack));
  260. }
  261. MeleeAttackAnimation::MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  262. : AttackAnimation(owner, attacker, _dest, _attacked)
  263. {
  264. logAnim->debug("Created melee attack anim for %s", attacker->getName());
  265. }
  266. StackMoveAnimation::StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex _currentHex):
  267. BattleStackAnimation(owner, _stack),
  268. currentHex(_currentHex)
  269. {
  270. }
  271. bool MovementAnimation::init()
  272. {
  273. assert(stack);
  274. assert(!myAnim->isDeadOrDying());
  275. if(!stack || myAnim->isDeadOrDying())
  276. {
  277. delete this;
  278. return false;
  279. }
  280. if(stackAnimation(stack)->framesInGroup(ECreatureAnimType::MOVING) == 0 ||
  281. stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
  282. {
  283. //no movement or teleport, end immediately
  284. delete this;
  285. return false;
  286. }
  287. logAnim->info("CMovementAnimation::init: stack %s moves %d -> %d", stack->getName(), oldPos, currentHex);
  288. //reverse unit if necessary
  289. if(owner.stacksController->shouldRotate(stack, oldPos, currentHex))
  290. {
  291. // it seems that H3 does NOT plays full rotation animation during movement
  292. // Logical since it takes quite a lot of time
  293. rotateStack(oldPos);
  294. }
  295. if(myAnim->getType() != ECreatureAnimType::MOVING)
  296. {
  297. myAnim->setType(ECreatureAnimType::MOVING);
  298. }
  299. if (owner.moveSoundHander == -1)
  300. {
  301. owner.moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
  302. }
  303. Point begPosition = owner.stacksController->getStackPositionAtHex(oldPos, stack);
  304. Point endPosition = owner.stacksController->getStackPositionAtHex(currentHex, stack);
  305. timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
  306. begX = begPosition.x;
  307. begY = begPosition.y;
  308. progress = 0;
  309. distanceX = endPosition.x - begPosition.x;
  310. distanceY = endPosition.y - begPosition.y;
  311. if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
  312. {
  313. float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
  314. timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
  315. }
  316. return true;
  317. }
  318. void MovementAnimation::nextFrame()
  319. {
  320. progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
  321. //moving instructions
  322. myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
  323. myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
  324. BattleAnimation::nextFrame();
  325. if(progress >= 1.0)
  326. {
  327. // Sets the position of the creature animation sprites
  328. Point coords = owner.stacksController->getStackPositionAtHex(currentHex, stack);
  329. myAnim->pos = coords;
  330. // true if creature haven't reached the final destination hex
  331. if ((curentMoveIndex + 1) < destTiles.size())
  332. {
  333. // update the next hex field which has to be reached by the stack
  334. curentMoveIndex++;
  335. oldPos = currentHex;
  336. currentHex = destTiles[curentMoveIndex];
  337. // request re-initialization
  338. initialized = false;
  339. }
  340. else
  341. delete this;
  342. }
  343. }
  344. MovementAnimation::~MovementAnimation()
  345. {
  346. assert(stack);
  347. myAnim->pos = owner.stacksController->getStackPositionAtHex(currentHex, stack);
  348. if(owner.moveSoundHander != -1)
  349. {
  350. CCS->soundh->stopSound(owner.moveSoundHander);
  351. owner.moveSoundHander = -1;
  352. }
  353. }
  354. MovementAnimation::MovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
  355. : StackMoveAnimation(owner, _stack, _destTiles.front()),
  356. destTiles(_destTiles),
  357. curentMoveIndex(0),
  358. oldPos(stack->getPosition()),
  359. begX(0), begY(0),
  360. distanceX(0), distanceY(0),
  361. timeToMove(0.0),
  362. progress(0.0)
  363. {
  364. logAnim->debug("Created movement anim for %s", stack->getName());
  365. }
  366. MovementEndAnimation::MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile)
  367. : StackMoveAnimation(owner, _stack, destTile)
  368. {
  369. logAnim->debug("Created movement end anim for %s", stack->getName());
  370. }
  371. bool MovementEndAnimation::init()
  372. {
  373. assert(stack);
  374. assert(!myAnim->isDeadOrDying());
  375. if(!stack || myAnim->isDeadOrDying())
  376. {
  377. delete this;
  378. return false;
  379. }
  380. logAnim->info("CMovementEndAnimation::init: stack %s", stack->getName());
  381. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  382. if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_END))
  383. {
  384. delete this;
  385. return false;
  386. }
  387. myAnim->setType(ECreatureAnimType::MOVE_END);
  388. myAnim->onAnimationReset += [&](){ delete this; };
  389. return true;
  390. }
  391. MovementEndAnimation::~MovementEndAnimation()
  392. {
  393. if(myAnim->getType() != ECreatureAnimType::DEAD)
  394. myAnim->setType(ECreatureAnimType::HOLDING); //resetting to default
  395. CCS->curh->show();
  396. }
  397. MovementStartAnimation::MovementStartAnimation(BattleInterface & owner, const CStack * _stack)
  398. : StackMoveAnimation(owner, _stack, _stack->getPosition())
  399. {
  400. logAnim->debug("Created movement start anim for %s", stack->getName());
  401. }
  402. bool MovementStartAnimation::init()
  403. {
  404. assert(stack);
  405. assert(!myAnim->isDeadOrDying());
  406. if(!stack || myAnim->isDeadOrDying())
  407. {
  408. delete this;
  409. return false;
  410. }
  411. logAnim->info("CMovementStartAnimation::init: stack %s", stack->getName());
  412. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  413. if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_START))
  414. {
  415. delete this;
  416. return false;
  417. }
  418. myAnim->setType(ECreatureAnimType::MOVE_START);
  419. myAnim->onAnimationReset += [&](){ delete this; };
  420. return true;
  421. }
  422. ReverseAnimation::ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest)
  423. : StackMoveAnimation(owner, stack, dest)
  424. {
  425. logAnim->debug("Created reverse anim for %s", stack->getName());
  426. }
  427. bool ReverseAnimation::init()
  428. {
  429. assert(myAnim);
  430. assert(!myAnim->isDeadOrDying());
  431. if(myAnim == nullptr || myAnim->isDeadOrDying())
  432. {
  433. delete this;
  434. return false; //there is no such creature
  435. }
  436. logAnim->info("CReverseAnimation::init: stack %s", stack->getName());
  437. if(myAnim->framesInGroup(ECreatureAnimType::TURN_L))
  438. {
  439. myAnim->playOnce(ECreatureAnimType::TURN_L);
  440. myAnim->onAnimationReset += std::bind(&ReverseAnimation::setupSecondPart, this);
  441. }
  442. else
  443. {
  444. setupSecondPart();
  445. }
  446. return true;
  447. }
  448. void BattleStackAnimation::rotateStack(BattleHex hex)
  449. {
  450. setStackFacingRight(stack, !stackFacingRight(stack));
  451. stackAnimation(stack)->pos = owner.stacksController->getStackPositionAtHex(hex, stack);
  452. }
  453. void ReverseAnimation::setupSecondPart()
  454. {
  455. assert(stack);
  456. if(!stack)
  457. {
  458. delete this;
  459. return;
  460. }
  461. rotateStack(currentHex);
  462. if(myAnim->framesInGroup(ECreatureAnimType::TURN_R))
  463. {
  464. myAnim->playOnce(ECreatureAnimType::TURN_R);
  465. myAnim->onAnimationReset += [&](){ delete this; };
  466. }
  467. else
  468. delete this;
  469. }
  470. bool ResurrectionAnimation::init()
  471. {
  472. assert(stack);
  473. if(!stack)
  474. {
  475. delete this;
  476. return false;
  477. }
  478. logAnim->info("CResurrectionAnimation::init: stack %s", stack->getName());
  479. myAnim->playOnce(ECreatureAnimType::RESURRECTION);
  480. myAnim->onAnimationReset += [&](){ delete this; };
  481. return true;
  482. }
  483. ResurrectionAnimation::ResurrectionAnimation(BattleInterface & owner, const CStack * _stack):
  484. BattleStackAnimation(owner, _stack)
  485. {
  486. }
  487. RangedAttackAnimation::RangedAttackAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  488. : AttackAnimation(owner_, attacker, dest_, defender),
  489. projectileEmitted(false)
  490. {
  491. }
  492. void RangedAttackAnimation::playSound()
  493. {
  494. CCS->soundh->playSound(battle_sound(getCreature(), shoot));
  495. }
  496. bool RangedAttackAnimation::init()
  497. {
  498. assert(attackingStack);
  499. assert(!myAnim->isDeadOrDying());
  500. if(!attackingStack || myAnim->isDeadOrDying())
  501. {
  502. //FIXME: how is this possible?
  503. logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
  504. delete this;
  505. return false;
  506. }
  507. logAnim->info("CRangedAttackAnimation::init: stack %s", stack->getName());
  508. setAnimationGroup();
  509. initializeProjectile();
  510. return true;
  511. }
  512. void RangedAttackAnimation::setAnimationGroup()
  513. {
  514. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  515. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack);
  516. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  517. static const double straightAngle = 0.2;
  518. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  519. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  520. if (projectileAngle > straightAngle)
  521. group = getUpwardsGroup();
  522. else if (projectileAngle < -straightAngle)
  523. group = getDownwardsGroup();
  524. else
  525. group = getForwardGroup();
  526. }
  527. void RangedAttackAnimation::initializeProjectile()
  528. {
  529. const CCreature *shooterInfo = getCreature();
  530. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225);
  531. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  532. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  533. if (group == getUpwardsGroup())
  534. {
  535. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  536. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  537. }
  538. else if (group == getDownwardsGroup())
  539. {
  540. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  541. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  542. }
  543. else if (group == getForwardGroup())
  544. {
  545. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  546. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  547. }
  548. else
  549. {
  550. assert(0);
  551. }
  552. createProjectile(shotOrigin, shotTarget);
  553. }
  554. void RangedAttackAnimation::emitProjectile()
  555. {
  556. logAnim->info("Ranged attack projectile emitted");
  557. owner.projectilesController->emitStackProjectile(attackingStack);
  558. projectileEmitted = true;
  559. }
  560. void RangedAttackAnimation::nextFrame()
  561. {
  562. // animation should be paused if there is an active projectile
  563. if (projectileEmitted)
  564. {
  565. if (owner.projectilesController->hasActiveProjectile(attackingStack))
  566. stackAnimation(attackingStack)->pause();
  567. else
  568. {
  569. stackAnimation(attackingStack)->play();
  570. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  571. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  572. }
  573. }
  574. AttackAnimation::nextFrame();
  575. if (!projectileEmitted)
  576. {
  577. // emit projectile once animation playback reached "climax" frame
  578. if ( stackAnimation(attackingStack)->getCurrentFrame() >= getAttackClimaxFrame() )
  579. {
  580. emitProjectile();
  581. stackAnimation(attackingStack)->pause();
  582. return;
  583. }
  584. }
  585. }
  586. RangedAttackAnimation::~RangedAttackAnimation()
  587. {
  588. //FIXME: this assert triggers under some unclear, rare conditions. Possibly - if game window is inactive and/or in foreground/minimized?
  589. assert(!owner.projectilesController->hasActiveProjectile(attackingStack));
  590. assert(projectileEmitted);
  591. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  592. if (!projectileEmitted)
  593. {
  594. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  595. emitProjectile();
  596. }
  597. }
  598. ShootingAnimation::ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  599. : RangedAttackAnimation(owner, attacker, _dest, _attacked)
  600. {
  601. logAnim->debug("Created shooting anim for %s", stack->getName());
  602. }
  603. void ShootingAnimation::createProjectile(const Point & from, const Point & dest) const
  604. {
  605. owner.projectilesController->createProjectile(attackingStack, from, dest);
  606. }
  607. uint32_t ShootingAnimation::getAttackClimaxFrame() const
  608. {
  609. const CCreature *shooterInfo = getCreature();
  610. return shooterInfo->animation.attackClimaxFrame;
  611. }
  612. ECreatureAnimType::Type ShootingAnimation::getUpwardsGroup() const
  613. {
  614. return ECreatureAnimType::SHOOT_UP;
  615. }
  616. ECreatureAnimType::Type ShootingAnimation::getForwardGroup() const
  617. {
  618. return ECreatureAnimType::SHOOT_FRONT;
  619. }
  620. ECreatureAnimType::Type ShootingAnimation::getDownwardsGroup() const
  621. {
  622. return ECreatureAnimType::SHOOT_DOWN;
  623. }
  624. CatapultAnimation::CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, int _catapultDmg)
  625. : ShootingAnimation(owner, attacker, _dest, _attacked),
  626. catapultDamage(_catapultDmg),
  627. explosionEmitted(false)
  628. {
  629. logAnim->debug("Created shooting anim for %s", stack->getName());
  630. }
  631. void CatapultAnimation::nextFrame()
  632. {
  633. ShootingAnimation::nextFrame();
  634. if ( explosionEmitted)
  635. return;
  636. if ( !projectileEmitted)
  637. return;
  638. if (owner.projectilesController->hasActiveProjectile(attackingStack))
  639. return;
  640. explosionEmitted = true;
  641. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225) - Point(126, 105);
  642. if(catapultDamage > 0)
  643. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLHIT", "SGEXPL.DEF", shotTarget));
  644. else
  645. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLMISS", "CSGRCK.DEF", shotTarget));
  646. }
  647. void CatapultAnimation::createProjectile(const Point & from, const Point & dest) const
  648. {
  649. owner.projectilesController->createCatapultProjectile(attackingStack, from, dest);
  650. }
  651. CastAnimation::CastAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell)
  652. : RangedAttackAnimation(owner_, attacker, dest_, defender),
  653. spell(spell)
  654. {
  655. assert(dest.isValid());// FIXME: when?
  656. if(!dest_.isValid() && defender)
  657. dest = defender->getPosition();
  658. }
  659. ECreatureAnimType::Type CastAnimation::findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const
  660. {
  661. for ( auto group : candidates)
  662. {
  663. if(myAnim->framesInGroup(group) > 0)
  664. return group;
  665. }
  666. assert(0);
  667. return ECreatureAnimType::HOLDING;
  668. }
  669. ECreatureAnimType::Type CastAnimation::getUpwardsGroup() const
  670. {
  671. return findValidGroup({
  672. ECreatureAnimType::VCMI_CAST_UP,
  673. ECreatureAnimType::CAST_UP,
  674. ECreatureAnimType::SHOOT_UP,
  675. ECreatureAnimType::ATTACK_UP
  676. });
  677. }
  678. ECreatureAnimType::Type CastAnimation::getForwardGroup() const
  679. {
  680. return findValidGroup({
  681. ECreatureAnimType::VCMI_CAST_FRONT,
  682. ECreatureAnimType::CAST_FRONT,
  683. ECreatureAnimType::SHOOT_FRONT,
  684. ECreatureAnimType::ATTACK_FRONT
  685. });
  686. }
  687. ECreatureAnimType::Type CastAnimation::getDownwardsGroup() const
  688. {
  689. return findValidGroup({
  690. ECreatureAnimType::VCMI_CAST_DOWN,
  691. ECreatureAnimType::CAST_DOWN,
  692. ECreatureAnimType::SHOOT_DOWN,
  693. ECreatureAnimType::ATTACK_DOWN
  694. });
  695. }
  696. void CastAnimation::createProjectile(const Point & from, const Point & dest) const
  697. {
  698. if (!spell->animationInfo.projectile.empty())
  699. owner.projectilesController->createSpellProjectile(attackingStack, from, dest, spell);
  700. }
  701. uint32_t CastAnimation::getAttackClimaxFrame() const
  702. {
  703. //FIXME: allow defining this parameter in config file, separately from attackClimaxFrame of missile attacks
  704. uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(group);
  705. if (maxFrames > 2)
  706. return maxFrames - 2;
  707. return 0;
  708. }
  709. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects):
  710. BattleAnimation(owner),
  711. animation(std::make_shared<CAnimation>(animationName)),
  712. soundName(soundName),
  713. effectFlags(effects),
  714. soundPlayed(false),
  715. soundFinished(false),
  716. effectFinished(false)
  717. {
  718. logAnim->info("CPointEffectAnimation::init: effect %s", animationName);
  719. }
  720. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects):
  721. PointEffectAnimation(owner, soundName, animationName, effects)
  722. {
  723. battlehexes = hex;
  724. }
  725. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex, int effects):
  726. PointEffectAnimation(owner, soundName, animationName, effects)
  727. {
  728. assert(hex.isValid());
  729. battlehexes.push_back(hex);
  730. }
  731. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos, int effects):
  732. PointEffectAnimation(owner, soundName, animationName, effects)
  733. {
  734. positions = pos;
  735. }
  736. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, int effects):
  737. PointEffectAnimation(owner, soundName, animationName, effects)
  738. {
  739. positions.push_back(pos);
  740. }
  741. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects):
  742. PointEffectAnimation(owner, soundName, animationName, effects)
  743. {
  744. assert(hex.isValid());
  745. battlehexes.push_back(hex);
  746. positions.push_back(pos);
  747. }
  748. bool PointEffectAnimation::init()
  749. {
  750. animation->preload();
  751. auto first = animation->getImage(0, 0, true);
  752. if(!first)
  753. {
  754. delete this;
  755. return false;
  756. }
  757. if (screenFill())
  758. {
  759. for(int i=0; i * first->width() < owner.pos.w ; ++i)
  760. for(int j=0; j * first->height() < owner.pos.h ; ++j)
  761. positions.push_back(Point( owner.pos.x + i * first->width(), owner.pos.y + j * first->height()));
  762. }
  763. BattleEffect be;
  764. be.effectID = ID;
  765. be.animation = animation;
  766. be.currentFrame = 0;
  767. for (size_t i = 0; i < std::max(battlehexes.size(), positions.size()); ++i)
  768. {
  769. bool hasTile = i < battlehexes.size();
  770. bool hasPosition = i < positions.size();
  771. if (hasTile && !forceOnTop())
  772. be.position = battlehexes[i];
  773. else
  774. be.position = BattleHex::INVALID;
  775. if (hasPosition)
  776. {
  777. be.x = positions[i].x;
  778. be.y = positions[i].y;
  779. }
  780. else
  781. {
  782. const CStack * destStack = owner.getCurrentPlayerInterface()->cb->battleGetStackByPos(battlehexes[i], false);
  783. Rect tilePos = owner.fieldController->hexPositionAbsolute(battlehexes[i]);
  784. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  785. if(destStack && destStack->doubleWide()) // Correction for 2-hex creatures.
  786. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  787. if (alignToBottom())
  788. be.y = tilePos.y + tilePos.h - first->height();
  789. else
  790. be.y = tilePos.y - first->height()/2;
  791. }
  792. owner.effectsController->battleEffects.push_back(be);
  793. }
  794. return true;
  795. }
  796. void PointEffectAnimation::nextFrame()
  797. {
  798. playSound();
  799. playEffect();
  800. if (soundFinished && effectFinished)
  801. {
  802. //remove visual effect itself only if sound has finished as well - necessary for obstacles like force field
  803. clearEffect();
  804. delete this;
  805. }
  806. }
  807. bool PointEffectAnimation::alignToBottom() const
  808. {
  809. return effectFlags & ALIGN_TO_BOTTOM;
  810. }
  811. bool PointEffectAnimation::waitForSound() const
  812. {
  813. return effectFlags & WAIT_FOR_SOUND;
  814. }
  815. bool PointEffectAnimation::forceOnTop() const
  816. {
  817. return effectFlags & FORCE_ON_TOP;
  818. }
  819. bool PointEffectAnimation::screenFill() const
  820. {
  821. return effectFlags & SCREEN_FILL;
  822. }
  823. void PointEffectAnimation::onEffectFinished()
  824. {
  825. effectFinished = true;
  826. }
  827. void PointEffectAnimation::onSoundFinished()
  828. {
  829. soundFinished = true;
  830. }
  831. void PointEffectAnimation::playSound()
  832. {
  833. if (soundPlayed)
  834. return;
  835. soundPlayed = true;
  836. if (soundName.empty())
  837. {
  838. onSoundFinished();
  839. return;
  840. }
  841. int channel = CCS->soundh->playSound(soundName);
  842. if (!waitForSound() || channel == -1)
  843. onSoundFinished();
  844. else
  845. CCS->soundh->setCallback(channel, [&](){ onSoundFinished(); });
  846. }
  847. void PointEffectAnimation::playEffect()
  848. {
  849. if ( effectFinished )
  850. return;
  851. for(auto & elem : owner.effectsController->battleEffects)
  852. {
  853. if(elem.effectID == ID)
  854. {
  855. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  856. if(elem.currentFrame >= elem.animation->size())
  857. {
  858. elem.currentFrame = elem.animation->size() - 1;
  859. onEffectFinished();
  860. break;
  861. }
  862. }
  863. }
  864. }
  865. void PointEffectAnimation::clearEffect()
  866. {
  867. auto & effects = owner.effectsController->battleEffects;
  868. vstd::erase_if(effects, [&](const BattleEffect & effect){
  869. return effect.effectID == ID;
  870. });
  871. }
  872. PointEffectAnimation::~PointEffectAnimation()
  873. {
  874. assert(effectFinished);
  875. assert(soundFinished);
  876. }
  877. void WaitingProjectileAnimation::nextFrame()
  878. {
  879. // initialization conditions fulfilled, delay is over
  880. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  881. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  882. delete this;
  883. }
  884. WaitingProjectileAnimation::WaitingProjectileAnimation(BattleInterface & owner_, const CStack * shooter):
  885. BattleAnimation(owner_),
  886. shooter(shooter)
  887. {}
  888. bool WaitingProjectileAnimation::init()
  889. {
  890. for(auto & elem : pendingAnimations())
  891. {
  892. auto * attackAnim = dynamic_cast<RangedAttackAnimation *>(elem);
  893. if( attackAnim && shooter && attackAnim->stack->ID == shooter->ID && !attackAnim->isInitialized() )
  894. {
  895. // there is ongoing ranged attack that involves our stack, but projectile was not created yet
  896. return false;
  897. }
  898. }
  899. if(owner.projectilesController->hasActiveProjectile(shooter))
  900. return false;
  901. return true;
  902. }