mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-26 22:57:00 +02:00
428fb832c6
- obstacles now have "foreground" field - if "foreground" field set, obstacle will appear on top of other objects, such as units - if "foreground" is not set, obstacle will appear below units - updated schema and cleared up obstacles config
216 lines
6.5 KiB
C++
216 lines
6.5 KiB
C++
/*
|
|
* BattleObstacleController.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 "BattleObstacleController.h"
|
|
|
|
#include "BattleInterface.h"
|
|
#include "BattleFieldController.h"
|
|
#include "BattleAnimationClasses.h"
|
|
#include "BattleStacksController.h"
|
|
#include "BattleRenderer.h"
|
|
#include "CreatureAnimation.h"
|
|
|
|
#include "../CMusicHandler.h"
|
|
#include "../CGameInfo.h"
|
|
#include "../CPlayerInterface.h"
|
|
#include "../gui/CGuiHandler.h"
|
|
#include "../render/Canvas.h"
|
|
|
|
#include "../../CCallback.h"
|
|
#include "../../lib/battle/CObstacleInstance.h"
|
|
#include "../../lib/ObstacleHandler.h"
|
|
|
|
BattleObstacleController::BattleObstacleController(BattleInterface & owner):
|
|
owner(owner),
|
|
timePassed(0.f)
|
|
{
|
|
auto obst = owner.curInt->cb->battleGetAllObstacles();
|
|
for(auto & elem : obst)
|
|
{
|
|
if ( elem->obstacleType == CObstacleInstance::MOAT )
|
|
continue; // handled by siege controller;
|
|
loadObstacleImage(*elem);
|
|
}
|
|
}
|
|
|
|
void BattleObstacleController::loadObstacleImage(const CObstacleInstance & oi)
|
|
{
|
|
std::string animationName = oi.getAnimation();
|
|
|
|
if (animationsCache.count(animationName) == 0)
|
|
{
|
|
if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
|
{
|
|
// obstacle uses single bitmap image for animations
|
|
auto animation = std::make_shared<CAnimation>();
|
|
animation->setCustom(animationName, 0, 0);
|
|
animationsCache[animationName] = animation;
|
|
animation->preload();
|
|
}
|
|
else
|
|
{
|
|
auto animation = std::make_shared<CAnimation>(animationName);
|
|
animationsCache[animationName] = animation;
|
|
animation->preload();
|
|
}
|
|
}
|
|
obstacleAnimations[oi.uniqueID] = animationsCache[animationName];
|
|
}
|
|
|
|
void BattleObstacleController::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
|
|
{
|
|
for(const auto & oi : obstacles)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
void BattleObstacleController::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles)
|
|
{
|
|
for(const auto & oi : obstacles)
|
|
{
|
|
auto side = owner.curInt->cb->playerToSide(owner.curInt->playerID);
|
|
|
|
if(!oi->visibleForSide(side.value(), owner.curInt->cb->battleHasNativeStack(side.value())))
|
|
continue;
|
|
|
|
auto animation = std::make_shared<CAnimation>(oi->getAppearAnimation());
|
|
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, *oi);
|
|
CCS->soundh->playSound( oi->getAppearSound() );
|
|
owner.stacksController->addNewAnim(new EffectAnimation(owner, oi->getAppearAnimation(), whereTo, oi->pos));
|
|
|
|
//so when multiple obstacles are added, they show up one after another
|
|
owner.waitForAnimations();
|
|
|
|
loadObstacleImage(*oi);
|
|
}
|
|
}
|
|
|
|
void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas)
|
|
{
|
|
//Blit absolute obstacles
|
|
for(auto & oi : owner.curInt->cb->battleGetAllObstacles())
|
|
{
|
|
if(oi->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
|
{
|
|
auto img = getObstacleImage(*oi);
|
|
if(img)
|
|
canvas.draw(img, Point(oi->getInfo().width, oi->getInfo().height));
|
|
}
|
|
}
|
|
}
|
|
|
|
void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
|
|
{
|
|
for (auto obstacle : owner.curInt->cb->battleGetAllObstacles())
|
|
{
|
|
if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
|
|
continue;
|
|
|
|
if (obstacle->obstacleType == CObstacleInstance::MOAT)
|
|
continue;
|
|
|
|
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 ){
|
|
auto img = getObstacleImage(*obstacle);
|
|
if(img)
|
|
{
|
|
Point p = getObstaclePosition(img, *obstacle);
|
|
canvas.draw(img, p);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
void BattleObstacleController::tick(uint32_t msPassed)
|
|
{
|
|
timePassed += msPassed / 1000.f;
|
|
}
|
|
|
|
std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
|
|
{
|
|
int framesCount = timePassed * AnimationControls::getObstaclesSpeed();
|
|
std::shared_ptr<CAnimation> animation;
|
|
|
|
// obstacle is not loaded yet, don't show anything
|
|
if (obstacleAnimations.count(oi.uniqueID) == 0)
|
|
return nullptr;
|
|
|
|
animation = obstacleAnimations[oi.uniqueID];
|
|
assert(animation);
|
|
|
|
if(animation)
|
|
{
|
|
int frameIndex = framesCount % animation->size(0);
|
|
return animation->getImage(frameIndex, 0);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
|
|
{
|
|
int offset = obstacle.getAnimationYOffset(image->height());
|
|
|
|
Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
|
|
r.y += 42 - image->height() + offset;
|
|
|
|
return r.topLeft();
|
|
}
|
|
|
|
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();
|
|
}
|