1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/battle/BattleAnimationClasses.cpp

1172 lines
31 KiB
C++
Raw Normal View History

/*
* BattleAnimationClasses.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "BattleAnimationClasses.h"
#include "BattleInterface.h"
#include "BattleInterfaceClasses.h"
#include "BattleProjectileController.h"
#include "BattleSiegeController.h"
#include "BattleFieldController.h"
#include "BattleEffectsController.h"
#include "BattleStacksController.h"
#include "CreatureAnimation.h"
#include "../CGameInfo.h"
#include "../CMusicHandler.h"
#include "../CPlayerInterface.h"
#include "../gui/CCursorHandler.h"
#include "../gui/CGuiHandler.h"
#include "../../CCallback.h"
#include "../../lib/CStack.h"
2022-12-12 22:04:25 +02:00
BattleAnimation::BattleAnimation(BattleInterface & owner)
2022-12-13 13:58:16 +02:00
: owner(owner),
ID(owner.stacksController->animIDhelper++),
initialized(false)
{
2017-08-10 22:47:58 +02:00
logAnim->trace("Animation #%d created", ID);
}
2022-12-12 22:04:25 +02:00
bool BattleAnimation::tryInitialize()
{
assert(!initialized);
if ( init() )
{
initialized = true;
return true;
}
return false;
}
2022-12-12 22:04:25 +02:00
bool BattleAnimation::isInitialized()
{
return initialized;
}
2022-12-12 22:04:25 +02:00
BattleAnimation::~BattleAnimation()
{
logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
for(auto & elem : pendingAnimations())
{
if(elem == this)
elem = nullptr;
}
logAnim->trace("Animation #%d deleted", ID);
}
2022-12-12 22:04:25 +02:00
std::vector<BattleAnimation *> & BattleAnimation::pendingAnimations()
{
2022-12-13 13:58:16 +02:00
return owner.stacksController->currentAnimations;
}
2022-12-12 22:04:25 +02:00
std::shared_ptr<CreatureAnimation> BattleAnimation::stackAnimation(const CStack * stack) const
{
2022-12-13 13:58:16 +02:00
return owner.stacksController->stackAnimation[stack->ID];
}
2022-12-12 22:04:25 +02:00
bool BattleAnimation::stackFacingRight(const CStack * stack)
{
2022-12-13 13:58:16 +02:00
return owner.stacksController->stackFacingRight[stack->ID];
}
2022-12-12 22:04:25 +02:00
void BattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
{
2022-12-13 13:58:16 +02:00
owner.stacksController->stackFacingRight[stack->ID] = facingRight;
}
2022-12-12 22:04:25 +02:00
BattleStackAnimation::BattleStackAnimation(BattleInterface & owner, const CStack * stack)
: BattleAnimation(owner),
myAnim(stackAnimation(stack)),
stack(stack)
{
assert(myAnim);
}
2022-12-12 22:04:25 +02:00
void AttackAnimation::nextFrame()
{
if(myAnim->getType() != group)
{
myAnim->setType(group);
myAnim->onAnimationReset += [&](){ delete this; };
}
if(!soundPlayed)
{
2022-12-12 22:04:25 +02:00
playSound();
soundPlayed = true;
}
}
2022-12-12 22:04:25 +02:00
AttackAnimation::~AttackAnimation()
{
myAnim->setType(ECreatureAnimType::HOLDING);
}
2022-12-12 22:04:25 +02:00
const CCreature * AttackAnimation::getCreature() const
2022-11-27 20:01:52 +02:00
{
if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
2022-12-13 13:58:16 +02:00
return owner.siegeController->getTurretCreature();
2022-11-27 20:01:52 +02:00
else
return attackingStack->getCreature();
}
2022-12-12 22:04:25 +02:00
AttackAnimation::AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
: BattleStackAnimation(owner, attacker),
group(ECreatureAnimType::SHOOT_FRONT),
2022-11-28 16:43:38 +02:00
soundPlayed(false),
dest(_dest),
2022-12-12 22:04:25 +02:00
defendingStack(defender),
2022-11-28 16:43:38 +02:00
attackingStack(attacker)
{
assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
attackingStackPosBeforeReturn = attackingStack->getPosition();
}
2022-12-12 22:04:25 +02:00
bool HittedAnimation::init()
{
CCS->soundh->playSound(battle_sound(stack->getCreature(), wince));
myAnim->playOnce(ECreatureAnimType::HITTED);
myAnim->onAnimationReset += [&](){ delete this; };
return true;
}
HittedAnimation::HittedAnimation(BattleInterface & owner, const CStack * stack)
: BattleStackAnimation(owner, stack)
{
}
2022-12-12 22:04:25 +02:00
DefenceAnimation::DefenceAnimation(BattleInterface & owner, const CStack * stack)
: BattleStackAnimation(owner, stack)
{
2022-12-12 22:04:25 +02:00
}
2022-12-12 22:04:25 +02:00
bool DefenceAnimation::init()
{
CCS->soundh->playSound(battle_sound(stack->getCreature(), defend));
myAnim->playOnce(ECreatureAnimType::DEFENCE);
myAnim->onAnimationReset += [&](){ delete this; };
return true; //initialized successfuly
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type DeathAnimation::getMyAnimType()
{
2022-12-12 22:04:25 +02:00
if(rangedAttack && myAnim->framesInGroup(ECreatureAnimType::DEATH_RANGED) > 0)
return ECreatureAnimType::DEATH_RANGED;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
else
2022-12-12 22:04:25 +02:00
return ECreatureAnimType::DEATH;
}
2022-12-12 22:04:25 +02:00
bool DeathAnimation::init()
{
2022-12-12 22:04:25 +02:00
CCS->soundh->playSound(battle_sound(stack->getCreature(), killed));
myAnim->playOnce(getMyAnimType());
myAnim->onAnimationReset += [&](){ delete this; };
return true;
}
2016-11-27 16:48:18 +02:00
2022-12-12 22:04:25 +02:00
DeathAnimation::DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged):
BattleStackAnimation(owner, stack),
rangedAttack(ranged)
{
}
2022-12-12 22:04:25 +02:00
DeathAnimation::~DeathAnimation()
{
2022-12-12 22:04:25 +02:00
if(rangedAttack && myAnim->framesInGroup(ECreatureAnimType::DEAD_RANGED) > 0)
myAnim->setType(ECreatureAnimType::DEAD_RANGED);
else
2022-12-12 22:04:25 +02:00
myAnim->setType(ECreatureAnimType::DEAD);
}
2022-12-12 22:04:25 +02:00
DummyAnimation::DummyAnimation(BattleInterface & owner, int howManyFrames)
: BattleAnimation(owner),
2022-11-28 16:43:38 +02:00
counter(0),
howMany(howManyFrames)
{
2017-08-11 13:38:10 +02:00
logAnim->debug("Created dummy animation for %d frames", howManyFrames);
}
2022-12-12 22:04:25 +02:00
bool DummyAnimation::init()
{
return true;
}
2022-12-12 22:04:25 +02:00
void DummyAnimation::nextFrame()
{
counter++;
if(counter > howMany)
delete this;
}
2022-12-12 22:04:25 +02:00
bool MeleeAttackAnimation::init()
{
assert(attackingStack);
assert(!myAnim->isDeadOrDying());
2022-11-28 22:35:38 +02:00
if(!attackingStack || myAnim->isDeadOrDying())
{
delete this;
return false;
}
2022-12-12 22:04:25 +02:00
logAnim->info("CMeleeAttackAnimation::init: stack %s -> stack %s", stack->getName(), defendingStack->getName());
static const ECreatureAnimType::Type mutPosToGroup[] =
{
ECreatureAnimType::ATTACK_UP,
ECreatureAnimType::ATTACK_UP,
ECreatureAnimType::ATTACK_FRONT,
ECreatureAnimType::ATTACK_DOWN,
ECreatureAnimType::ATTACK_DOWN,
ECreatureAnimType::ATTACK_FRONT
};
static const ECreatureAnimType::Type mutPosToGroup2H[] =
{
ECreatureAnimType::VCMI_2HEX_UP,
ECreatureAnimType::VCMI_2HEX_UP,
ECreatureAnimType::VCMI_2HEX_FRONT,
ECreatureAnimType::VCMI_2HEX_DOWN,
ECreatureAnimType::VCMI_2HEX_DOWN,
ECreatureAnimType::VCMI_2HEX_FRONT
};
int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
if(mutPos == -1 && attackingStack->doubleWide())
{
2022-12-12 22:04:25 +02:00
mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->getPosition());
}
2022-12-12 22:04:25 +02:00
if (mutPos == -1 && defendingStack->doubleWide())
{
2022-12-12 22:04:25 +02:00
mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, defendingStack->occupiedHex());
}
2022-12-12 22:04:25 +02:00
if (mutPos == -1 && defendingStack->doubleWide() && attackingStack->doubleWide())
{
2022-12-12 22:04:25 +02:00
mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->occupiedHex());
}
switch(mutPos) //attack direction
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
group = mutPosToGroup[mutPos];
if(attackingStack->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
{
ECreatureAnimType::Type group2H = mutPosToGroup2H[mutPos];
if(myAnim->framesInGroup(group2H)>0)
group = group2H;
}
break;
default:
2017-08-11 13:38:10 +02:00
logGlobal->error("Critical Error! Wrong dest in stackAttacking! dest: %d; attacking stack pos: %d; mutual pos: %d", dest.hex, attackingStackPosBeforeReturn, mutPos);
group = ECreatureAnimType::ATTACK_FRONT;
break;
}
return true;
}
2022-12-12 22:04:25 +02:00
void MeleeAttackAnimation::nextFrame()
{
size_t currentFrame = stackAnimation(attackingStack)->getCurrentFrame();
size_t totalFrames = stackAnimation(attackingStack)->framesInGroup(group);
if ( currentFrame * 2 >= totalFrames )
{
if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
owner.setAnimationCondition(EAnimationEvents::HIT, true);
}
2022-12-12 22:04:25 +02:00
AttackAnimation::nextFrame();
}
2022-12-12 22:04:25 +02:00
void MeleeAttackAnimation::playSound()
{
CCS->soundh->playSound(battle_sound(getCreature(), attack));
}
2022-12-12 22:04:25 +02:00
MeleeAttackAnimation::MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
: AttackAnimation(owner, attacker, _dest, _attacked)
{
2017-08-10 20:59:55 +02:00
logAnim->debug("Created melee attack anim for %s", attacker->getName());
}
2022-12-12 22:04:25 +02:00
StackMoveAnimation::StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex _currentHex):
BattleStackAnimation(owner, _stack),
2022-11-28 22:35:38 +02:00
currentHex(_currentHex)
{
}
2022-12-12 22:04:25 +02:00
bool MovementAnimation::init()
{
assert(stack);
assert(!myAnim->isDeadOrDying());
2022-11-28 22:35:38 +02:00
if(!stack || myAnim->isDeadOrDying())
{
delete this;
return false;
}
if(stackAnimation(stack)->framesInGroup(ECreatureAnimType::MOVING) == 0 ||
stack->hasBonus(Selector::typeSubtype(Bonus::FLYING, 1)))
{
//no movement or teleport, end immediately
delete this;
return false;
}
logAnim->info("CMovementAnimation::init: stack %s moves %d -> %d", stack->getName(), oldPos, currentHex);
//reverse unit if necessary
2022-12-13 13:58:16 +02:00
if(owner.stacksController->shouldRotate(stack, oldPos, currentHex))
{
// it seems that H3 does NOT plays full rotation animation during movement
// Logical since it takes quite a lot of time
rotateStack(oldPos);
}
if(myAnim->getType() != ECreatureAnimType::MOVING)
{
myAnim->setType(ECreatureAnimType::MOVING);
}
2022-12-13 13:58:16 +02:00
if (owner.moveSoundHander == -1)
{
2022-12-13 13:58:16 +02:00
owner.moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
}
2022-12-13 13:58:16 +02:00
Point begPosition = owner.stacksController->getStackPositionAtHex(oldPos, stack);
Point endPosition = owner.stacksController->getStackPositionAtHex(currentHex, stack);
timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
begX = begPosition.x;
begY = begPosition.y;
progress = 0;
distanceX = endPosition.x - begPosition.x;
distanceY = endPosition.y - begPosition.y;
if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
{
float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
}
return true;
}
2022-12-12 22:04:25 +02:00
void MovementAnimation::nextFrame()
{
progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
//moving instructions
myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
2022-12-12 22:04:25 +02:00
BattleAnimation::nextFrame();
if(progress >= 1.0)
{
// Sets the position of the creature animation sprites
2022-12-13 13:58:16 +02:00
Point coords = owner.stacksController->getStackPositionAtHex(currentHex, stack);
myAnim->pos = coords;
// true if creature haven't reached the final destination hex
if ((curentMoveIndex + 1) < destTiles.size())
{
// update the next hex field which has to be reached by the stack
curentMoveIndex++;
2022-11-28 22:35:38 +02:00
oldPos = currentHex;
currentHex = destTiles[curentMoveIndex];
// request re-initialization
initialized = false;
}
else
delete this;
}
}
2022-12-12 22:04:25 +02:00
MovementAnimation::~MovementAnimation()
{
assert(stack);
2022-12-13 13:58:16 +02:00
myAnim->pos = owner.stacksController->getStackPositionAtHex(currentHex, stack);
2022-12-13 13:58:16 +02:00
if(owner.moveSoundHander != -1)
{
2022-12-13 13:58:16 +02:00
CCS->soundh->stopSound(owner.moveSoundHander);
owner.moveSoundHander = -1;
}
}
2022-12-12 22:04:25 +02:00
MovementAnimation::MovementAnimation(BattleInterface & owner, const CStack *_stack, std::vector<BattleHex> _destTiles, int _distance)
: StackMoveAnimation(owner, _stack, _destTiles.front()),
destTiles(_destTiles),
curentMoveIndex(0),
oldPos(stack->getPosition()),
begX(0), begY(0),
distanceX(0), distanceY(0),
timeToMove(0.0),
2022-11-28 22:35:38 +02:00
progress(0.0)
{
2017-08-10 20:59:55 +02:00
logAnim->debug("Created movement anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
MovementEndAnimation::MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile)
: StackMoveAnimation(owner, _stack, destTile)
{
2017-08-10 20:59:55 +02:00
logAnim->debug("Created movement end anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
bool MovementEndAnimation::init()
{
assert(stack);
assert(!myAnim->isDeadOrDying());
if(!stack || myAnim->isDeadOrDying())
{
delete this;
return false;
}
logAnim->info("CMovementEndAnimation::init: stack %s", stack->getName());
CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_END))
{
delete this;
return false;
}
myAnim->setType(ECreatureAnimType::MOVE_END);
myAnim->onAnimationReset += [&](){ delete this; };
return true;
}
2022-12-12 22:04:25 +02:00
MovementEndAnimation::~MovementEndAnimation()
{
if(myAnim->getType() != ECreatureAnimType::DEAD)
myAnim->setType(ECreatureAnimType::HOLDING); //resetting to default
CCS->curh->show();
}
2022-12-12 22:04:25 +02:00
MovementStartAnimation::MovementStartAnimation(BattleInterface & owner, const CStack * _stack)
: StackMoveAnimation(owner, _stack, _stack->getPosition())
{
2017-08-10 20:59:55 +02:00
logAnim->debug("Created movement start anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
bool MovementStartAnimation::init()
{
assert(stack);
assert(!myAnim->isDeadOrDying());
2022-11-28 22:35:38 +02:00
if(!stack || myAnim->isDeadOrDying())
{
delete this;
return false;
}
logAnim->info("CMovementStartAnimation::init: stack %s", stack->getName());
CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_START))
{
delete this;
return false;
}
myAnim->setType(ECreatureAnimType::MOVE_START);
myAnim->onAnimationReset += [&](){ delete this; };
return true;
}
2022-12-12 22:04:25 +02:00
ReverseAnimation::ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest)
: StackMoveAnimation(owner, stack, dest)
{
2017-08-10 20:59:55 +02:00
logAnim->debug("Created reverse anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
bool ReverseAnimation::init()
{
assert(myAnim);
assert(!myAnim->isDeadOrDying());
2022-11-28 22:35:38 +02:00
if(myAnim == nullptr || myAnim->isDeadOrDying())
{
delete this;
return false; //there is no such creature
}
logAnim->info("CReverseAnimation::init: stack %s", stack->getName());
if(myAnim->framesInGroup(ECreatureAnimType::TURN_L))
{
myAnim->playOnce(ECreatureAnimType::TURN_L);
2022-12-12 22:04:25 +02:00
myAnim->onAnimationReset += std::bind(&ReverseAnimation::setupSecondPart, this);
}
else
{
setupSecondPart();
}
return true;
}
2022-12-12 22:04:25 +02:00
void BattleStackAnimation::rotateStack(BattleHex hex)
{
setStackFacingRight(stack, !stackFacingRight(stack));
2022-12-13 13:58:16 +02:00
stackAnimation(stack)->pos = owner.stacksController->getStackPositionAtHex(hex, stack);
}
2022-12-12 22:04:25 +02:00
void ReverseAnimation::setupSecondPart()
{
assert(stack);
if(!stack)
{
delete this;
return;
}
2022-11-28 22:35:38 +02:00
rotateStack(currentHex);
if(myAnim->framesInGroup(ECreatureAnimType::TURN_R))
{
myAnim->playOnce(ECreatureAnimType::TURN_R);
myAnim->onAnimationReset += [&](){ delete this; };
}
else
delete this;
}
2022-12-12 22:04:25 +02:00
bool ResurrectionAnimation::init()
{
assert(stack);
if(!stack)
{
delete this;
return false;
}
logAnim->info("CResurrectionAnimation::init: stack %s", stack->getName());
myAnim->playOnce(ECreatureAnimType::RESURRECTION);
myAnim->onAnimationReset += [&](){ delete this; };
return true;
}
2022-12-12 22:04:25 +02:00
ResurrectionAnimation::ResurrectionAnimation(BattleInterface & owner, const CStack * _stack):
BattleStackAnimation(owner, _stack)
{
}
bool FadingAnimation::init()
{
logAnim->info("FadingAnimation::init: stack %s", stack->getName());
//TODO: pause animation?
return true;
}
void FadingAnimation::nextFrame()
{
float elapsed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
float fullTime = AnimationControls::getFadeInDuration();
float delta = elapsed / fullTime;
progress += delta;
if (progress > 1.0f)
progress = 1.0f;
uint8_t blueFactor = stack->cloned ? 0 : 255;
uint8_t blueAdded = stack->cloned ? 255 : 0;
uint8_t alpha = CSDL_Ext::lerp(from, dest, progress);
ColorShifterMultiplyAndAdd shifterFade ({255, 255, blueFactor, alpha}, {0, 0, blueAdded, 0});
stackAnimation(stack)->shiftColor(&shifterFade);
if (progress == 1.0f)
delete this;
}
FadingAnimation::FadingAnimation(BattleInterface & owner, const CStack * _stack, uint8_t alphaFrom, uint8_t alphaDest):
BattleStackAnimation(owner, _stack),
from(alphaFrom),
dest(alphaDest)
{
}
2022-12-12 22:04:25 +02:00
RangedAttackAnimation::RangedAttackAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
: AttackAnimation(owner_, attacker, dest_, defender),
projectileEmitted(false)
{
}
2022-12-12 22:04:25 +02:00
void RangedAttackAnimation::playSound()
{
CCS->soundh->playSound(battle_sound(getCreature(), shoot));
}
bool RangedAttackAnimation::init()
{
assert(attackingStack);
2022-11-28 22:35:38 +02:00
assert(!myAnim->isDeadOrDying());
2022-11-28 22:35:38 +02:00
if(!attackingStack || myAnim->isDeadOrDying())
{
//FIXME: how is this possible?
logAnim->warn("Shooting animation has not started yet but attacker is dead! Aborting...");
delete this;
return false;
}
logAnim->info("CRangedAttackAnimation::init: stack %s", stack->getName());
setAnimationGroup();
initializeProjectile();
return true;
}
2022-12-12 22:04:25 +02:00
void RangedAttackAnimation::setAnimationGroup()
{
Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
2022-12-12 22:04:25 +02:00
Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack);
//maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
static const double straightAngle = 0.2;
2022-11-27 20:01:52 +02:00
double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
// Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
if (projectileAngle > straightAngle)
group = getUpwardsGroup();
else if (projectileAngle < -straightAngle)
group = getDownwardsGroup();
else
group = getForwardGroup();
}
2022-12-12 22:04:25 +02:00
void RangedAttackAnimation::initializeProjectile()
{
2022-11-27 20:01:52 +02:00
const CCreature *shooterInfo = getCreature();
2022-12-12 22:04:25 +02:00
Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225);
Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
2022-12-01 23:40:03 +02:00
if (group == getUpwardsGroup())
{
shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
}
2022-12-01 23:40:03 +02:00
else if (group == getDownwardsGroup())
{
shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
}
2022-12-01 23:40:03 +02:00
else if (group == getForwardGroup())
{
shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
}
else
{
assert(0);
}
createProjectile(shotOrigin, shotTarget);
}
2022-12-12 22:04:25 +02:00
void RangedAttackAnimation::emitProjectile()
{
logAnim->info("Ranged attack projectile emitted");
2022-12-13 13:58:16 +02:00
owner.projectilesController->emitStackProjectile(attackingStack);
projectileEmitted = true;
}
2022-12-12 22:04:25 +02:00
void RangedAttackAnimation::nextFrame()
{
2022-11-27 20:01:52 +02:00
// animation should be paused if there is an active projectile
if (projectileEmitted)
{
2022-12-13 13:58:16 +02:00
if (owner.projectilesController->hasActiveProjectile(attackingStack))
2022-11-27 20:01:52 +02:00
stackAnimation(attackingStack)->pause();
else
{
2022-11-27 20:01:52 +02:00
stackAnimation(attackingStack)->play();
if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
owner.setAnimationCondition(EAnimationEvents::HIT, true);
}
2022-11-27 20:01:52 +02:00
}
2022-12-12 22:04:25 +02:00
AttackAnimation::nextFrame();
2022-11-27 20:01:52 +02:00
if (!projectileEmitted)
{
// emit projectile once animation playback reached "climax" frame
2022-12-06 14:12:13 +02:00
if ( stackAnimation(attackingStack)->getCurrentFrame() >= getAttackClimaxFrame() )
{
emitProjectile();
stackAnimation(attackingStack)->pause();
return;
}
}
}
2022-12-12 22:04:25 +02:00
RangedAttackAnimation::~RangedAttackAnimation()
{
2022-12-13 13:58:16 +02:00
assert(!owner.projectilesController->hasActiveProjectile(attackingStack));
2022-11-27 20:01:52 +02:00
assert(projectileEmitted);
// FIXME: is this possible? Animation is over but we're yet to fire projectile?
if (!projectileEmitted)
{
logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
emitProjectile();
}
}
2022-12-12 22:04:25 +02:00
ShootingAnimation::ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
: RangedAttackAnimation(owner, attacker, _dest, _attacked)
{
logAnim->debug("Created shooting anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
void ShootingAnimation::createProjectile(const Point & from, const Point & dest) const
{
2022-12-13 13:58:16 +02:00
owner.projectilesController->createProjectile(attackingStack, from, dest);
}
2022-12-12 22:04:25 +02:00
uint32_t ShootingAnimation::getAttackClimaxFrame() const
2022-12-06 14:12:13 +02:00
{
const CCreature *shooterInfo = getCreature();
return shooterInfo->animation.attackClimaxFrame;
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type ShootingAnimation::getUpwardsGroup() const
{
return ECreatureAnimType::SHOOT_UP;
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type ShootingAnimation::getForwardGroup() const
{
return ECreatureAnimType::SHOOT_FRONT;
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type ShootingAnimation::getDownwardsGroup() const
{
return ECreatureAnimType::SHOOT_DOWN;
}
2022-12-12 22:04:25 +02:00
CatapultAnimation::CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, int _catapultDmg)
: ShootingAnimation(owner, attacker, _dest, _attacked),
catapultDamage(_catapultDmg),
explosionEmitted(false)
{
logAnim->debug("Created shooting anim for %s", stack->getName());
}
2022-12-12 22:04:25 +02:00
void CatapultAnimation::nextFrame()
{
2022-12-12 22:04:25 +02:00
ShootingAnimation::nextFrame();
if ( explosionEmitted)
return;
if ( !projectileEmitted)
return;
2022-12-13 13:58:16 +02:00
if (owner.projectilesController->hasActiveProjectile(attackingStack))
return;
explosionEmitted = true;
2022-12-12 22:04:25 +02:00
Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225) - Point(126, 105);
if(catapultDamage > 0)
2022-12-12 22:04:25 +02:00
owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLHIT", "SGEXPL.DEF", shotTarget));
else
2022-12-12 22:04:25 +02:00
owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLMISS", "CSGRCK.DEF", shotTarget));
}
2022-12-12 22:04:25 +02:00
void CatapultAnimation::createProjectile(const Point & from, const Point & dest) const
{
2022-12-13 13:58:16 +02:00
owner.projectilesController->createCatapultProjectile(attackingStack, from, dest);
}
2022-12-12 22:04:25 +02:00
CastAnimation::CastAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell)
: RangedAttackAnimation(owner_, attacker, dest_, defender),
spell(spell)
{
assert(dest.isValid());// FIXME: when?
if(!dest_.isValid() && defender)
dest = defender->getPosition();
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type CastAnimation::findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const
{
for ( auto group : candidates)
{
if(myAnim->framesInGroup(group) > 0)
return group;
}
assert(0);
return ECreatureAnimType::HOLDING;
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type CastAnimation::getUpwardsGroup() const
{
return findValidGroup({
ECreatureAnimType::VCMI_CAST_UP,
ECreatureAnimType::CAST_UP,
ECreatureAnimType::SHOOT_UP,
ECreatureAnimType::ATTACK_UP
});
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type CastAnimation::getForwardGroup() const
{
return findValidGroup({
ECreatureAnimType::VCMI_CAST_FRONT,
ECreatureAnimType::CAST_FRONT,
ECreatureAnimType::SHOOT_FRONT,
ECreatureAnimType::ATTACK_FRONT
});
}
2022-12-12 22:04:25 +02:00
ECreatureAnimType::Type CastAnimation::getDownwardsGroup() const
{
return findValidGroup({
ECreatureAnimType::VCMI_CAST_DOWN,
ECreatureAnimType::CAST_DOWN,
ECreatureAnimType::SHOOT_DOWN,
ECreatureAnimType::ATTACK_DOWN
});
}
2022-12-12 22:04:25 +02:00
void CastAnimation::createProjectile(const Point & from, const Point & dest) const
{
2022-12-01 23:40:03 +02:00
if (!spell->animationInfo.projectile.empty())
2022-12-13 13:58:16 +02:00
owner.projectilesController->createSpellProjectile(attackingStack, from, dest, spell);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
2022-12-12 22:04:25 +02:00
uint32_t CastAnimation::getAttackClimaxFrame() const
2022-12-06 14:12:13 +02:00
{
//TODO: allow defining this parameter in config file, separately from attackClimaxFrame of missile attacks
2022-12-06 14:12:13 +02:00
uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(group);
if (maxFrames > 2)
return maxFrames - 2;
return 0;
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects):
BattleAnimation(owner),
animation(std::make_shared<CAnimation>(animationName)),
soundName(soundName),
effectFlags(effects),
soundPlayed(false),
soundFinished(false),
effectFinished(false)
{
logAnim->info("CPointEffectAnimation::init: effect %s", animationName);
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects):
PointEffectAnimation(owner, soundName, animationName, effects)
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
battlehexes = hex;
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex, int effects):
PointEffectAnimation(owner, soundName, animationName, effects)
{
assert(hex.isValid());
battlehexes.push_back(hex);
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos, int effects):
PointEffectAnimation(owner, soundName, animationName, effects)
{
positions = pos;
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, int effects):
PointEffectAnimation(owner, soundName, animationName, effects)
{
positions.push_back(pos);
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects):
PointEffectAnimation(owner, soundName, animationName, effects)
{
assert(hex.isValid());
battlehexes.push_back(hex);
positions.push_back(pos);
}
2022-12-12 22:04:25 +02:00
bool PointEffectAnimation::init()
{
animation->preload();
2018-03-30 13:02:04 +02:00
auto first = animation->getImage(0, 0, true);
if(!first)
{
delete this;
return false;
}
if (screenFill())
{
2022-12-13 13:58:16 +02:00
for(int i=0; i * first->width() < owner.pos.w ; ++i)
for(int j=0; j * first->height() < owner.pos.h ; ++j)
positions.push_back(Point( owner.pos.x + i * first->width(), owner.pos.y + j * first->height()));
}
BattleEffect be;
be.effectID = ID;
be.animation = animation;
be.currentFrame = 0;
for (size_t i = 0; i < std::max(battlehexes.size(), positions.size()); ++i)
{
bool hasTile = i < battlehexes.size();
bool hasPosition = i < positions.size();
if (hasTile && !forceOnTop())
be.position = battlehexes[i];
else
be.position = BattleHex::INVALID;
2016-11-27 16:48:18 +02:00
if (hasPosition)
{
be.x = positions[i].x;
be.y = positions[i].y;
}
else
{
2022-12-13 13:58:16 +02:00
const CStack * destStack = owner.getCurrentPlayerInterface()->cb->battleGetStackByPos(battlehexes[i], false);
Rect tilePos = owner.fieldController->hexPositionAbsolute(battlehexes[i]);
be.x = tilePos.x + tilePos.w/2 - first->width()/2;
if(destStack && destStack->doubleWide()) // Correction for 2-hex creatures.
be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
if (alignToBottom())
be.y = tilePos.y + tilePos.h - first->height();
else
be.y = tilePos.y - first->height()/2;
}
2022-12-13 13:58:16 +02:00
owner.effectsController->battleEffects.push_back(be);
}
return true;
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::nextFrame()
{
playSound();
playEffect();
if (soundFinished && effectFinished)
{
//remove visual effect itself only if sound has finished as well - necessary for obstacles like force field
clearEffect();
delete this;
}
}
2022-12-12 22:04:25 +02:00
bool PointEffectAnimation::alignToBottom() const
{
return effectFlags & ALIGN_TO_BOTTOM;
}
2022-12-12 22:04:25 +02:00
bool PointEffectAnimation::waitForSound() const
{
return effectFlags & WAIT_FOR_SOUND;
}
2022-12-12 22:04:25 +02:00
bool PointEffectAnimation::forceOnTop() const
{
return effectFlags & FORCE_ON_TOP;
}
2022-12-12 22:04:25 +02:00
bool PointEffectAnimation::screenFill() const
{
return effectFlags & SCREEN_FILL;
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::onEffectFinished()
{
effectFinished = true;
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::onSoundFinished()
{
soundFinished = true;
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::playSound()
{
if (soundPlayed)
return;
soundPlayed = true;
if (soundName.empty())
{
onSoundFinished();
return;
}
int channel = CCS->soundh->playSound(soundName);
if (!waitForSound() || channel == -1)
onSoundFinished();
else
CCS->soundh->setCallback(channel, [&](){ onSoundFinished(); });
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::playEffect()
{
if ( effectFinished )
return;
2022-12-13 13:58:16 +02:00
for(auto & elem : owner.effectsController->battleEffects)
{
if(elem.effectID == ID)
{
elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
if(elem.currentFrame >= elem.animation->size())
{
elem.currentFrame = elem.animation->size() - 1;
onEffectFinished();
break;
}
}
}
}
2022-12-12 22:04:25 +02:00
void PointEffectAnimation::clearEffect()
{
2022-12-13 13:58:16 +02:00
auto & effects = owner.effectsController->battleEffects;
2022-11-27 20:01:52 +02:00
2022-12-13 15:10:31 +02:00
vstd::erase_if(effects, [&](const BattleEffect & effect){
return effect.effectID == ID;
});
}
2022-12-12 22:04:25 +02:00
PointEffectAnimation::~PointEffectAnimation()
{
assert(effectFinished);
assert(soundFinished);
}
HeroCastAnimation::HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell):
BattleAnimation(owner),
projectileEmitted(false),
hero(hero),
target(defender),
tile(dest),
spell(spell)
{
}
bool HeroCastAnimation::init()
{
hero->setPhase(EHeroAnimType::CAST_SPELL);
hero->onPhaseFinished([&](){
assert(owner.getAnimationCondition(EAnimationEvents::HIT) == true);
delete this;
});
initializeProjectile();
return true;
}
void HeroCastAnimation::initializeProjectile()
{
//spell has no projectile to play, ignore this step
if (spell->animationInfo.projectile.empty())
return;
Point srccoord = hero->pos.center();
Point destcoord = owner.stacksController->getStackPositionAtHex(tile, target); //position attacked by projectile
destcoord += Point(222, 265); // FIXME: what are these constants?
owner.projectilesController->createSpellProjectile( nullptr, srccoord, destcoord, spell);
}
void HeroCastAnimation::emitProjectile()
{
if (projectileEmitted)
return;
//spell has no projectile to play, skip this step and immediately play hit animations
if (spell->animationInfo.projectile.empty())
emitAnimationEvent();
else
owner.projectilesController->emitStackProjectile( nullptr );
projectileEmitted = true;
}
void HeroCastAnimation::emitAnimationEvent()
{
if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
owner.setAnimationCondition(EAnimationEvents::HIT, true);
}
void HeroCastAnimation::nextFrame()
{
float frame = hero->getFrame();
if (frame < 4.0f) // middle point of animation //TODO: un-hardcode
return;
if (!projectileEmitted)
{
emitProjectile();
hero->pause();
return;
}
if (!owner.projectilesController->hasActiveProjectile(nullptr))
{
emitAnimationEvent();
//TODO: check H3 - it is possible that hero animation should be paused until hit effect is over, not just projectile
hero->play();
}
}