ToyGameConfigParser.cpp
3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// ToyGameConfigParser.cpp
// SteveAndMaggieGame-mobile
//
// Created by Katarzyna Kalinowska-Górska on 07/05/2019.
//
#include "cocos2d.h"
#include "ToyGameConfigParser.h"
#include "ToyJSONParseUtils.h"
#include "ToySubGameSceneTapNCatch.h"
#include "ToySoundsRepo.h"
ToyGameConfigParser::ToyGameConfigParser(std::string configFilePath){
_configJson = new rapidjson::Document();
auto jsonString = cocos2d::FileUtils::getInstance()->getStringFromFile(configFilePath);
_configJson->Parse(jsonString.c_str());
}
ToyGameConfigParser::~ToyGameConfigParser(){
delete _configJson;
}
ToySubGameScene* ToyGameConfigParser::createGameScene(int gameId,std::string layoutFilePath){
return createGameScene(gameId, parseGameType(), layoutFilePath);
}
std::string ToyGameConfigParser::parseGameType(){
if(ToyJSONParseUtils::hasMemberString(*_configJson, "game_type")){
return (*_configJson)["game_type"].GetString();
}
return "";
}
ToySubGameScene* ToyGameConfigParser::createGameScene(int gameId, std::string gameType, std::string layoutFilePath){
//TODO add if exists everyewhere
// ToySoundsRepo::soundsFolder = (ToyJSONParseUtils::hasMemberString(*_configJson, "sounds_path")) ?
// (*_configJson)["sounds_path"].GetString() : "";
// parseSoundInfoFile(ToySoundsRepo::soundsFolder, ToySoundsRepo::soundsFolder + "sounds_info.si", ToySoundsRepo::soundDurations);
auto gamePropertiesJson = (*_configJson)["game_properties"].GetObject();
ToySubGameScene::CommonConfig commonConfig;
// if(ToyJSONParseUtils::hasMemberArray(gamePropertiesJson, "itemPictureFilePaths")){ //<-- something's wrong with this code
commonConfig.itemPicturePaths = ToyJSONParseUtils::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 == "tap_n_catch"){
return ToySubGameSceneTapNCatch::create(gameId, layoutFilePath, commonConfig, [&](ToySubGameSceneTapNCatch::GameConfig& gameConfig){
auto scale = 1/cocos2d::Director::getInstance()->getContentScaleFactor();
auto levelConfigs = gamePropertiesJson["level_info"].GetArray();
for(const auto& levelJson : levelConfigs){
ToySubGameSceneTapNCatch::LevelConfig levelConfig;
levelConfig.updateTimeInterval = levelJson["updateTimeInterval"].GetFloat();
levelConfig.maxItemsFrac = levelJson["maxItemsFrac"].GetFloat();
levelConfig.itemDisappearProbLim = levelJson["itemDisappearProbLim"].GetFloat();
gameConfig.levelConfigs.push_back(levelConfig);
}
// gameConfig.obstacles = ToyJSONParseUtils::parseRectArray(gamePropertiesJson["obstacles"]);
gameConfig.staticItems = gamePropertiesJson["static_items"].GetInt()*scale;
gameConfig.levelTimeSeconds = gamePropertiesJson["levelTimeSeconds"].GetFloat();
gameConfig.levelsPerLevel = gamePropertiesJson["levelsPerLevel"].GetInt();
});
}
return nullptr;
}