mardi 3 décembre 2019

why game state is not updating?

I am a beginner in game states (and in most of c++). This is the method that handle the states:

void Game::run() {
    float newTime, frameTime, interpolation;
    float accumulator = 0.f;
    float currentTime = gameClock.getElapsedTime().asSeconds();

    while(data->renderWindow.isOpen()){
        data->stateManager.makeChanges();
        newTime = gameClock.getElapsedTime().asSeconds();
        frameTime = newTime - currentTime;
        if(frameTime > 0.25f)
            frameTime = 0.25f;
        currentTime = newTime;
        accumulator += frameTime;
        while(accumulator >= dt){
            data->stateManager.getActiveState()->handleInput();
            data->stateManager.getActiveState()->update(dt);
            accumulator -= dt;
        }
        interpolation = accumulator/dt;
        data->stateManager.getActiveState()->draw(interpolation);
    }
}

And this is the last state of the sequence:

void GameLoop::update(float dt) {
    if(gen.getElapsedTime().asSeconds() >= 15) {
        entities.emplace_back(new Asteroid(data->textureManager));
        score.setScore(1);
        gen.restart();
    }

    for(int i = 0; i < entities.size(); i++){
        entities.at(i)->updatePosition();
        entities.at(i)->getAnimation().update(entities.at(i)->getSprite());
        if (!entities.at(i)->getHp()){
            if(entities.at(i)->getType() == EntityType::asteroid){
                entities.emplace_back(new Explosion(data->textureManager, *entities.at(i)));
                entities.emplace_back(new Asteroid(data->textureManager, *entities.at(i)));
                entities.emplace_back(new Asteroid(data->textureManager, *entities.at(i)));
                score.setScore(10);
            }
            if(entities.at(i)->getType() == EntityType::rubble) {
                entities.emplace_back(new Explosion(data->textureManager, *entities.at(i)));
                score.setScore(7);
            }
            entities.erase(entities.begin()+i);
        }
    }

    for (int i = 0; i < entities.size(); i++) {
        for (int j = 0; j < entities.size(); j++) {
            if (entities.at(i)->getType() == EntityType::projectile && (entities.at(j)->getType() == EntityType::asteroid || entities.at(j)->getType() == EntityType::rubble)) {
                if (Collision::PixelPerfectTest(entities.at(i)->getSprite(), entities.at(j)->getSprite())) {
                    entities.at(i)->setHp(0);
                    entities.at(j)->setHp(0);
                }
            }
        }
    }
}

The game loop was working fine before i implemented a state manager pattern, now it works without a problem until GameLoop state, where it is stuck at the first frame and is not updating. In the previous states it update well, now for some reason it is not updating. Why?

P.S. : I'm using sfml.

Aucun commentaire:

Enregistrer un commentaire