1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

code review

This commit is contained in:
Laserlicht
2025-06-24 13:48:36 +02:00
parent b95293f254
commit 30521bbc8f
6 changed files with 32 additions and 16 deletions

View File

@@ -56,8 +56,8 @@ public:
/// returns true if specified object is the currently active hero
virtual bool isActiveHero(const CGObjectInstance* obj) const = 0;
/// returns direction of attacker if specified object is a monster and currently attacked
virtual int monsterAttacked(const CGObjectInstance * obj) const = 0;
/// Returns moveDir of hero that attacked this wandering monster, or -1 on failure
virtual int attackedMonsterDirection(const CGObjectInstance * wanderingMonster) const = 0;
virtual size_t objectGroupIndex(ObjectInstanceID objectID) const = 0;
virtual Point objectImageOffset(ObjectInstanceID objectID, const int3 & coordinates) const = 0;

View File

@@ -420,6 +420,19 @@ std::shared_ptr<CAnimation> MapRendererObjects::getAnimation(const AnimationPath
return ret;
}
std::shared_ptr<IImage> MapRendererObjects::getImage(const ImagePath & filename) const
{
auto it = images.find(filename);
if(it != images.end())
return it->second;
auto ret = ENGINE->renderHandler().loadImage(filename, EImageBlitMode::SIMPLE);
images[filename] = ret;
return ret;
}
std::shared_ptr<CAnimation> MapRendererObjects::getFlagAnimation(const CGObjectInstance* obj)
{
//TODO: relocate to config file?
@@ -456,7 +469,7 @@ std::shared_ptr<CAnimation> MapRendererObjects::getOverlayAnimation(const CGObje
return nullptr;
}
std::shared_ptr<IImage> MapRendererObjects::getImage(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation>& animation) const
std::shared_ptr<IImage> MapRendererObjects::getImageToRender(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation>& animation) const
{
if(!animation)
return nullptr;
@@ -466,7 +479,7 @@ std::shared_ptr<IImage> MapRendererObjects::getImage(IMapRendererContext & conte
if(animation->size(groupIndex) == 0)
return nullptr;
auto attackerPos = context.monsterAttacked(obj);
auto attackerPos = context.attackedMonsterDirection(obj);
if(attackerPos != -1)
{
const auto * creature = dynamic_cast<const CArmedInstance *>(obj);
@@ -474,10 +487,7 @@ std::shared_ptr<IImage> MapRendererObjects::getImage(IMapRendererContext & conte
auto dir = std::vector<int>({1, 2, 7, 8});
ImagePath imgPath = std::count(dir.begin(), dir.end(), attackerPos) ? creatureType->mapAttackFromRight : creatureType->mapAttackFromLeft;
if(!imgPath.empty())
{
auto img = ENGINE->renderHandler().loadImage(imgPath, EImageBlitMode::SIMPLE);
return img;
}
return getImage(imgPath);
}
size_t frameIndex = context.objectImageIndex(obj->id, animation->size(groupIndex));
@@ -516,9 +526,9 @@ void MapRendererObjects::renderImage(IMapRendererContext & context, Canvas & tar
void MapRendererObjects::renderObject(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * instance)
{
renderImage(context, target, coordinates, instance, getImage(context, instance, getBaseAnimation(instance)));
renderImage(context, target, coordinates, instance, getImage(context, instance, getFlagAnimation(instance)));
renderImage(context, target, coordinates, instance, getImage(context, instance, getOverlayAnimation(instance)));
renderImage(context, target, coordinates, instance, getImageToRender(context, instance, getBaseAnimation(instance)));
renderImage(context, target, coordinates, instance, getImageToRender(context, instance, getFlagAnimation(instance)));
renderImage(context, target, coordinates, instance, getImageToRender(context, instance, getOverlayAnimation(instance)));
}
void MapRendererObjects::renderTile(IMapRendererContext & context, Canvas & target, const int3 & coordinates)

View File

@@ -73,14 +73,16 @@ public:
class MapRendererObjects
{
std::map<AnimationPath, std::shared_ptr<CAnimation>> animations;
mutable std::map<ImagePath, std::shared_ptr<IImage>> images;
std::shared_ptr<CAnimation> getBaseAnimation(const CGObjectInstance * obj);
std::shared_ptr<CAnimation> getFlagAnimation(const CGObjectInstance * obj);
std::shared_ptr<CAnimation> getOverlayAnimation(const CGObjectInstance * obj);
std::shared_ptr<CAnimation> getAnimation(const AnimationPath & filename, bool generateMovementGroups, bool enableOverlay);
std::shared_ptr<IImage> getImage(const ImagePath & filename) const;
std::shared_ptr<IImage> getImage(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation> & animation) const;
std::shared_ptr<IImage> getImageToRender(IMapRendererContext & context, const CGObjectInstance * obj, const std::shared_ptr<CAnimation> & animation) const;
void renderImage(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * object, const std::shared_ptr<IImage> & image);
void renderObject(IMapRendererContext & context, Canvas & target, const int3 & coordinates, const CGObjectInstance * obj);

View File

@@ -87,13 +87,13 @@ bool MapRendererBaseContext::isActiveHero(const CGObjectInstance * obj) const
return false;
}
int MapRendererBaseContext::monsterAttacked(const CGObjectInstance * obj) const
int MapRendererBaseContext::attackedMonsterDirection(const CGObjectInstance * wanderingMonster) const
{
if(obj->ID != Obj::MONSTER)
if(wanderingMonster->ID != Obj::MONSTER)
return -1;
for(auto & battle : GAME->interface()->cb->getActiveBattles())
if(obj->pos == battle.second->getBattle()->getLocation())
if(wanderingMonster->pos == battle.second->getBattle()->getLocation())
return battle.second->getBattle()->getSideHero(BattleSide::ATTACKER)->moveDir;
return -1;

View File

@@ -36,7 +36,7 @@ public:
bool tileAnimated(const int3 & coordinates) const override;
bool isActiveHero(const CGObjectInstance* obj) const override;
int monsterAttacked(const CGObjectInstance * obj) const override;
int attackedMonsterDirection(const CGObjectInstance * wanderingMonster) const override;
const TerrainTile & getMapTile(const int3 & coordinates) const override;
const MapObjectsList & getObjects(const int3 & coordinates) const override;

View File

@@ -129,6 +129,10 @@ In order to make functional creature you also need:
// Large icon for this creature, used for example in town screen
"iconLarge" : "",
// Optional. Images of creatures when attacked on adventure map
"mapAttackFromRight": "",
"mapAttackFromLeft": "",
// animation parameters
// how often creature should play idle animation