1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

Minor refactoring of BattleHero class

This commit is contained in:
Ivan Savenko 2022-12-21 01:08:56 +02:00
parent a57eec23e6
commit 6138ff1670
3 changed files with 29 additions and 51 deletions

View File

@ -123,44 +123,11 @@ BattleInterface::BattleInterface(const CCreatureSet *army1, const CCreatureSet *
actionsController.reset( new BattleActionsController(*this));
effectsController.reset(new BattleEffectsController(*this));
//loading hero animations
if(hero1) // attacking hero
{
std::string battleImage;
if(!hero1->type->battleImage.empty())
{
battleImage = hero1->type->battleImage;
}
else
{
if(hero1->sex)
battleImage = hero1->type->heroClass->imageBattleFemale;
else
battleImage = hero1->type->heroClass->imageBattleMale;
}
attackingHero = std::make_shared<BattleHero>(battleImage, false, hero1->tempOwner, hero1->tempOwner == curInt->playerID ? hero1 : nullptr, *this);
}
attackingHero = std::make_shared<BattleHero>(*this, hero1, false);
if(hero2) // defending hero
{
std::string battleImage;
if(!hero2->type->battleImage.empty())
{
battleImage = hero2->type->battleImage;
}
else
{
if(hero2->sex)
battleImage = hero2->type->heroClass->imageBattleFemale;
else
battleImage = hero2->type->heroClass->imageBattleMale;
}
defendingHero = std::make_shared<BattleHero>(battleImage, true, hero2->tempOwner, hero2->tempOwner == curInt->playerID ? hero2 : nullptr, *this);
}
defendingHero = std::make_shared<BattleHero>(*this, hero2, true);
obstacleController.reset(new BattleObstacleController(*this));

View File

@ -40,6 +40,7 @@
#include "../../lib/CGameState.h"
#include "../../lib/CGeneralTextHandler.h"
#include "../../lib/CTownHandler.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/NetPacks.h"
#include "../../lib/StartInfo.h"
#include "../../lib/CondSh.h"
@ -168,7 +169,7 @@ void BattleHero::render(Canvas & canvas)
Point heroPosition = pos.center() - heroFrame->dimensions() / 2;
Point flagPosition = pos.center() - flagFrame->dimensions() / 2;
if(flip)
if(defender)
flagPosition += Point(-4, -41);
else
flagPosition += Point(4, -41);
@ -234,10 +235,10 @@ void BattleHero::clickLeft(tribool down, bool previousState)
if(boost::logic::indeterminate(down))
return;
if(!myHero || down || !owner.myTurn)
if(!hero || down || !owner.myTurn)
return;
if(owner.getCurrentPlayerInterface()->cb->battleCanCastSpell(myHero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
if(owner.getCurrentPlayerInterface()->cb->battleCanCastSpell(hero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
{
BattleHex hoveredHex = owner.fieldController->getHoveredHex();
//do nothing when any hex is hovered - hero's animation overlaps battlefield
@ -246,7 +247,7 @@ void BattleHero::clickLeft(tribool down, bool previousState)
CCS->curh->set(Cursor::Map::POINTER);
GH.pushIntT<CSpellWindow>(myHero, owner.getCurrentPlayerInterface());
GH.pushIntT<CSpellWindow>(hero, owner.getCurrentPlayerInterface());
}
}
@ -256,13 +257,13 @@ void BattleHero::clickRight(tribool down, bool previousState)
return;
Point windowPosition;
windowPosition.x = (!flip) ? owner.pos.topLeft().x + 1 : owner.pos.topRight().x - 79;
windowPosition.x = (!defender) ? owner.pos.topLeft().x + 1 : owner.pos.topRight().x - 79;
windowPosition.y = owner.pos.y + 135;
InfoAboutHero targetHero;
if(down && (owner.myTurn || settings["session"]["spectate"].Bool()))
{
auto h = flip ? owner.defendingHeroInstance : owner.attackingHeroInstance;
auto h = defender ? owner.defendingHeroInstance : owner.attackingHeroInstance;
targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
GH.pushIntT<HeroInfoWindow>(targetHero, &windowPosition);
}
@ -278,9 +279,9 @@ void BattleHero::switchToNextPhase()
copy();
}
BattleHero::BattleHero(const std::string & animationPath, bool flipG, PlayerColor player, const CGHeroInstance * hero, const BattleInterface & owner):
flip(flipG),
myHero(hero),
BattleHero::BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender):
defender(defender),
hero(hero),
owner(owner),
phase(EHeroAnimType::HOLDING),
nextPhase(EHeroAnimType::HOLDING),
@ -288,24 +289,34 @@ BattleHero::BattleHero(const std::string & animationPath, bool flipG, PlayerColo
currentFrame(0.f),
flagCurrentFrame(0.f)
{
std::string animationPath;
if(!hero->type->battleImage.empty())
animationPath = hero->type->battleImage;
else
if(hero->sex)
animationPath = hero->type->heroClass->imageBattleFemale;
else
animationPath = hero->type->heroClass->imageBattleMale;
animation = std::make_shared<CAnimation>(animationPath);
animation->preload();
pos.w = 64;
pos.h = 136;
pos.x = owner.pos.x + (flipG ? (owner.pos.w - pos.w) : 0);
pos.x = owner.pos.x + (defender ? (owner.pos.w - pos.w) : 0);
pos.y = owner.pos.y;
if(flip)
if(defender)
animation->verticalFlip();
if(flip)
if(defender)
flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
else
flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
flagAnimation->preload();
flagAnimation->playerColored(player);
flagAnimation->playerColored(hero->tempOwner);
addUsedEvents(LCLICK | RCLICK | HOVER);

View File

@ -79,14 +79,14 @@ public:
/// Hero battle animation
class BattleHero : public CIntObject
{
bool flip; //false if it's attacking hero, true otherwise
bool defender;
CFunctionList<void()> phaseFinishedCallback;
std::shared_ptr<CAnimation> animation;
std::shared_ptr<CAnimation> flagAnimation;
const CGHeroInstance * myHero; //this animation's hero instance
const CGHeroInstance * hero; //this animation's hero instance
const BattleInterface & owner; //battle interface to which this animation is assigned
EHeroAnimType phase; //stage of animation
@ -111,7 +111,7 @@ public:
void hover(bool on) override;
void clickLeft(tribool down, bool previousState) override; //call-in
void clickRight(tribool down, bool previousState) override; //call-in
BattleHero(const std::string & animationPath, bool filpG, PlayerColor player, const CGHeroInstance * hero, const BattleInterface & owner);
BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender);
};
class HeroInfoWindow : public CWindowObject