HGameConfigParser.cpp 5.16 KB
//
//  HGameConfigParser.cpp
//  SteveAndMaggieGame-mobile
//
//  Created by Katarzyna Kalinowska-Górska on 07/05/2019.
//

#include "cocos2d.h"
#include "HGameConfigParser.h"
#include "HJSONParseUtils.h"
#include "HSubGameSceneSpaceInvaders.h"

HGameConfigParser::HGameConfigParser(std::string configFilePath){
    _configJson = new rapidjson::Document();
    auto jsonString = cocos2d::FileUtils::getInstance()->getStringFromFile(configFilePath);
    _configJson->Parse(jsonString.c_str());
}

HGameConfigParser::~HGameConfigParser(){
    delete _configJson;
}

HSubGameScene* HGameConfigParser::createGameScene(std::string layoutFilePath){
    return createGameScene(parseGameType(), layoutFilePath);
}

std::string HGameConfigParser::parseGameType(){
    if(HJSONParseUtils::hasMemberString(*_configJson, "game_type")){
        return (*_configJson)["game_type"].GetString();
    }
    return "";
}

HSubGameScene* HGameConfigParser::createGameScene(std::string gameType, std::string layoutFilePath){
    //TODO add if exists everyewhere
    HSubGameScene::SoundConfig soundConfig;
    soundConfig.soundsFolder = (HJSONParseUtils::hasMemberString(*_configJson, "sounds_path")) ?
    (*_configJson)["sounds_path"].GetString() : "";
    parseSoundInfoFile(soundConfig.soundsFolder, soundConfig.soundsFolder + "sounds_info.si", soundConfig.soundDurations);
    
    auto gamePropertiesJson = (*_configJson)["game_properties"].GetObject();
    HSubGameScene::CommonConfig commonConfig;
    commonConfig.soundConfig = soundConfig;
//    if(HJSONParseUtils::hasMemberArray(gamePropertiesJson, "itemPictureFilePaths")){ //<-- something's wrong with this code
        commonConfig.itemPicturePaths = HJSONParseUtils::parseStringArray(gamePropertiesJson["itemPictureFilePaths"]);
//    }
    commonConfig.screenPadding = gamePropertiesJson["screen_padding"].GetInt();
    commonConfig.maxSecondsInactivity = gamePropertiesJson["max_seconds_inactivity"].GetInt();
    commonConfig.lives = gamePropertiesJson["lives"].GetInt();
    commonConfig.maxItemsAtATime = gamePropertiesJson["max_items_at_a_time"].GetInt();
    
    if (gameType == "space_invaders"){
        return HSubGameSceneSpaceInvaders::create(layoutFilePath, commonConfig, [&](HSubGameSceneSpaceInvaders::GameConfig& gameConfig){
            gameConfig.catchingCharacterRangeFrac = gamePropertiesJson["catchingCharacterRangeFrac"].GetFloat();
            gameConfig.catchingCharacterGrabExtendPercent = gamePropertiesJson["catchingCharacterGrabExtendPercent"].GetInt();
            gameConfig.catchingCharacterCatchStartXFrac = gamePropertiesJson["catchingCharacterCatchStartXFrac"].GetFloat();
            gameConfig.catchingCharacterCatchEndXFrac = gamePropertiesJson["catchingCharacterCatchEndXFrac"].GetFloat();
            gameConfig.catchingCharacterCatchTopYFrac = gamePropertiesJson["catchingCharacterCatchTopYFrac"].GetFloat();
            gameConfig.catchingCharacterCatchBottomYFrac = gamePropertiesJson["catchingCharacterCatchBottomYFrac"].GetFloat();
            gameConfig.catchingCharacterBottomImagePath = gamePropertiesJson["catchingCharacterBottomImagePath"].GetString();
            gameConfig.catchingCharacterFrontImagePath = gamePropertiesJson["catchingCharacterFrontImagePath"].GetString();
            gameConfig.topYCutValue = gamePropertiesJson["topYCutValue"].GetFloat()/cocos2d::Director::getInstance()->getContentScaleFactor();
            gameConfig.levelTimeSeconds = gamePropertiesJson["levelTimeSeconds"].GetFloat();
            
            auto levelConfigs = gamePropertiesJson["level_info"].GetArray();
            for(const auto& levelJson : levelConfigs){
                HSubGameSceneSpaceInvaders::LevelConfig levelConfig;
                levelConfig.itemVelocity = levelJson["itemVelocity"].GetFloat();
                levelConfig.windParam = levelJson["windParam"].GetFloat();
                levelConfig.sineMovementMaxAmp = levelJson["sineMovementMaxAmp"].GetFloat();
                levelConfig.sineMovementProbability = levelJson["sineMovementProbability"].GetFloat();
                levelConfig.sineSum3MovementProbability = levelJson["sineSum3MovementProbability"].GetFloat();
                levelConfig.bgMusicFilePath = soundConfig.soundsFolder + levelJson["bgMusicFilePath"].GetString();
                levelConfig.rotationProbability = levelJson["rotationProbability"].GetFloat();
                gameConfig.levelConfigs.push_back(levelConfig);
            }
        });
    }
    
    return nullptr;
}

void HGameConfigParser::parseSoundInfoFile(std::string soundFolderPath, std::string filePath, std::map<std::string, float>& mapToFill){
    if(cocos2d::FileUtils::getInstance()->isFileExist(filePath)){
        auto soundInfoJson = new rapidjson::Document();
        auto jsonString = cocos2d::FileUtils::getInstance()->getStringFromFile(filePath);
        soundInfoJson->Parse(jsonString.c_str());
        
        auto soundsList = (*soundInfoJson)["sounds"].GetArray();
        for(const auto& sound : soundsList){
            auto soundFilename = sound["filename"].GetString();
            auto soundDuration = sound["duration_seconds"].GetFloat();
            mapToFill.insert(std::make_pair(soundFolderPath + soundFilename, soundDuration));
        }
    }
}