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-12-21 17:02:53 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/Canvas.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
|
|
|
{
|
2023-03-26 19:15:26 +02:00
|
|
|
std::string animationName = oi.getAnimation();
|
2022-12-01 22:06:42 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-26 18:57:21 +02:00
|
|
|
void BattleObstacleController::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
|
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
for(const auto & oi : obstacles)
|
2023-03-26 18:57:21 +02:00
|
|
|
{
|
|
|
|
auto & obstacle = oi.data["obstacle"];
|
|
|
|
|
|
|
|
if (!obstacle.isStruct())
|
|
|
|
{
|
|
|
|
logGlobal->error("I don't know how to animate removal of this obstacle");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto animation = std::make_shared<CAnimation>(obstacle["appearAnimation"].String());
|
|
|
|
animation->preload();
|
|
|
|
|
|
|
|
auto first = animation->getImage(0, 0);
|
|
|
|
if(!first)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//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, obstacle);
|
|
|
|
//AFAIK, in H3 there is no sound of obstacle removal
|
|
|
|
owner.stacksController->addNewAnim(new EffectAnimation(owner, obstacle["appearAnimation"].String(), whereTo, obstacle["position"].Integer(), 0, true));
|
|
|
|
|
|
|
|
obstacleAnimations.erase(oi.id);
|
|
|
|
//so when multiple obstacles are removed, they show up one after another
|
|
|
|
owner.waitForAnimations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
for(const auto & oi : obstacles)
|
2022-11-17 19:36:25 +02:00
|
|
|
{
|
2023-03-21 00:22:31 +02:00
|
|
|
auto side = owner.curInt->cb->playerToSide(owner.curInt->playerID);
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
if(!oi->visibleForSide(side.value(), owner.curInt->cb->battleHasNativeStack(side.value())))
|
2023-03-21 00:22:31 +02:00
|
|
|
continue;
|
|
|
|
|
2023-03-26 19:15:26 +02:00
|
|
|
auto animation = std::make_shared<CAnimation>(oi->getAppearAnimation());
|
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);
|
2023-03-26 19:15:26 +02:00
|
|
|
CCS->soundh->playSound( oi->getAppearSound() );
|
|
|
|
owner.stacksController->addNewAnim(new EffectAnimation(owner, oi->getAppearAnimation(), whereTo, oi->pos));
|
2022-12-01 22:06:42 +02:00
|
|
|
|
|
|
|
//so when multiple obstacles are added, they show up one after another
|
2023-03-19 01:40:31 +02:00
|
|
|
owner.waitForAnimations();
|
2022-12-01 22:06:42 +02:00
|
|
|
|
2023-03-26 19:15:26 +02:00
|
|
|
loadObstacleImage(*oi);
|
2022-12-01 22:06:42 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2023-06-06 14:53:14 +02:00
|
|
|
bool isForeground = obstacle->obstacleType == CObstacleInstance::USUAL && obstacle->getInfo().isForegroundObstacle;
|
|
|
|
|
|
|
|
auto layer = isForeground ? EBattleFieldLayer::OBSTACLES_FG : EBattleFieldLayer::OBSTACLES_BG;
|
|
|
|
|
|
|
|
renderer.insert(layer, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 22:38:57 +02:00
|
|
|
void BattleObstacleController::tick(uint32_t msPassed)
|
2022-12-21 17:02:53 +02:00
|
|
|
{
|
2023-05-13 22:38:57 +02:00
|
|
|
timePassed += msPassed / 1000.f;
|
2022-12-21 17:02:53 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2023-03-26 18:57:21 +02:00
|
|
|
|
|
|
|
Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const JsonNode & obstacle)
|
|
|
|
{
|
|
|
|
auto animationYOffset = obstacle["animationYOffset"].Integer();
|
|
|
|
auto offset = image->height() % 42;
|
|
|
|
|
|
|
|
if(obstacle["needAnimationOffsetFix"].Bool() && offset > 37)
|
|
|
|
animationYOffset -= 42;
|
|
|
|
|
|
|
|
offset += animationYOffset;
|
|
|
|
|
|
|
|
Rect r = owner.fieldController->hexPositionLocal(obstacle["position"].Integer());
|
|
|
|
r.y += 42 - image->height() + offset;
|
|
|
|
|
|
|
|
return r.topLeft();
|
|
|
|
}
|