From 7c4e5242ba77635c6e4aee4075d7e9a6c02ac09c Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 16 Oct 2016 14:56:52 +0000 Subject: (svn r27664) -Codechange: Deduplicate code for animating effect vehicles. --- src/effectvehicle.cpp | 57 ++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/effectvehicle.cpp b/src/effectvehicle.cpp index 209b78d2e..c871ac09a 100644 --- a/src/effectvehicle.cpp +++ b/src/effectvehicle.cpp @@ -22,6 +22,22 @@ #include "safeguards.h" +/** + * Increment the sprite unless it has reached the end of the animation. + * @param v Vehicle to increment sprite of. + * @param last Last sprite of animation. + * @return true if the sprite was incremented, false if the end was reached. + */ +static bool IncrementSprite(EffectVehicle *v, SpriteID last) +{ + if (v->cur_image != last) { + v->cur_image++; + return true; + } else { + return false; + } +} + static void ChimneySmokeInit(EffectVehicle *v) { uint32 r = Random(); @@ -40,9 +56,7 @@ static bool ChimneySmokeTick(EffectVehicle *v) return false; } - if (v->cur_image != SPR_CHIMNEY_SMOKE_7) { - v->cur_image++; - } else { + if (!IncrementSprite(v, SPR_CHIMNEY_SMOKE_7)) { v->cur_image = SPR_CHIMNEY_SMOKE_0; } v->progress = 7; @@ -70,9 +84,7 @@ static bool SteamSmokeTick(EffectVehicle *v) } if ((v->progress & 0xF) == 4) { - if (v->cur_image != SPR_STEAM_SMOKE_4) { - v->cur_image++; - } else { + if (!IncrementSprite(v, SPR_STEAM_SMOKE_4)) { delete v; return false; } @@ -98,13 +110,11 @@ static bool DieselSmokeTick(EffectVehicle *v) v->z_pos++; v->UpdatePositionAndViewport(); } else if ((v->progress & 7) == 1) { - if (v->cur_image != SPR_DIESEL_SMOKE_5) { - v->cur_image++; - v->UpdatePositionAndViewport(); - } else { + if (!IncrementSprite(v, SPR_DIESEL_SMOKE_5)) { delete v; return false; } + v->UpdatePositionAndViewport(); } return true; @@ -122,13 +132,12 @@ static bool ElectricSparkTick(EffectVehicle *v) v->progress++; } else { v->progress = 0; - if (v->cur_image != SPR_ELECTRIC_SPARK_5) { - v->cur_image++; - v->UpdatePositionAndViewport(); - } else { + + if (!IncrementSprite(v, SPR_ELECTRIC_SPARK_5)) { delete v; return false; } + v->UpdatePositionAndViewport(); } return true; @@ -152,9 +161,7 @@ static bool SmokeTick(EffectVehicle *v) } if ((v->progress & 0xF) == 4) { - if (v->cur_image != SPR_SMOKE_4) { - v->cur_image++; - } else { + if (!IncrementSprite(v, SPR_SMOKE_4)) { delete v; return false; } @@ -176,13 +183,11 @@ static bool ExplosionLargeTick(EffectVehicle *v) { v->progress++; if ((v->progress & 3) == 0) { - if (v->cur_image != SPR_EXPLOSION_LARGE_F) { - v->cur_image++; - v->UpdatePositionAndViewport(); - } else { + if (!IncrementSprite(v, SPR_EXPLOSION_LARGE_F)) { delete v; return false; } + v->UpdatePositionAndViewport(); } return true; @@ -198,9 +203,7 @@ static bool BreakdownSmokeTick(EffectVehicle *v) { v->progress++; if ((v->progress & 7) == 0) { - if (v->cur_image != SPR_BREAKDOWN_SMOKE_3) { - v->cur_image++; - } else { + if (!IncrementSprite(v, SPR_BREAKDOWN_SMOKE_3)) { v->cur_image = SPR_BREAKDOWN_SMOKE_0; } v->UpdatePositionAndViewport(); @@ -225,13 +228,11 @@ static bool ExplosionSmallTick(EffectVehicle *v) { v->progress++; if ((v->progress & 3) == 0) { - if (v->cur_image != SPR_EXPLOSION_SMALL_B) { - v->cur_image++; - v->UpdatePositionAndViewport(); - } else { + if (!IncrementSprite(v, SPR_EXPLOSION_SMALL_B)) { delete v; return false; } + v->UpdatePositionAndViewport(); } return true; -- cgit v1.2.3-54-g00ecf