AniGameStaticCreator.h 2.8 KB
//
//  AniGameStaticCreator.h
//  SteveAndMaggieGame
//
//  Created by Katarzyna Kalinowska-Górska on 07/05/2019.
//

#ifndef AniGameStaticCreator_h
#define AniGameStaticCreator_h

#include "AniMiscUtils.h"
#include "AniGameConfigParser.h"
#include "AniResourceUtilities.h"

class AniGameStaticCreator {
    
public:

    void preloadGameResources(std::function<void(std::vector<std::string>)> callback){
        if(m_counter >= 0){
            return; //do nothing
        } else {
            m_counter = 0;
            
            onResourcesLoadedCallback = callback;
            auto layoutFile = getLayoutFile();
            auto AniResourceUtilities = AniResourceUtilities::getInstance();
            auto subCallback = [&](std::vector<std::string> res){
                ++m_counter;
                if(m_counter == CounterTarget){
                    onResourcesLoadedCallback(res);
                    m_counter = -1;
                }
            };
            std::vector<std::string> settingsResources = {"graphics/levels/level_1.png", "graphics/levels/level_2.png", "graphics/levels/level_3.png", "settings_buttons/steve_maggie.png", "settings_buttons/play_preview_again.png", "graphics/level_halo.png" };
//            std::vector<std::string> tutorialResources = {/*"graphics/tutorials/graphics/fingers_two.png",*/ "graphics/tutorials/graphics/finger_one.png"};
            // don't load two fingers.it will only be used rarely after the first time.
            std::vector<std::string> otherGameResources = {"graphics/star.png", "graphics/tutorials/graphics/finger_one.png"}; /*"buttons/graphics/button_repeat.png",*/
//            std::vector<std::stirng> mapTilesets = {"baby_level/tileset.png", }; //maybe we don't need them. mind the path!!
            
            AniResourceUtilities.preloadResourcesForFile(layoutFile, subCallback);
            AniResourceUtilities.preloadFiles(settingsResources, subCallback);
//            AniResourceUtilities.preloadFiles(tutorialResources, subCallback);
            AniResourceUtilities.preloadFiles(otherGameResources, subCallback);
        }
    }
    
    cocos2d::Scene* createGameScene(){
        AniGameConfigParser parser("common/games/game_map/gconfig.gcf");
        //TODO use adv scene mmethod somehow? duplicated in AniAdventureScene.
        std::string layoutFile = getLayoutFile();
        return parser.createGameScene(1, layoutFile);
    }
    
private:
    
    const int CounterTarget = 3;
    int m_counter { -1 };
    std::function<void(std::vector<std::string>)> onResourcesLoadedCallback {[](std::vector<std::string>){}};
    
    static std::string getLayoutFile(){
        return AniMiscUtils::lastLevel() == AniMiscUtils::Level::CHILD ? "graphics/game_map/baby_level/scene_layout_baby.scl" : "graphics/game_map/scene_layout.scl";
    }
};

#endif /* AniGameStaticCreator_h */