2022-11-17 13:21:03 +02:00
|
|
|
/*
|
2022-12-11 23:16:23 +02:00
|
|
|
* BattleObstacleController.cpp, part of VCMI engine
|
2022-11-17 13:21:03 +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"
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "BattleObstacleController.h"
|
2022-11-28 16:43:38 +02:00
|
|
|
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "BattleInterface.h"
|
|
|
|
#include "BattleFieldController.h"
|
|
|
|
#include "BattleAnimationClasses.h"
|
|
|
|
#include "BattleStacksController.h"
|
|
|
|
#include "BattleRenderer.h"
|
2022-12-21 17:02:53 +02:00
|
|
|
#include "CreatureAnimation.h"
|
2022-11-28 16:43:38 +02:00
|
|
|
|
2022-12-25 21:35:13 +02:00
|
|
|
#include "../CMusicHandler.h"
|
|
|
|
#include "../CGameInfo.h"
|
2022-11-17 19:36:25 +02:00
|
|
|
#include "../CPlayerInterface.h"
|
2022-11-28 16:43:38 +02:00
|
|
|
#include "../gui/CAnimation.h"
|
2022-12-11 22:09:57 +02:00
|
|
|
#include "../gui/Canvas.h"
|
2022-12-21 17:02:53 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2022-11-28 16:43:38 +02:00
|
|
|
|
2022-11-17 19:36:25 +02:00
|
|
|
#include "../../CCallback.h"
|
|
|
|
#include "../../lib/battle/CObstacleInstance.h"
|
|
|
|
#include "../../lib/ObstacleHandler.h"
|
2022-11-17 13:21:03 +02:00
|
|
|
|
2022-12-13 13:58:16 +02:00
|
|
|
BattleObstacleController::BattleObstacleController(BattleInterface & owner):
|
2022-12-21 17:02:53 +02:00
|
|
|
owner(owner),
|
|
|
|
timePassed(0.f)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-13 13:58:16 +02:00
|
|
|
auto obst = owner.curInt->cb->battleGetAllObstacles();
|
2022-11-17 19:36:25 +02:00
|
|
|
for(auto & elem : obst)
|
|
|
|
{
|
2022-12-01 22:06:42 +02:00
|
|
|
if ( elem->obstacleType == CObstacleInstance::MOAT )
|
|
|
|
continue; // handled by siege controller;
|
|
|
|
loadObstacleImage(*elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleObstacleController::loadObstacleImage(const CObstacleInstance & oi)
|
2022-12-01 22:06:42 +02:00
|
|
|
{
|
|
|
|
std::string animationName;
|
|
|
|
|
|
|
|
if (auto spellObstacle = dynamic_cast<const SpellCreatedObstacle*>(&oi))
|
|
|
|
{
|
|
|
|
animationName = spellObstacle->animation;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert( oi.obstacleType == CObstacleInstance::USUAL || oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE);
|
|
|
|
animationName = oi.getInfo().animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animationsCache.count(animationName) == 0)
|
|
|
|
{
|
|
|
|
if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-12 00:05:00 +02:00
|
|
|
// obstacle uses single bitmap image for animations
|
2022-12-01 22:06:42 +02:00
|
|
|
auto animation = std::make_shared<CAnimation>();
|
|
|
|
animation->setCustom(animationName, 0, 0);
|
|
|
|
animationsCache[animationName] = animation;
|
2022-12-12 00:05:00 +02:00
|
|
|
animation->preload();
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
2022-12-01 22:06:42 +02:00
|
|
|
else
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-01 22:06:42 +02:00
|
|
|
auto animation = std::make_shared<CAnimation>(animationName);
|
|
|
|
animationsCache[animationName] = animation;
|
|
|
|
animation->preload();
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 22:06:42 +02:00
|
|
|
obstacleAnimations[oi.uniqueID] = animationsCache[animationName];
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleObstacleController::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-01 22:06:42 +02:00
|
|
|
for (auto const & oi : obstacles)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-01 22:06:42 +02:00
|
|
|
auto spellObstacle = dynamic_cast<const SpellCreatedObstacle*>(oi.get());
|
|
|
|
|
|
|
|
if (!spellObstacle)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-01 22:06:42 +02:00
|
|
|
logGlobal->error("I don't know how to animate appearing obstacle of type %d", (int)oi->obstacleType);
|
|
|
|
continue;
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 01:55:09 +02:00
|
|
|
auto animation = std::make_shared<CAnimation>(spellObstacle->appearAnimation);
|
2022-12-01 22:06:42 +02:00
|
|
|
animation->preload();
|
2022-11-17 19:36:25 +02:00
|
|
|
|
2022-12-01 22:06:42 +02:00
|
|
|
auto first = animation->getImage(0, 0);
|
|
|
|
if(!first)
|
|
|
|
continue;
|
2022-11-17 19:36:25 +02:00
|
|
|
|
2022-12-01 22:06:42 +02:00
|
|
|
//we assume here that effect graphics have the same size as the usual obstacle image
|
|
|
|
// -> if we know how to blit obstacle, let's blit the effect in the same place
|
|
|
|
Point whereTo = getObstaclePosition(first, *oi);
|
2022-12-25 21:35:13 +02:00
|
|
|
CCS->soundh->playSound( spellObstacle->appearSound );
|
2022-12-25 21:39:55 +02:00
|
|
|
owner.stacksController->addNewAnim(new EffectAnimation(owner, spellObstacle->appearAnimation, whereTo, oi->pos));
|
2022-12-01 22:06:42 +02:00
|
|
|
|
|
|
|
//so when multiple obstacles are added, they show up one after another
|
2022-12-10 00:25:11 +02:00
|
|
|
owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
|
2022-12-01 22:06:42 +02:00
|
|
|
|
|
|
|
loadObstacleImage(*spellObstacle);
|
|
|
|
}
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 14:16:01 +02:00
|
|
|
void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
|
|
|
//Blit absolute obstacles
|
2022-12-13 13:58:16 +02:00
|
|
|
for(auto & oi : owner.curInt->cb->battleGetAllObstacles())
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
|
|
|
if(oi->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
|
|
|
{
|
|
|
|
auto img = getObstacleImage(*oi);
|
|
|
|
if(img)
|
2023-01-05 14:16:01 +02:00
|
|
|
canvas.draw(img, Point(oi->getInfo().width, oi->getInfo().height));
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-13 13:58:16 +02:00
|
|
|
for (auto obstacle : owner.curInt->cb->battleGetAllObstacles())
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-11-25 00:26:14 +02:00
|
|
|
if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
|
|
|
continue;
|
|
|
|
|
2022-11-27 02:26:02 +02:00
|
|
|
if (obstacle->obstacleType == CObstacleInstance::MOAT)
|
2022-11-25 00:26:14 +02:00
|
|
|
continue;
|
|
|
|
|
2022-12-13 15:10:31 +02:00
|
|
|
renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
|
2022-12-02 17:49:38 +02:00
|
|
|
auto img = getObstacleImage(*obstacle);
|
|
|
|
if(img)
|
|
|
|
{
|
|
|
|
Point p = getObstaclePosition(img, *obstacle);
|
2022-12-11 22:09:57 +02:00
|
|
|
canvas.draw(img, p);
|
2022-12-02 17:49:38 +02:00
|
|
|
}
|
|
|
|
});
|
2022-11-17 19:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 17:02:53 +02:00
|
|
|
void BattleObstacleController::update()
|
|
|
|
{
|
|
|
|
timePassed += GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2022-12-21 17:02:53 +02:00
|
|
|
int framesCount = timePassed * AnimationControls::getObstaclesSpeed();
|
2022-11-17 19:36:25 +02:00
|
|
|
std::shared_ptr<CAnimation> animation;
|
|
|
|
|
2022-12-17 19:37:00 +02:00
|
|
|
// obstacle is not loaded yet, don't show anything
|
2022-12-01 22:06:42 +02:00
|
|
|
if (obstacleAnimations.count(oi.uniqueID) == 0)
|
2022-12-17 19:37:00 +02:00
|
|
|
return nullptr;
|
2022-11-17 19:36:25 +02:00
|
|
|
|
2022-12-01 22:06:42 +02:00
|
|
|
animation = obstacleAnimations[oi.uniqueID];
|
|
|
|
assert(animation);
|
|
|
|
|
2022-11-17 19:36:25 +02:00
|
|
|
if(animation)
|
|
|
|
{
|
2022-12-21 17:02:53 +02:00
|
|
|
int frameIndex = framesCount % animation->size(0);
|
2022-11-17 19:36:25 +02:00
|
|
|
return animation->getImage(frameIndex, 0);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
|
|
|
int offset = obstacle.getAnimationYOffset(image->height());
|
|
|
|
|
2023-01-05 14:16:01 +02:00
|
|
|
Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
|
2022-11-17 19:36:25 +02:00
|
|
|
r.y += 42 - image->height() + offset;
|
|
|
|
|
|
|
|
return r.topLeft();
|
|
|
|
}
|