BattleAnimationClasses.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 "BattleInterfaceClasses.h"
  14. #include "BattleProjectileController.h"
  15. #include "BattleSiegeController.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleEffectsController.h"
  18. #include "BattleStacksController.h"
  19. #include "CreatureAnimation.h"
  20. #include "../CGameInfo.h"
  21. #include "../CMusicHandler.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../gui/CCursorHandler.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/CStack.h"
  27. BattleAnimation::BattleAnimation(BattleInterface & owner)
  28. : owner(owner),
  29. ID(owner.stacksController->animIDhelper++),
  30. initialized(false)
  31. {
  32. logAnim->trace("Animation #%d created", ID);
  33. }
  34. bool BattleAnimation::tryInitialize()
  35. {
  36. assert(!initialized);
  37. if ( init() )
  38. {
  39. initialized = true;
  40. return true;
  41. }
  42. return false;
  43. }
  44. bool BattleAnimation::isInitialized()
  45. {
  46. return initialized;
  47. }
  48. BattleAnimation::~BattleAnimation()
  49. {
  50. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  51. for(auto & elem : pendingAnimations())
  52. {
  53. if(elem == this)
  54. elem = nullptr;
  55. }
  56. logAnim->trace("Animation #%d deleted", ID);
  57. }
  58. std::vector<BattleAnimation *> & BattleAnimation::pendingAnimations()
  59. {
  60. return owner.stacksController->currentAnimations;
  61. }
  62. std::shared_ptr<CreatureAnimation> BattleAnimation::stackAnimation(const CStack * stack) const
  63. {
  64. return owner.stacksController->stackAnimation[stack->ID];
  65. }
  66. bool BattleAnimation::stackFacingRight(const CStack * stack)
  67. {
  68. return owner.stacksController->stackFacingRight[stack->ID];
  69. }
  70. void BattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  71. {
  72. owner.stacksController->stackFacingRight[stack->ID] = facingRight;
  73. }
  74. BattleStackAnimation::BattleStackAnimation(BattleInterface & owner, const CStack * stack)
  75. : BattleAnimation(owner),
  76. myAnim(stackAnimation(stack)),
  77. stack(stack)
  78. {
  79. assert(myAnim);
  80. }
  81. void AttackAnimation::nextFrame()
  82. {
  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. HeroCastAnimation::HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell):
  878. BattleAnimation(owner),
  879. projectileEmitted(false),
  880. hero(hero),
  881. target(defender),
  882. tile(dest),
  883. spell(spell)
  884. {
  885. }
  886. bool HeroCastAnimation::init()
  887. {
  888. hero->setPhase(EHeroAnimType::CAST_SPELL);
  889. hero->onPhaseFinished([&](){
  890. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == true);
  891. delete this;
  892. });
  893. initializeProjectile();
  894. return true;
  895. }
  896. void HeroCastAnimation::initializeProjectile()
  897. {
  898. //spell has no projectile to play, ignore this step
  899. if (spell->animationInfo.projectile.empty())
  900. return;
  901. Point srccoord = hero->pos.center();
  902. Point destcoord = owner.stacksController->getStackPositionAtHex(tile, target); //position attacked by projectile
  903. destcoord += Point(222, 265); // FIXME: what are these constants?
  904. owner.projectilesController->createSpellProjectile( nullptr, srccoord, destcoord, spell);
  905. }
  906. void HeroCastAnimation::emitProjectile()
  907. {
  908. if (projectileEmitted)
  909. return;
  910. //spell has no projectile to play, skip this step and immediately play hit animations
  911. if (spell->animationInfo.projectile.empty())
  912. emitAnimationEvent();
  913. else
  914. owner.projectilesController->emitStackProjectile( nullptr );
  915. projectileEmitted = true;
  916. }
  917. void HeroCastAnimation::emitAnimationEvent()
  918. {
  919. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  920. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  921. }
  922. void HeroCastAnimation::nextFrame()
  923. {
  924. float frame = hero->getFrame();
  925. if (frame < 4.0f)
  926. return;
  927. if (!projectileEmitted)
  928. {
  929. emitProjectile();
  930. hero->pause();
  931. return;
  932. }
  933. if (!owner.projectilesController->hasActiveProjectile(nullptr))
  934. {
  935. emitAnimationEvent();
  936. //FIXME: check H3 - it is possible that hero animation should be paused until hit effect is over, not just projectile
  937. hero->play();
  938. }
  939. }