HGameConfigParser.cpp
5.16 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// 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));
}
}
}