1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-30 08:57:00 +02:00
vcmi/client/battle/BattleEffectsController.cpp

160 lines
4.5 KiB
C++
Raw Normal View History

2022-11-24 16:30:04 +02:00
/*
* BattleEffectsController.cpp, part of VCMI engine
2022-11-24 16:30:04 +02:00
*
* 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 "BattleEffectsController.h"
2022-11-24 16:30:04 +02:00
#include "BattleAnimationClasses.h"
#include "BattleWindow.h"
#include "BattleInterface.h"
#include "BattleInterfaceClasses.h"
#include "BattleFieldController.h"
#include "BattleStacksController.h"
#include "BattleRenderer.h"
2022-11-28 16:43:38 +02:00
2022-11-24 16:30:04 +02:00
#include "../CMusicHandler.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../render/Canvas.h"
#include "../render/CAnimation.h"
2022-11-28 16:43:38 +02:00
2022-11-24 16:30:04 +02:00
#include "../../CCallback.h"
#include "../../lib/battle/BattleAction.h"
#include "../../lib/filesystem/ResourceID.h"
2022-11-24 16:30:04 +02:00
#include "../../lib/NetPacks.h"
#include "../../lib/CStack.h"
#include "../../lib/IGameEventsReceiver.h"
#include "../../lib/CGeneralTextHandler.h"
2022-12-13 13:58:16 +02:00
BattleEffectsController::BattleEffectsController(BattleInterface & owner):
2022-11-24 16:30:04 +02:00
owner(owner)
{
loadColorMuxers();
}
2022-11-24 16:30:04 +02:00
void BattleEffectsController::displayEffect(EBattleEffect effect, const BattleHex & destTile)
2022-11-24 16:30:04 +02:00
{
2022-12-25 20:11:22 +02:00
displayEffect(effect, "", destTile);
2022-11-24 16:30:04 +02:00
}
2022-12-25 20:11:22 +02:00
void BattleEffectsController::displayEffect(EBattleEffect effect, std::string soundFile, const BattleHex & destTile)
2022-11-24 16:30:04 +02:00
{
size_t effectID = static_cast<size_t>(effect);
std::string customAnim = graphics->battleACToDef[effectID][0];
CCS->soundh->playSound( soundFile );
owner.stacksController->addNewAnim(new EffectAnimation(owner, customAnim, destTile));
2022-11-24 16:30:04 +02:00
}
void BattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
2022-11-24 16:30:04 +02:00
{
owner.checkForAnimations();
2022-12-13 13:58:16 +02:00
const CStack * stack = owner.curInt->cb->battleGetStackByID(bte.stackID);
2022-11-24 16:30:04 +02:00
if(!stack)
{
logGlobal->error("Invalid stack ID %d", bte.stackID);
return;
}
//don't show animation when no HP is regenerated
switch(bte.effect)
{
case Bonus::HP_REGENERATION:
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::REGENERATION, "REGENER", stack->getPosition());
2022-11-24 16:30:04 +02:00
break;
case Bonus::MANA_DRAIN:
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::MANA_DRAIN, "MANADRAI", stack->getPosition());
2022-11-24 16:30:04 +02:00
break;
case Bonus::POISON:
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::POISON, "POISON", stack->getPosition());
2022-11-24 16:30:04 +02:00
break;
case Bonus::FEAR:
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::FEAR, "FEAR", stack->getPosition());
2022-11-24 16:30:04 +02:00
break;
case Bonus::MORALE:
{
std::string hlp = CGI->generaltexth->allTexts[33];
boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::GOOD_MORALE, "GOODMRLE", stack->getPosition());
owner.appendBattleLog(hlp);
2022-11-24 16:30:04 +02:00
break;
}
default:
return;
}
owner.waitForAnimations();
2022-11-24 16:30:04 +02:00
}
void BattleEffectsController::startAction(const BattleAction* action)
2022-11-24 16:30:04 +02:00
{
owner.checkForAnimations();
2022-12-13 13:58:16 +02:00
const CStack *stack = owner.curInt->cb->battleGetStackByID(action->stackNumber);
2022-11-24 16:30:04 +02:00
switch(action->actionType)
{
case EActionType::WAIT:
owner.appendBattleLog(stack->formatGeneralMessage(136));
2022-11-24 16:30:04 +02:00
break;
case EActionType::BAD_MORALE:
owner.appendBattleLog(stack->formatGeneralMessage(-34));
2022-12-25 20:11:22 +02:00
displayEffect(EBattleEffect::BAD_MORALE, "BADMRLE", stack->getPosition());
2022-11-24 16:30:04 +02:00
break;
}
owner.waitForAnimations();
2022-11-24 16:30:04 +02:00
}
void BattleEffectsController::collectRenderableObjects(BattleRenderer & renderer)
2022-11-24 16:30:04 +02:00
{
for (auto & elem : battleEffects)
{
renderer.insert( EBattleFieldLayer::EFFECTS, elem.tile, [&elem](BattleRenderer::RendererRef canvas)
{
int currentFrame = static_cast<int>(floor(elem.currentFrame));
currentFrame %= elem.animation->size();
2022-11-24 16:30:04 +02:00
auto img = elem.animation->getImage(currentFrame);
2022-11-24 16:30:04 +02:00
canvas.draw(img, elem.pos);
});
2022-11-24 16:30:04 +02:00
}
}
void BattleEffectsController::loadColorMuxers()
{
const JsonNode config(ResourceID("config/battleEffects.json"));
for(auto & muxer : config["colorMuxers"].Struct())
{
ColorMuxerEffect effect;
std::string identifier = muxer.first;
for (const JsonNode & entry : muxer.second.Vector() )
{
effect.timePoints.push_back(entry["time"].Float());
effect.filters.push_back(ColorFilter::genFromJson(entry));
}
colorMuxerEffects[identifier] = effect;
}
}
const ColorMuxerEffect & BattleEffectsController::getMuxerEffect(const std::string & name)
{
static const ColorMuxerEffect emptyEffect;
if (colorMuxerEffects.count(name))
return colorMuxerEffects[name];
logAnim->error("Failed to find color muxer effect named '%s'!", name);
return emptyEffect;
}