Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make speed of fade-in & fade-out animations for AI-controlled heroes dependent on the AI speed setting #8114

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/fheroes2/ai/ai_hero_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeOut();
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier() );
}

hero.Scout( targetIndex );
Expand All @@ -264,7 +264,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeIn();
hero.FadeIn( Game::AIHeroAnimSpeedMultiplier() );
}

AI::Get().HeroesActionComplete( hero, targetIndex, hero.getObjectTypeUnderHero() );
Expand All @@ -283,8 +283,12 @@ namespace

if ( playSound ) {
AudioManager::PlaySound( M82::KILLFADE );

hero.FadeOut();
}
else {
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier() );
}
hero.FadeOut();
}

hero.Dismiss( reason );
Expand Down Expand Up @@ -847,7 +851,7 @@ namespace
if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
// AI-controlled hero cannot activate Stone Liths from the same tile, but should move to this tile from some
// other tile first, so there is no need to re-center the game area on the hero before his disappearance
hero.FadeOut();
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier() );
}

hero.Scout( indexTo );
Expand All @@ -856,7 +860,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeIn();
hero.FadeIn( Game::AIHeroAnimSpeedMultiplier() );
}

hero.ActionNewPosition( false );
Expand Down Expand Up @@ -912,7 +916,7 @@ namespace
if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
// AI-controlled hero cannot activate Whirlpool from the same tile, but should move to this tile from some
// other tile first, so there is no need to re-center the game area on the hero before his disappearance
hero.FadeOut();
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier() );
}

hero.Scout( indexTo );
Expand All @@ -923,7 +927,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeIn();
hero.FadeIn( Game::AIHeroAnimSpeedMultiplier() );
}

hero.ActionNewPosition( false );
Expand Down Expand Up @@ -1573,7 +1577,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeOut( offset );
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier(), offset );
}

hero.Scout( dst_index );
Expand Down Expand Up @@ -1616,7 +1620,7 @@ namespace

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( prevPos );
hero.FadeIn( offset );
hero.FadeIn( Game::AIHeroAnimSpeedMultiplier(), offset );
}

hero.ActionNewPosition( true );
Expand Down Expand Up @@ -2057,7 +2061,7 @@ namespace AI

bool resetHeroSprite = false;
if ( heroAnimationFrameCount > 0 ) {
const int32_t heroMovementSkipValue = Game::AIHeroAnimSkip();
const int32_t heroMovementSkipValue = Game::AIHeroAnimSpeedMultiplier();

gameArea.ShiftCenter( { heroAnimationOffset.x * heroMovementSkipValue, heroAnimationOffset.y * heroMovementSkipValue } );
gameArea.SetRedraw();
Expand Down Expand Up @@ -2087,7 +2091,7 @@ namespace AI
else {
const fheroes2::Point movement( hero.MovementDirection() );
if ( movement != fheroes2::Point() ) { // don't waste resources for no movement
const int32_t heroMovementSkipValue = Game::AIHeroAnimSkip();
const int32_t heroMovementSkipValue = Game::AIHeroAnimSpeedMultiplier();

heroAnimationOffset = movement;
gameArea.ShiftCenter( movement );
Expand Down Expand Up @@ -2136,7 +2140,7 @@ namespace AI

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeOut();
hero.FadeOut( Game::AIHeroAnimSpeedMultiplier() );
}

hero.Scout( targetIndex );
Expand All @@ -2147,7 +2151,7 @@ namespace AI

if ( AIIsShowAnimationForHero( hero, AIGetAllianceColors() ) ) {
Interface::AdventureMap::Get().getGameArea().SetCenter( hero.GetCenter() );
hero.FadeIn();
hero.FadeIn( Game::AIHeroAnimSpeedMultiplier() );
}

hero.ActionNewPosition( false );
Expand Down
8 changes: 6 additions & 2 deletions src/fheroes2/game/game_delays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,17 @@ void Game::UpdateGameSpeed()
delays[BATTLE_FLAGS_DELAY].setDelay( static_cast<uint64_t>( 250 * adjustedIdleAnimationSpeed ) );
}

int Game::HumanHeroAnimSkip()
int Game::HumanHeroAnimSpeedMultiplier()
{
assert( humanHeroMultiplier > 0 );

return humanHeroMultiplier;
}

int Game::AIHeroAnimSkip()
int Game::AIHeroAnimSpeedMultiplier()
{
assert( aiHeroMultiplier > 0 );

return aiHeroMultiplier;
}

Expand Down
7 changes: 5 additions & 2 deletions src/fheroes2/game/game_delays.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ namespace Game

uint32_t ApplyBattleSpeed( uint32_t delay );

int HumanHeroAnimSkip();
int AIHeroAnimSkip();
// Returns the animation speed multiplier for a human-controlled hero.
int HumanHeroAnimSpeedMultiplier();

// Returns the animation speed multiplier for an AI-controlled hero.
int AIHeroAnimSpeedMultiplier();

// Returns true if every of delay type has passed.
bool hasEveryDelayPassed( const std::vector<Game::DelayType> & delayTypes );
Expand Down
4 changes: 2 additions & 2 deletions src/fheroes2/game/game_startgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ fheroes2::GameMode Interface::AdventureMap::HumanTurn( const bool isload )
if ( hero ) {
bool resetHeroSprite = false;
if ( heroAnimationFrameCount > 0 ) {
const int32_t heroMovementSkipValue = Game::HumanHeroAnimSkip();
const int32_t heroMovementSkipValue = Game::HumanHeroAnimSpeedMultiplier();

_gameArea.ShiftCenter( { heroAnimationOffset.x * heroMovementSkipValue, heroAnimationOffset.y * heroMovementSkipValue } );
_gameArea.SetRedraw();
Expand Down Expand Up @@ -1279,7 +1279,7 @@ fheroes2::GameMode Interface::AdventureMap::HumanTurn( const bool isload )
// Do not generate a frame as we are going to do it later.
Interface::AdventureMap::RedrawLocker redrawLocker( Interface::AdventureMap::Get() );

const int32_t heroMovementSkipValue = Game::HumanHeroAnimSkip();
const int32_t heroMovementSkipValue = Game::HumanHeroAnimSpeedMultiplier();

heroAnimationOffset = movement;
_gameArea.ShiftCenter( movement );
Expand Down
19 changes: 17 additions & 2 deletions src/fheroes2/heroes/heroes.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,23 @@ class Heroes final : public HeroBase, public ColorBase

fheroes2::Point getCurrentPixelOffset() const;

void FadeOut( const fheroes2::Point & offset = fheroes2::Point() ) const;
void FadeIn( const fheroes2::Point & offset = fheroes2::Point() ) const;
// Performs a hero fade-out animation with the given speed multiplier and an optional offset
void FadeOut( const int animSpeedMultiplier, const fheroes2::Point & offset = fheroes2::Point() ) const;

// Performs a hero fade-in animation with the given speed multiplier and an optional offset
void FadeIn( const int animSpeedMultiplier, const fheroes2::Point & offset = fheroes2::Point() ) const;

// Performs a hero fade-out animation with an optional offset at the lowest possible speed
void FadeOut( const fheroes2::Point & offset = fheroes2::Point() ) const
{
FadeOut( 1, offset );
}

// Performs a hero fade-in animation with an optional offset at the lowest possible speed
void FadeIn( const fheroes2::Point & offset = fheroes2::Point() ) const
{
FadeIn( 1, offset );
}

void Scout( const int tileIndex ) const;
int GetScoutingDistance() const;
Expand Down
72 changes: 36 additions & 36 deletions src/fheroes2/heroes/heroes_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,71 +483,71 @@ fheroes2::Point Heroes::getCurrentPixelOffset() const
return realOffset;
}

void Heroes::FadeOut( const fheroes2::Point & offset ) const
void Heroes::FadeOut( const int animSpeedMultiplier, const fheroes2::Point & offset /* = fheroes2::Point() */ ) const
{
if ( !isInVisibleMapArea() )
assert( animSpeedMultiplier > 0 );

if ( !isInVisibleMapArea() ) {
return;
}

Interface::AdventureMap & iface = Interface::AdventureMap::Get();
Interface::GameArea & gamearea = iface.getGameArea();

int multiplier = std::max( offset.x < 0 ? -offset.x : offset.x, offset.y < 0 ? -offset.y : offset.y );
if ( multiplier < 1 )
multiplier = 1;

const bool offsetScreen = offset.x != 0 || offset.y != 0;

fheroes2::Display & display = fheroes2::Display::instance();
LocalEvent & le = LocalEvent::Get();
_alphaValue = 255 - 8 * multiplier;

const std::vector<Game::DelayType> delayTypes = { Game::HEROES_FADE_DELAY };
while ( le.HandleEvents( Game::isDelayNeeded( delayTypes ) ) && _alphaValue > 0 ) {
if ( Game::validateAnimationDelay( Game::HEROES_FADE_DELAY ) ) {
if ( offsetScreen ) {
gamearea.ShiftCenter( offset );
}
_alphaValue = 255;

iface.redraw( Interface::REDRAW_GAMEAREA );
while ( le.HandleEvents( Game::isDelayNeeded( { Game::HEROES_FADE_DELAY } ) ) && _alphaValue > 0 ) {
if ( !Game::validateAnimationDelay( Game::HEROES_FADE_DELAY ) ) {
continue;
}

display.render();
_alphaValue -= 8 * multiplier;
if ( offset.x != 0 || offset.y != 0 ) {
gamearea.ShiftCenter( offset );
}

_alphaValue = std::max( 0, _alphaValue - 8 * animSpeedMultiplier );

iface.redraw( Interface::REDRAW_GAMEAREA );

display.render();
}

_alphaValue = 255;
}

void Heroes::FadeIn( const fheroes2::Point & offset ) const
void Heroes::FadeIn( const int animSpeedMultiplier, const fheroes2::Point & offset /* = fheroes2::Point() */ ) const
{
if ( !isInVisibleMapArea() )
assert( animSpeedMultiplier > 0 );

if ( !isInVisibleMapArea() ) {
return;
}

Interface::AdventureMap & iface = Interface::AdventureMap::Get();
Interface::GameArea & gamearea = iface.getGameArea();

int multiplier = std::max( offset.x < 0 ? -offset.x : offset.x, offset.y < 0 ? -offset.y : offset.y );
if ( multiplier < 1 )
multiplier = 1;

const bool offsetScreen = offset.x != 0 || offset.y != 0;

fheroes2::Display & display = fheroes2::Display::instance();
LocalEvent & le = LocalEvent::Get();
_alphaValue = 8 * multiplier;

const std::vector<Game::DelayType> delayTypes = { Game::HEROES_FADE_DELAY };
while ( le.HandleEvents( Game::isDelayNeeded( delayTypes ) ) && _alphaValue < 250 ) {
if ( Game::validateAnimationDelay( Game::HEROES_FADE_DELAY ) ) {
if ( offsetScreen ) {
gamearea.ShiftCenter( offset );
}
_alphaValue = 0;

iface.redraw( Interface::REDRAW_GAMEAREA );
while ( le.HandleEvents( Game::isDelayNeeded( { Game::HEROES_FADE_DELAY } ) ) && _alphaValue < 255 ) {
if ( !Game::validateAnimationDelay( Game::HEROES_FADE_DELAY ) ) {
continue;
}

display.render();
_alphaValue += 8 * multiplier;
if ( offset.x != 0 || offset.y != 0 ) {
gamearea.ShiftCenter( offset );
}

_alphaValue = std::min( _alphaValue + 8 * animSpeedMultiplier, 255 );

iface.redraw( Interface::REDRAW_GAMEAREA );

display.render();
}

_alphaValue = 255;
Expand Down