BattleAnimationClasses.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 "../gui/SDL_Extensions.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CStack.h"
  28. BattleAnimation::BattleAnimation(BattleInterface & owner)
  29. : owner(owner),
  30. ID(owner.stacksController->animIDhelper++),
  31. initialized(false)
  32. {
  33. logAnim->trace("Animation #%d created", ID);
  34. }
  35. bool BattleAnimation::tryInitialize()
  36. {
  37. assert(!initialized);
  38. if ( init() )
  39. {
  40. initialized = true;
  41. return true;
  42. }
  43. return false;
  44. }
  45. bool BattleAnimation::isInitialized()
  46. {
  47. return initialized;
  48. }
  49. BattleAnimation::~BattleAnimation()
  50. {
  51. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  52. for(auto & elem : pendingAnimations())
  53. {
  54. if(elem == this)
  55. elem = nullptr;
  56. }
  57. logAnim->trace("Animation #%d deleted", ID);
  58. }
  59. std::vector<BattleAnimation *> & BattleAnimation::pendingAnimations()
  60. {
  61. return owner.stacksController->currentAnimations;
  62. }
  63. std::shared_ptr<CreatureAnimation> BattleAnimation::stackAnimation(const CStack * stack) const
  64. {
  65. return owner.stacksController->stackAnimation[stack->ID];
  66. }
  67. bool BattleAnimation::stackFacingRight(const CStack * stack)
  68. {
  69. return owner.stacksController->stackFacingRight[stack->ID];
  70. }
  71. void BattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  72. {
  73. owner.stacksController->stackFacingRight[stack->ID] = facingRight;
  74. }
  75. BattleStackAnimation::BattleStackAnimation(BattleInterface & owner, const CStack * stack)
  76. : BattleAnimation(owner),
  77. myAnim(stackAnimation(stack)),
  78. stack(stack)
  79. {
  80. assert(myAnim);
  81. }
  82. StackActionAnimation::StackActionAnimation(BattleInterface & owner, const CStack * stack)
  83. : BattleStackAnimation(owner, stack)
  84. , nextGroup(ECreatureAnimType::HOLDING)
  85. , currGroup(ECreatureAnimType::HOLDING)
  86. {
  87. }
  88. ECreatureAnimType::Type StackActionAnimation::getGroup() const
  89. {
  90. return currGroup;
  91. }
  92. void StackActionAnimation::setNextGroup( ECreatureAnimType::Type group )
  93. {
  94. nextGroup = group;
  95. }
  96. void StackActionAnimation::setGroup( ECreatureAnimType::Type group )
  97. {
  98. currGroup = group;
  99. }
  100. void StackActionAnimation::setSound( std::string sound )
  101. {
  102. this->sound = sound;
  103. }
  104. bool StackActionAnimation::init()
  105. {
  106. if (!sound.empty())
  107. CCS->soundh->playSound(sound);
  108. if (myAnim->framesInGroup(currGroup) > 0)
  109. {
  110. myAnim->playOnce(currGroup);
  111. myAnim->onAnimationReset += [&](){ delete this; };
  112. }
  113. else
  114. delete this;
  115. return true;
  116. }
  117. StackActionAnimation::~StackActionAnimation()
  118. {
  119. if (stack->isFrozen())
  120. myAnim->setType(ECreatureAnimType::HOLDING);
  121. else
  122. myAnim->setType(nextGroup);
  123. }
  124. ECreatureAnimType::Type AttackAnimation::findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const
  125. {
  126. for ( auto group : candidates)
  127. {
  128. if(myAnim->framesInGroup(group) > 0)
  129. return group;
  130. }
  131. assert(0);
  132. return ECreatureAnimType::HOLDING;
  133. }
  134. const CCreature * AttackAnimation::getCreature() const
  135. {
  136. if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
  137. return owner.siegeController->getTurretCreature();
  138. else
  139. return attackingStack->getCreature();
  140. }
  141. AttackAnimation::AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  142. : StackActionAnimation(owner, attacker),
  143. dest(_dest),
  144. defendingStack(defender),
  145. attackingStack(attacker)
  146. {
  147. assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
  148. attackingStackPosBeforeReturn = attackingStack->getPosition();
  149. }
  150. HittedAnimation::HittedAnimation(BattleInterface & owner, const CStack * stack)
  151. : StackActionAnimation(owner, stack)
  152. {
  153. setGroup(ECreatureAnimType::HITTED);
  154. setSound(battle_sound(stack->getCreature(), wince));
  155. logAnim->debug("Created HittedAnimation for %s", stack->getName());
  156. }
  157. DefenceAnimation::DefenceAnimation(BattleInterface & owner, const CStack * stack)
  158. : StackActionAnimation(owner, stack)
  159. {
  160. setGroup(ECreatureAnimType::DEFENCE);
  161. setSound(battle_sound(stack->getCreature(), defend));
  162. logAnim->debug("Created DefenceAnimation for %s", stack->getName());
  163. }
  164. DeathAnimation::DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged):
  165. StackActionAnimation(owner, stack)
  166. {
  167. setSound(battle_sound(stack->getCreature(), killed));
  168. if(ranged && myAnim->framesInGroup(ECreatureAnimType::DEATH_RANGED) > 0)
  169. setGroup(ECreatureAnimType::DEATH_RANGED);
  170. else
  171. setGroup(ECreatureAnimType::DEATH);
  172. if(ranged && myAnim->framesInGroup(ECreatureAnimType::DEAD_RANGED) > 0)
  173. setNextGroup(ECreatureAnimType::DEAD_RANGED);
  174. else
  175. setNextGroup(ECreatureAnimType::DEAD);
  176. logAnim->debug("Created DeathAnimation for %s", stack->getName());
  177. }
  178. DummyAnimation::DummyAnimation(BattleInterface & owner, int howManyFrames)
  179. : BattleAnimation(owner),
  180. counter(0),
  181. howMany(howManyFrames)
  182. {
  183. logAnim->debug("Created dummy animation for %d frames", howManyFrames);
  184. }
  185. bool DummyAnimation::init()
  186. {
  187. return true;
  188. }
  189. void DummyAnimation::nextFrame()
  190. {
  191. counter++;
  192. if(counter > howMany)
  193. delete this;
  194. }
  195. ECreatureAnimType::Type MeleeAttackAnimation::getUpwardsGroup(bool multiAttack) const
  196. {
  197. if (!multiAttack)
  198. return ECreatureAnimType::ATTACK_UP;
  199. return findValidGroup({
  200. ECreatureAnimType::GROUP_ATTACK_UP,
  201. ECreatureAnimType::SPECIAL_UP,
  202. ECreatureAnimType::ATTACK_UP
  203. });
  204. }
  205. ECreatureAnimType::Type MeleeAttackAnimation::getForwardGroup(bool multiAttack) const
  206. {
  207. if (!multiAttack)
  208. return ECreatureAnimType::ATTACK_FRONT;
  209. return findValidGroup({
  210. ECreatureAnimType::GROUP_ATTACK_FRONT,
  211. ECreatureAnimType::SPECIAL_FRONT,
  212. ECreatureAnimType::ATTACK_FRONT
  213. });
  214. }
  215. ECreatureAnimType::Type MeleeAttackAnimation::getDownwardsGroup(bool multiAttack) const
  216. {
  217. if (!multiAttack)
  218. return ECreatureAnimType::ATTACK_DOWN;
  219. return findValidGroup({
  220. ECreatureAnimType::GROUP_ATTACK_DOWN,
  221. ECreatureAnimType::SPECIAL_DOWN,
  222. ECreatureAnimType::ATTACK_DOWN
  223. });
  224. }
  225. ECreatureAnimType::Type MeleeAttackAnimation::selectGroup(bool multiAttack)
  226. {
  227. const ECreatureAnimType::Type mutPosToGroup[] =
  228. {
  229. getUpwardsGroup (multiAttack),
  230. getUpwardsGroup (multiAttack),
  231. getForwardGroup (multiAttack),
  232. getDownwardsGroup(multiAttack),
  233. getDownwardsGroup(multiAttack),
  234. getForwardGroup (multiAttack)
  235. };
  236. int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
  237. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  238. if(mutPos == -1 && attackingStack->doubleWide())
  239. {
  240. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->getPosition());
  241. }
  242. if (mutPos == -1 && defendingStack->doubleWide())
  243. {
  244. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, defendingStack->occupiedHex());
  245. }
  246. if (mutPos == -1 && defendingStack->doubleWide() && attackingStack->doubleWide())
  247. {
  248. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->occupiedHex());
  249. }
  250. assert(mutPos >= 0 && mutPos <=5);
  251. return mutPosToGroup[mutPos];
  252. }
  253. void MeleeAttackAnimation::nextFrame()
  254. {
  255. size_t currentFrame = stackAnimation(attackingStack)->getCurrentFrame();
  256. size_t totalFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
  257. if ( currentFrame * 2 >= totalFrames )
  258. {
  259. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  260. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  261. }
  262. AttackAnimation::nextFrame();
  263. }
  264. MeleeAttackAnimation::MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool multiAttack)
  265. : AttackAnimation(owner, attacker, _dest, _attacked)
  266. {
  267. logAnim->debug("Created MeleeAttackAnimation for %s", attacker->getName());
  268. setSound(battle_sound(getCreature(), attack));
  269. setGroup(selectGroup(multiAttack));
  270. }
  271. StackMoveAnimation::StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex prevHex, BattleHex nextHex):
  272. BattleStackAnimation(owner, _stack),
  273. prevHex(prevHex),
  274. nextHex(nextHex)
  275. {
  276. }
  277. bool MovementAnimation::init()
  278. {
  279. assert(stack);
  280. assert(!myAnim->isDeadOrDying());
  281. if(stackAnimation(stack)->framesInGroup(ECreatureAnimType::MOVING) == 0 ||
  282. stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
  283. {
  284. //no movement or teleport, end immediately
  285. delete this;
  286. return false;
  287. }
  288. logAnim->debug("CMovementAnimation::init: stack %s moves %d -> %d", stack->getName(), prevHex, nextHex);
  289. //reverse unit if necessary
  290. if(owner.stacksController->shouldRotate(stack, prevHex, nextHex))
  291. {
  292. // it seems that H3 does NOT plays full rotation animation during movement
  293. // Logical since it takes quite a lot of time
  294. rotateStack(prevHex);
  295. }
  296. if(myAnim->getType() != ECreatureAnimType::MOVING)
  297. {
  298. myAnim->setType(ECreatureAnimType::MOVING);
  299. }
  300. if (owner.moveSoundHander == -1)
  301. {
  302. owner.moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
  303. }
  304. Point begPosition = owner.stacksController->getStackPositionAtHex(prevHex, stack);
  305. Point endPosition = owner.stacksController->getStackPositionAtHex(nextHex, stack);
  306. timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
  307. begX = begPosition.x;
  308. begY = begPosition.y;
  309. progress = 0;
  310. distanceX = endPosition.x - begPosition.x;
  311. distanceY = endPosition.y - begPosition.y;
  312. if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
  313. {
  314. float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
  315. timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
  316. }
  317. return true;
  318. }
  319. void MovementAnimation::nextFrame()
  320. {
  321. progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
  322. //moving instructions
  323. myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
  324. myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
  325. BattleAnimation::nextFrame();
  326. if(progress >= 1.0)
  327. {
  328. // Sets the position of the creature animation sprites
  329. Point coords = owner.stacksController->getStackPositionAtHex(nextHex, stack);
  330. myAnim->pos.moveTo(coords);
  331. // true if creature haven't reached the final destination hex
  332. if ((curentMoveIndex + 1) < destTiles.size())
  333. {
  334. // update the next hex field which has to be reached by the stack
  335. curentMoveIndex++;
  336. prevHex = nextHex;
  337. nextHex = destTiles[curentMoveIndex];
  338. // request re-initialization
  339. initialized = false;
  340. }
  341. else
  342. delete this;
  343. }
  344. }
  345. MovementAnimation::~MovementAnimation()
  346. {
  347. assert(stack);
  348. myAnim->pos.moveTo(owner.stacksController->getStackPositionAtHex(nextHex, stack));
  349. if(owner.moveSoundHander != -1)
  350. {
  351. CCS->soundh->stopSound(owner.moveSoundHander);
  352. owner.moveSoundHander = -1;
  353. }
  354. }
  355. MovementAnimation::MovementAnimation(BattleInterface & owner, const CStack *stack, std::vector<BattleHex> _destTiles, int _distance)
  356. : StackMoveAnimation(owner, stack, stack->getPosition(), _destTiles.front()),
  357. destTiles(_destTiles),
  358. curentMoveIndex(0),
  359. begX(0), begY(0),
  360. distanceX(0), distanceY(0),
  361. timeToMove(0.0),
  362. progress(0.0)
  363. {
  364. logAnim->debug("Created MovementAnimation for %s", stack->getName());
  365. }
  366. MovementEndAnimation::MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile)
  367. : StackMoveAnimation(owner, _stack, destTile, destTile)
  368. {
  369. logAnim->debug("Created MovementEndAnimation 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->debug("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(), _stack->getPosition())
  399. {
  400. logAnim->debug("Created MovementStartAnimation 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->debug("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, dest)
  424. {
  425. logAnim->debug("Created ReverseAnimation 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->debug("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.moveTo(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(nextHex);
  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. ResurrectionAnimation::ResurrectionAnimation(BattleInterface & owner, const CStack * _stack):
  471. StackActionAnimation(owner, _stack)
  472. {
  473. setGroup(ECreatureAnimType::RESURRECTION);
  474. logAnim->debug("Created ResurrectionAnimation for %s", stack->getName());
  475. }
  476. void ColorTransformAnimation::nextFrame()
  477. {
  478. float elapsed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  479. float fullTime = AnimationControls::getFadeInDuration();
  480. float delta = elapsed / fullTime;
  481. totalProgress += delta;
  482. size_t index = 0;
  483. while (index < timePoints.size() && timePoints[index] < totalProgress )
  484. ++index;
  485. if (index == timePoints.size())
  486. {
  487. //end of animation. Apply ColorShifter using final values and die
  488. const auto & shifter = steps[index - 1];
  489. owner.stacksController->setStackColorFilter(shifter, stack, spell, false);
  490. delete this;
  491. return;
  492. }
  493. assert(index != 0);
  494. const auto & prevShifter = steps[index - 1];
  495. const auto & nextShifter = steps[index];
  496. float prevPoint = timePoints[index-1];
  497. float nextPoint = timePoints[index];
  498. float localProgress = totalProgress - prevPoint;
  499. float stepDuration = (nextPoint - prevPoint);
  500. float factor = localProgress / stepDuration;
  501. auto shifter = ColorFilter::genInterpolated(prevShifter, nextShifter, factor);
  502. owner.stacksController->setStackColorFilter(shifter, stack, spell, true);
  503. }
  504. ColorTransformAnimation::ColorTransformAnimation(BattleInterface & owner, const CStack * _stack, const CSpell * spell):
  505. StackActionAnimation(owner, _stack),
  506. spell(spell),
  507. totalProgress(0.f)
  508. {
  509. logAnim->debug("Created ColorTransformAnimation for %s", stack->getName());
  510. }
  511. ColorTransformAnimation * ColorTransformAnimation::bloodlustAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  512. {
  513. auto result = new ColorTransformAnimation(owner, stack, spell);
  514. result->steps.push_back(ColorFilter::genEmptyShifter());
  515. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.0, 0.3, 0.4}, { 0, 1, 0, 0}, { 0, 0, 1, 0}, 1.f ));
  516. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.3, 0.3, 0.1}, { 0, 0.5, 0, 0}, { 0, 0.5, 0, 0}, 1.f ));
  517. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.0, 0.3, 0.4}, { 0, 1, 0, 0}, { 0, 0, 1, 0}, 1.f ));
  518. result->steps.push_back(ColorFilter::genEmptyShifter());
  519. result->timePoints.push_back(0.0f);
  520. result->timePoints.push_back(0.2f);
  521. result->timePoints.push_back(0.4f);
  522. result->timePoints.push_back(0.6f);
  523. result->timePoints.push_back(0.8f);
  524. return result;
  525. }
  526. ColorTransformAnimation * ColorTransformAnimation::cloneAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  527. {
  528. auto result = new ColorTransformAnimation(owner, stack, spell);
  529. result->steps.push_back(ColorFilter::genRangeShifter( 0, 0, 0.5, 0.5, 0.5, 1.0));
  530. result->timePoints.push_back(0.f);
  531. return result;
  532. }
  533. ColorTransformAnimation * ColorTransformAnimation::petrifyAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  534. {
  535. auto result = new ColorTransformAnimation(owner, stack, spell);
  536. result->steps.push_back(ColorFilter::genEmptyShifter());
  537. result->steps.push_back(ColorFilter::genGrayscaleShifter());
  538. result->timePoints.push_back(0.f);
  539. result->timePoints.push_back(1.f);
  540. return result;
  541. }
  542. ColorTransformAnimation * ColorTransformAnimation::fadeInAnimation(BattleInterface & owner, const CStack * stack)
  543. {
  544. auto result = new ColorTransformAnimation(owner, stack, nullptr);
  545. result->steps.push_back(ColorFilter::genAlphaShifter(0.f));
  546. result->steps.push_back(ColorFilter::genEmptyShifter());
  547. result->timePoints.push_back(0.f);
  548. result->timePoints.push_back(1.f);
  549. return result;
  550. }
  551. ColorTransformAnimation * ColorTransformAnimation::fadeOutAnimation(BattleInterface & owner, const CStack * stack)
  552. {
  553. auto result = fadeInAnimation(owner, stack);
  554. std::swap(result->steps[0], result->steps[1]);
  555. return result;
  556. }
  557. RangedAttackAnimation::RangedAttackAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  558. : AttackAnimation(owner_, attacker, dest_, defender),
  559. projectileEmitted(false)
  560. {
  561. setSound(battle_sound(getCreature(), shoot));
  562. }
  563. bool RangedAttackAnimation::init()
  564. {
  565. setAnimationGroup();
  566. initializeProjectile();
  567. return AttackAnimation::init();
  568. }
  569. void RangedAttackAnimation::setAnimationGroup()
  570. {
  571. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  572. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack);
  573. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  574. static const double straightAngle = 0.2;
  575. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  576. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  577. if (projectileAngle > straightAngle)
  578. setGroup(getUpwardsGroup());
  579. else if (projectileAngle < -straightAngle)
  580. setGroup(getDownwardsGroup());
  581. else
  582. setGroup(getForwardGroup());
  583. }
  584. void RangedAttackAnimation::initializeProjectile()
  585. {
  586. const CCreature *shooterInfo = getCreature();
  587. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225);
  588. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  589. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  590. if (getGroup() == getUpwardsGroup())
  591. {
  592. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  593. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  594. }
  595. else if (getGroup() == getDownwardsGroup())
  596. {
  597. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  598. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  599. }
  600. else if (getGroup() == getForwardGroup())
  601. {
  602. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  603. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  604. }
  605. else
  606. {
  607. assert(0);
  608. }
  609. createProjectile(shotOrigin, shotTarget);
  610. }
  611. void RangedAttackAnimation::emitProjectile()
  612. {
  613. logAnim->debug("Ranged attack projectile emitted");
  614. owner.projectilesController->emitStackProjectile(attackingStack);
  615. projectileEmitted = true;
  616. }
  617. void RangedAttackAnimation::nextFrame()
  618. {
  619. // animation should be paused if there is an active projectile
  620. if (projectileEmitted)
  621. {
  622. if (!owner.projectilesController->hasActiveProjectile(attackingStack, false))
  623. {
  624. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  625. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  626. }
  627. }
  628. AttackAnimation::nextFrame();
  629. if (!projectileEmitted)
  630. {
  631. // emit projectile once animation playback reached "climax" frame
  632. if ( stackAnimation(attackingStack)->getCurrentFrame() >= getAttackClimaxFrame() )
  633. {
  634. emitProjectile();
  635. return;
  636. }
  637. }
  638. }
  639. RangedAttackAnimation::~RangedAttackAnimation()
  640. {
  641. assert(!owner.projectilesController->hasActiveProjectile(attackingStack, false));
  642. assert(projectileEmitted);
  643. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  644. if (!projectileEmitted)
  645. {
  646. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  647. emitProjectile();
  648. }
  649. }
  650. ShootingAnimation::ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  651. : RangedAttackAnimation(owner, attacker, _dest, _attacked)
  652. {
  653. logAnim->debug("Created ShootingAnimation for %s", stack->getName());
  654. }
  655. void ShootingAnimation::createProjectile(const Point & from, const Point & dest) const
  656. {
  657. owner.projectilesController->createProjectile(attackingStack, from, dest);
  658. }
  659. uint32_t ShootingAnimation::getAttackClimaxFrame() const
  660. {
  661. const CCreature *shooterInfo = getCreature();
  662. return shooterInfo->animation.attackClimaxFrame;
  663. }
  664. ECreatureAnimType::Type ShootingAnimation::getUpwardsGroup() const
  665. {
  666. return ECreatureAnimType::SHOOT_UP;
  667. }
  668. ECreatureAnimType::Type ShootingAnimation::getForwardGroup() const
  669. {
  670. return ECreatureAnimType::SHOOT_FRONT;
  671. }
  672. ECreatureAnimType::Type ShootingAnimation::getDownwardsGroup() const
  673. {
  674. return ECreatureAnimType::SHOOT_DOWN;
  675. }
  676. CatapultAnimation::CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, int _catapultDmg)
  677. : ShootingAnimation(owner, attacker, _dest, _attacked),
  678. catapultDamage(_catapultDmg),
  679. explosionEmitted(false)
  680. {
  681. logAnim->debug("Created shooting anim for %s", stack->getName());
  682. }
  683. void CatapultAnimation::nextFrame()
  684. {
  685. ShootingAnimation::nextFrame();
  686. if ( explosionEmitted)
  687. return;
  688. if ( !projectileEmitted)
  689. return;
  690. if (owner.projectilesController->hasActiveProjectile(attackingStack, false))
  691. return;
  692. explosionEmitted = true;
  693. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225) - Point(126, 105);
  694. if(catapultDamage > 0)
  695. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLHIT", "SGEXPL.DEF", shotTarget));
  696. else
  697. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLMISS", "CSGRCK.DEF", shotTarget));
  698. }
  699. void CatapultAnimation::createProjectile(const Point & from, const Point & dest) const
  700. {
  701. owner.projectilesController->createCatapultProjectile(attackingStack, from, dest);
  702. }
  703. CastAnimation::CastAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell)
  704. : RangedAttackAnimation(owner_, attacker, dest_, defender),
  705. spell(spell)
  706. {
  707. assert(dest.isValid());// FIXME: when?
  708. if(!dest_.isValid() && defender)
  709. dest = defender->getPosition();
  710. }
  711. ECreatureAnimType::Type CastAnimation::getUpwardsGroup() const
  712. {
  713. return findValidGroup({
  714. ECreatureAnimType::CAST_UP,
  715. ECreatureAnimType::SPECIAL_UP,
  716. ECreatureAnimType::SHOOT_UP,
  717. ECreatureAnimType::ATTACK_UP
  718. });
  719. }
  720. ECreatureAnimType::Type CastAnimation::getForwardGroup() const
  721. {
  722. return findValidGroup({
  723. ECreatureAnimType::CAST_FRONT,
  724. ECreatureAnimType::SPECIAL_FRONT,
  725. ECreatureAnimType::SHOOT_FRONT,
  726. ECreatureAnimType::ATTACK_FRONT
  727. });
  728. }
  729. ECreatureAnimType::Type CastAnimation::getDownwardsGroup() const
  730. {
  731. return findValidGroup({
  732. ECreatureAnimType::CAST_DOWN,
  733. ECreatureAnimType::SPECIAL_DOWN,
  734. ECreatureAnimType::SHOOT_DOWN,
  735. ECreatureAnimType::ATTACK_DOWN
  736. });
  737. }
  738. void CastAnimation::createProjectile(const Point & from, const Point & dest) const
  739. {
  740. if (!spell->animationInfo.projectile.empty())
  741. owner.projectilesController->createSpellProjectile(attackingStack, from, dest, spell);
  742. }
  743. uint32_t CastAnimation::getAttackClimaxFrame() const
  744. {
  745. //TODO: allow defining this parameter in config file, separately from attackClimaxFrame of missile attacks
  746. uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
  747. return maxFrames / 2;
  748. }
  749. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects):
  750. BattleAnimation(owner),
  751. animation(std::make_shared<CAnimation>(animationName)),
  752. soundName(soundName),
  753. effectFlags(effects),
  754. soundPlayed(false),
  755. soundFinished(false),
  756. effectFinished(false)
  757. {
  758. logAnim->debug("CPointEffectAnimation::init: effect %s", animationName);
  759. }
  760. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects):
  761. PointEffectAnimation(owner, soundName, animationName, effects)
  762. {
  763. battlehexes = hex;
  764. }
  765. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex, int effects):
  766. PointEffectAnimation(owner, soundName, animationName, effects)
  767. {
  768. assert(hex.isValid());
  769. battlehexes.push_back(hex);
  770. }
  771. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos, int effects):
  772. PointEffectAnimation(owner, soundName, animationName, effects)
  773. {
  774. positions = pos;
  775. }
  776. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, int effects):
  777. PointEffectAnimation(owner, soundName, animationName, effects)
  778. {
  779. positions.push_back(pos);
  780. }
  781. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects):
  782. PointEffectAnimation(owner, soundName, animationName, effects)
  783. {
  784. assert(hex.isValid());
  785. battlehexes.push_back(hex);
  786. positions.push_back(pos);
  787. }
  788. bool PointEffectAnimation::init()
  789. {
  790. animation->preload();
  791. auto first = animation->getImage(0, 0, true);
  792. if(!first)
  793. {
  794. delete this;
  795. return false;
  796. }
  797. if (screenFill())
  798. {
  799. for(int i=0; i * first->width() < owner.pos.w ; ++i)
  800. for(int j=0; j * first->height() < owner.pos.h ; ++j)
  801. positions.push_back(Point( owner.pos.x + i * first->width(), owner.pos.y + j * first->height()));
  802. }
  803. BattleEffect be;
  804. be.effectID = ID;
  805. be.animation = animation;
  806. be.currentFrame = 0;
  807. for (size_t i = 0; i < std::max(battlehexes.size(), positions.size()); ++i)
  808. {
  809. bool hasTile = i < battlehexes.size();
  810. bool hasPosition = i < positions.size();
  811. if (hasTile && !forceOnTop())
  812. be.position = battlehexes[i];
  813. else
  814. be.position = BattleHex::INVALID;
  815. if (hasPosition)
  816. {
  817. be.x = positions[i].x;
  818. be.y = positions[i].y;
  819. }
  820. else
  821. {
  822. const CStack * destStack = owner.getCurrentPlayerInterface()->cb->battleGetStackByPos(battlehexes[i], false);
  823. Rect tilePos = owner.fieldController->hexPositionAbsolute(battlehexes[i]);
  824. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  825. if(destStack && destStack->doubleWide()) // Correction for 2-hex creatures.
  826. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  827. if (alignToBottom())
  828. be.y = tilePos.y + tilePos.h - first->height();
  829. else
  830. be.y = tilePos.y - first->height()/2;
  831. }
  832. owner.effectsController->battleEffects.push_back(be);
  833. }
  834. return true;
  835. }
  836. void PointEffectAnimation::nextFrame()
  837. {
  838. playSound();
  839. playEffect();
  840. if (soundFinished && effectFinished)
  841. {
  842. //remove visual effect itself only if sound has finished as well - necessary for obstacles like force field
  843. clearEffect();
  844. delete this;
  845. }
  846. }
  847. bool PointEffectAnimation::alignToBottom() const
  848. {
  849. return effectFlags & ALIGN_TO_BOTTOM;
  850. }
  851. bool PointEffectAnimation::waitForSound() const
  852. {
  853. return effectFlags & WAIT_FOR_SOUND;
  854. }
  855. bool PointEffectAnimation::forceOnTop() const
  856. {
  857. return effectFlags & FORCE_ON_TOP;
  858. }
  859. bool PointEffectAnimation::screenFill() const
  860. {
  861. return effectFlags & SCREEN_FILL;
  862. }
  863. void PointEffectAnimation::onEffectFinished()
  864. {
  865. effectFinished = true;
  866. }
  867. void PointEffectAnimation::onSoundFinished()
  868. {
  869. soundFinished = true;
  870. }
  871. void PointEffectAnimation::playSound()
  872. {
  873. if (soundPlayed)
  874. return;
  875. soundPlayed = true;
  876. if (soundName.empty())
  877. {
  878. onSoundFinished();
  879. return;
  880. }
  881. int channel = CCS->soundh->playSound(soundName);
  882. if (!waitForSound() || channel == -1)
  883. onSoundFinished();
  884. else
  885. CCS->soundh->setCallback(channel, [&](){ onSoundFinished(); });
  886. }
  887. void PointEffectAnimation::playEffect()
  888. {
  889. if ( effectFinished )
  890. return;
  891. for(auto & elem : owner.effectsController->battleEffects)
  892. {
  893. if(elem.effectID == ID)
  894. {
  895. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  896. if(elem.currentFrame >= elem.animation->size())
  897. {
  898. elem.currentFrame = elem.animation->size() - 1;
  899. onEffectFinished();
  900. break;
  901. }
  902. }
  903. }
  904. }
  905. void PointEffectAnimation::clearEffect()
  906. {
  907. auto & effects = owner.effectsController->battleEffects;
  908. vstd::erase_if(effects, [&](const BattleEffect & effect){
  909. return effect.effectID == ID;
  910. });
  911. }
  912. PointEffectAnimation::~PointEffectAnimation()
  913. {
  914. assert(effectFinished);
  915. assert(soundFinished);
  916. }
  917. HeroCastAnimation::HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell):
  918. BattleAnimation(owner),
  919. projectileEmitted(false),
  920. hero(hero),
  921. target(defender),
  922. tile(dest),
  923. spell(spell)
  924. {
  925. }
  926. bool HeroCastAnimation::init()
  927. {
  928. hero->setPhase(EHeroAnimType::CAST_SPELL);
  929. hero->onPhaseFinished([&](){
  930. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == true);
  931. delete this;
  932. });
  933. initializeProjectile();
  934. return true;
  935. }
  936. void HeroCastAnimation::initializeProjectile()
  937. {
  938. // spell has no projectile to play, ignore this step
  939. if (spell->animationInfo.projectile.empty())
  940. return;
  941. // targeted spells should have well, target
  942. assert(tile.isValid());
  943. Point srccoord = hero->pos.center();
  944. Point destcoord = owner.stacksController->getStackPositionAtHex(tile, target); //position attacked by projectile
  945. destcoord += Point(222, 265); // FIXME: what are these constants?
  946. owner.projectilesController->createSpellProjectile( nullptr, srccoord, destcoord, spell);
  947. }
  948. void HeroCastAnimation::emitProjectile()
  949. {
  950. if (projectileEmitted)
  951. return;
  952. //spell has no projectile to play, skip this step and immediately play hit animations
  953. if (spell->animationInfo.projectile.empty())
  954. emitAnimationEvent();
  955. else
  956. owner.projectilesController->emitStackProjectile( nullptr );
  957. projectileEmitted = true;
  958. }
  959. void HeroCastAnimation::emitAnimationEvent()
  960. {
  961. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  962. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  963. }
  964. void HeroCastAnimation::nextFrame()
  965. {
  966. float frame = hero->getFrame();
  967. if (frame < 4.0f) // middle point of animation //TODO: un-hardcode
  968. return;
  969. if (!projectileEmitted)
  970. {
  971. emitProjectile();
  972. hero->pause();
  973. return;
  974. }
  975. if (!owner.projectilesController->hasActiveProjectile(nullptr, false))
  976. {
  977. emitAnimationEvent();
  978. //TODO: check H3 - it is possible that hero animation should be paused until hit effect is over, not just projectile
  979. hero->play();
  980. }
  981. }