mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-14 10:12:59 +02:00
103 lines
2.8 KiB
C++
103 lines
2.8 KiB
C++
/*
|
|
* BattleFieldController.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 "BattleRenderer.h"
|
|
|
|
#include "BattleInterface.h"
|
|
#include "BattleInterfaceClasses.h"
|
|
#include "BattleEffectsController.h"
|
|
#include "BattleWindow.h"
|
|
#include "BattleSiegeController.h"
|
|
#include "BattleStacksController.h"
|
|
#include "BattleFieldController.h"
|
|
#include "BattleObstacleController.h"
|
|
#include "logging/VisualLogger.h"
|
|
#include "../client/render/Canvas.h"
|
|
#include "../client/render/Colors.h"
|
|
#include "../client/gui/TextAlignment.h"
|
|
|
|
void BattleRenderer::collectObjects()
|
|
{
|
|
owner.effectsController->collectRenderableObjects(*this);
|
|
owner.obstacleController->collectRenderableObjects(*this);
|
|
owner.stacksController->collectRenderableObjects(*this);
|
|
if (owner.siegeController)
|
|
owner.siegeController->collectRenderableObjects(*this);
|
|
if (owner.defendingHero)
|
|
owner.defendingHero->collectRenderableObjects(*this);
|
|
if (owner.attackingHero)
|
|
owner.attackingHero->collectRenderableObjects(*this);
|
|
}
|
|
|
|
void BattleRenderer::sortObjects()
|
|
{
|
|
auto getRow = [](const RenderableInstance & object) -> int
|
|
{
|
|
if (object.tile.isValid())
|
|
return object.tile.getY();
|
|
|
|
if ( object.tile == BattleHex::HEX_BEFORE_ALL )
|
|
return -1;
|
|
|
|
assert( object.tile == BattleHex::HEX_AFTER_ALL || object.tile == BattleHex::INVALID);
|
|
return GameConstants::BFIELD_HEIGHT;
|
|
};
|
|
|
|
std::stable_sort(objects.begin(), objects.end(), [&](const RenderableInstance & left, const RenderableInstance & right){
|
|
if ( getRow(left) != getRow(right))
|
|
return getRow(left) < getRow(right);
|
|
return left.layer < right.layer;
|
|
});
|
|
}
|
|
|
|
void BattleRenderer::renderObjects(BattleRenderer::RendererRef targetCanvas)
|
|
{
|
|
for (auto const & object : objects)
|
|
object.functor(targetCanvas);
|
|
}
|
|
|
|
BattleRenderer::BattleRenderer(BattleInterface & owner):
|
|
owner(owner)
|
|
{
|
|
}
|
|
|
|
void BattleRenderer::insert(EBattleFieldLayer layer, BattleHex tile, BattleRenderer::RenderFunctor functor)
|
|
{
|
|
objects.push_back({functor, layer, tile});
|
|
}
|
|
|
|
class VisualLoggerBattleRenderer : public IBattleOverlayLogVisualizer
|
|
{
|
|
private:
|
|
Canvas & target;
|
|
BattleInterface & owner;
|
|
|
|
public:
|
|
VisualLoggerBattleRenderer(Canvas & target, BattleInterface & owner) : target(target), owner(owner)
|
|
{
|
|
}
|
|
|
|
virtual void drawText(BattleHex hex, std::vector<std::string> texts) override
|
|
{
|
|
const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
|
|
|
|
target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts);
|
|
}
|
|
};
|
|
|
|
void BattleRenderer::execute(BattleRenderer::RendererRef targetCanvas)
|
|
{
|
|
collectObjects();
|
|
sortObjects();
|
|
renderObjects(targetCanvas);
|
|
|
|
logVisual->visualize(VisualLoggerBattleRenderer(targetCanvas, owner));
|
|
}
|