在c ++中编写一堆类似if语句的漂亮方法吗?

时间:2017-01-28 19:25:36

标签: c++ if-statement

我想为精灵制作动画。要做到这一点,我有一个自动收报机给我一个毫秒的时间。根据我希望精灵显示不同图像的时间。不幸的是,除了一堆if语句之外,我无法想到一个简单而漂亮的方法。

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 0 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 1)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_00.png"));
    }

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 1 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 2)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_01.png"));
    }

    // 10 more of these ...

    if (ticker_.getElapsedTime().asMilliseconds() >= ANIMATION_STEP * 13 &&
           ticker_.getElapsedTime().asMilliseconds() < ANIMATION_STEP * 14)
    {
        sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_13.png"));
    }

5 个答案:

答案 0 :(得分:5)

这样的东西
int frame = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;
if (frame < MAX_FRAMES) {
    std::string filename = "animation/explosion_"
                         + std::to_string(frame) + ".png";
}

答案 1 :(得分:2)

鉴于步骤均匀,您可以计算使用哪一个:

int step = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;

你可以进行整数到字符串的转换来使用它来创建文件名,但这有点笨拙使用和过于严格。相反,您可以填充一组文件名:

// populate array during program initialization 
vector<string> explosion_filenames;

// In your function,
sprite_.setTexture(*game_->getResourceManager().getTexture(explosion_filenames[step]));

甚至更好,一组纹理:

// populate array during program initialization 
vector<whatever_the_texture_type_is> explosion_textures;

// In your function,
sprite_.setTexture(*explosion_textures[step]));

如果步骤不均匀,您可以使用有序地图

// I use `int`, but use whatever type is most appropriate
// populate map during program initialization 
map<int, texture_type> explosion_textures;

// In your function,
int step = ticker_.getElapsedTime().asMilliseconds();
sprite_.setTexture(*explosion_textures.lower_bound(step)->second);

答案 2 :(得分:1)

unsigned frame = ticker_.getElapsedTime().asMilliseconds() / ANIMATION_STEP;
auto digit0 = std::to_string(frame % 10);
auto digit1 = std::to_string(frame / 10);
sprite_.setTexture(*game_->getResourceManager().getTexture("animation/explosion_"+digit1+digit0+".png"));

答案 3 :(得分:1)

怎么样:

bool check(std::size_t step)
{
    auto tm = ticker_.getElapsedTime().asMilliseconds();
    return (tm >= ANIMATION_STEP * step) && (tm < ANIMATION_STEP * (step + 1));
}

for(int i=0; i <n;++i)
{
    if (check(i))
    {
        std::stringstream fname;
        fname<<"animation/explosion_"<<
        fname<<std::setfill(0)<<std::setw(2)<<i<<".png";
        sprite_.setTexture(*game_->getResourceManager().getTexture(fname.str()));
    }
}

答案 4 :(得分:0)

这里有一些像oop一样的方法,可以很容易地扩展到任意数量的animation_files。

这只是一个快速的原型,但该计划应该是明确的。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

class AnimationManager
{
public:
    AnimationManager(float pStepsize) : mStepsize(pStepsize) {}
    void registerSprite(std::string pFilename) //assuming that animation files are registered in the right order, alternatively use std::map as backend or something else
    {
        mFilenames.push_back(pFilename);
    }
    std::string getFilenameForAnimationStep(float ellapsed_time)
    {
        if (ellapsed_time < 0) //we dont want go pack in the past
            ellapsed_time = 0;
        unsigned int step = static_cast<unsigned int>(ellapsed_time/mStepsize); //trunc to int
        step = std::min(step, mFilenames.size() - 1); //make shure we use the last registered animation, if we are to fahre in the future
        return mFilenames[step];
    }

private:
    std::vector<std::string> mFilenames;
    float mStepsize;
};




int main()
{
    AnimationManager my_manager(0.2);
    my_manager.registerSprite("step_0.jpq");
    my_manager.registerSprite("step_1.jpq");
    my_manager.registerSprite("step_2.jpq");
    my_manager.registerSprite("step_3.jpq");
    //....
    //testing
    std::cout << my_manager.getFilenameForAnimationStep(-0.5) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.1) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.2) <<std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.5) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(0.7) << std::endl;
    std::cout << my_manager.getFilenameForAnimationStep(1.7) << std::endl;

    system("Pause");
}