BattleAnimationClasses.cpp 29 KB

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