diff --git a/.gitignore b/.gitignore index a6bf3b3..a929ead 100644 --- a/.gitignore +++ b/.gitignore @@ -75,4 +75,5 @@ iOSInjectionProject/ /windows/DoubleHitBalls-win/DoubleHitBalls-win/resources *.db *.opendb -/proj.android-studio/app/release/ \ No newline at end of file +/proj.android-studio/app/release/ +assets/levels/user_progress.json \ No newline at end of file diff --git a/assets/gui_main_menu.json b/assets/gui_main_menu.json index f113c21..6c74356 100755 --- a/assets/gui_main_menu.json +++ b/assets/gui_main_menu.json @@ -26,7 +26,8 @@ "paddingLeft": 10, "paddingRight": 10, "itemSpacing": 20, - "children" : [{ + "children" : [ + { "type": "HorizontalLinearLayout", "name": "row1", "paddingBottom": 15, diff --git a/assets/shaders/hoverable-button.fragment b/assets/shaders/hoverable-button.fragment new file mode 100755 index 0000000..4701ae1 --- /dev/null +++ b/assets/shaders/hoverable-button.fragment @@ -0,0 +1,16 @@ +precision highp float; + +uniform sampler2D Texture; +uniform bool Hover; +varying vec2 texCoord; +varying vec4 color; + +void main() +{ + vec4 texColor = texture2D(Texture,texCoord).rgba; + gl_FragColor = color * texColor.rgba; + if (Hover) + { + gl_FragColor.xyz *= 1.5; + } +} diff --git a/game/galaxy_menu.cpp b/game/galaxy_menu.cpp index 45ea9a4..d3e2972 100755 --- a/game/galaxy_menu.cpp +++ b/game/galaxy_menu.cpp @@ -28,7 +28,6 @@ bool GalaxyMenu::InitGalaxyMenu(std::string config_json, float scale) { boost::property_tree::ptree error_pt; error_pt.put("error", "something goes wrong at InitGalaxyMenu"); - int levelIndex = 1; /*..Init Menu..*/ BOOST_FOREACH(auto menu_pt, config_pt.get_child("Space", error_pt)) { @@ -39,6 +38,9 @@ bool GalaxyMenu::InitGalaxyMenu(std::string config_json, float scale) { galax.texture = menu_pt.second.get("texture", "error"); galax.position = Eigen::Vector2f(menu_pt.second.get("position.x_coord", 0.0f), menu_pt.second.get("position.y_coord", 0.0f)); //boost::property_tree::ptree stars = menu_pt.second.get("Stars", error_pt); + + int levelStar = 0; + BOOST_FOREACH(auto stars_pt, menu_pt.second.get_child("Stars", error_pt)) { StarObject star; @@ -50,18 +52,24 @@ bool GalaxyMenu::InitGalaxyMenu(std::string config_json, float scale) { ResourceManager->TexList.AddTexture(star.texture); star.position = Eigen::Vector2f(stars_pt.second.get("position.x_coord", 0.0f), stars_pt.second.get("position.y_coord", 0.0f)); + + int levelIndex = 0; /*..Levels..*/ BOOST_FOREACH(auto levels_pt, stars_pt.second.get_child("levels")) { std::string levelName = levels_pt.second.get("name", "empty"); - std::shared_ptr lvl = std::make_shared(); + std::shared_ptr lvl = std::make_shared(levelStar, levelIndex); lvl->FillWithFile(ST::PathToResources + levelName + ".txt"); star.selectionMenu.gameLevels.push_back(lvl); + + ++levelIndex; } galax.Stars.push_back(star); + + ++levelStar; } galaxies.push_back(galax); } @@ -203,12 +211,15 @@ void GalaxyMenu::DrawGalaxyMenu() { ); // DrawRect /*..Draw stars..*/ + Renderer->PushShader("HoverableButtonShader"); if (stars_params.size() >= i) { for (int j = 0; j < stars_params[i].size(); j++) { - if (planetHoverIndex == j) { + if (planetHoverIndex == j) { + RenderUniform1i("Hover", 1); glBindTexture(GL_TEXTURE_2D, SE::ResourceManager->TexList[galaxies[i].Stars[j].textureName]); } - else { + else { + RenderUniform1i("Hover", 0); glBindTexture(GL_TEXTURE_2D, SE::ResourceManager->TexList[galaxies[i].Stars[j].textureName]); } SE::Renderer->DrawRect( @@ -223,6 +234,7 @@ void GalaxyMenu::DrawGalaxyMenu() { ); // DrawRect } } + Renderer->PopShader(); /*..Draw level selection menu..*/ //drawSelectionMenu(starIndex); @@ -494,16 +506,32 @@ int GalaxyMenu::negativeV(float val) { } int GalaxyMenu::findPlanetByPos(Eigen::Vector2f pos) { - for (int i = 0; i < stars_params.size(); i++) { - for (int j = 0; j < stars_params[i].size(); j++) { - if (pos(0) >= (stars_params[i][j].first(0) - stars_params[i][j].second(0) / 2) && pos(0) <= (stars_params[i][j].first(0) + stars_params[i][j].second(0) / 2)) { - if (pos(1) >= (stars_params[i][j].first(1) - stars_params[i][j].second(1) / 2) && pos(1) <= (stars_params[i][j].first(1) + stars_params[i][j].second(1) / 2)) { - return j; + + float minimalDistance = std::numeric_limits::max(); + int index = -1; + + for (int i = 0; i < stars_params.size(); i++) + { + for (int j = 0; j < stars_params[i].size(); j++) + { + if (pos(0) >= (stars_params[i][j].first(0) - stars_params[i][j].second(0) / 2) && pos(0) <= (stars_params[i][j].first(0) + stars_params[i][j].second(0) / 2)) + { + if (pos(1) >= (stars_params[i][j].first(1) - stars_params[i][j].second(1) / 2) && pos(1) <= (stars_params[i][j].first(1) + stars_params[i][j].second(1) / 2)) + { + float dx = pos(0) - stars_params[i][j].first(0); + float dy = pos(1) - stars_params[i][j].first(1); + float distance = dx * dx + dy * dy; + + if (distance < minimalDistance) + { + minimalDistance = distance; + index = j; + } } } } } - return -1; + return index; } bool GalaxyMenu::checkMenuBound(Eigen::Vector2f pos) { diff --git a/game/gamecode.cpp b/game/gamecode.cpp index 6758778..6c68ff3 100755 --- a/game/gamecode.cpp +++ b/game/gamecode.cpp @@ -439,8 +439,10 @@ void TBall::Update(size_t dt) //=========================================== -TGameLevel::TGameLevel() +TGameLevel::TGameLevel(int levelStar, int levelIndex) { + this->levelStar = levelStar; + this->levelIndex = levelIndex; BkgTexture = ""; @@ -462,6 +464,16 @@ TGameLevel::TGameLevel() TGameLevel::~TGameLevel() { } + +int TGameLevel::getStarIndex() +{ + return levelStar; +} + +int TGameLevel::getLevelIndex() +{ + return levelIndex; +} @@ -1319,6 +1331,8 @@ void TGameLevel::Update(size_t dt) if (noMoreBlocks && LevelState != CONST_LEVELSTATE_FINISH_FREEZE) { + Application->SaveUserProgress(levelStar, levelIndex); + Application->fireworkEffect(); // Firework start Application->OpenNextLevel(); diff --git a/game/gamecode.h b/game/gamecode.h index 8b183f7..cce5dd3 100755 --- a/game/gamecode.h +++ b/game/gamecode.h @@ -130,6 +130,9 @@ protected: //std::string LevelScreenTexture; std::string LevelFileName; + int levelIndex; + int levelStar; + GLuint prerenderedImage; Vector2f ReflectorPos; @@ -190,7 +193,7 @@ protected: void ReloadLevel(); public: - TGameLevel(); + TGameLevel(int levelStar, int levelIndex); ~TGameLevel(); void FillWithFile(const std::string& filename); @@ -199,6 +202,9 @@ public: //void SetLevelScale(); //Vector2f GetLevelScale(); + int getStarIndex(); + int getLevelIndex(); + void SetStandBy(); void SetLoading(); bool IsLoaded(); diff --git a/game/main_code.cpp b/game/main_code.cpp index 72efe6c..273d217 100755 --- a/game/main_code.cpp +++ b/game/main_code.cpp @@ -54,9 +54,73 @@ const float CONST_CREDITS_SHOW_TIME = 150.f; TMyApplication* Application; +int currentStar; +std::vector finishedLevels; + +void TMyApplication::LoadUserProgress() +{ + boost::property_tree::ptree userProgressJson; + + try + { + boost::property_tree::json_parser::read_json(ST::PathToResources + "levels/user_progress.json", userProgressJson); + currentStar = userProgressJson.get("currentStar"); + for (auto& index : userProgressJson.get_child("finishedLevels")) + { + finishedLevels.push_back(index.second.get_value()); + } + } + catch (...) + { + currentStar = 0; + finishedLevels.clear(); + } +} + +void TMyApplication::SaveUserProgress(int levelStar, int levelIndex) +{ + if (levelStar < currentStar) + { + return; + } + + auto iter = std::find(finishedLevels.begin(), finishedLevels.end(), levelIndex); + if (iter != finishedLevels.end()) + { + return; + } + + finishedLevels.push_back(levelIndex); + + if (finishedLevels.size() == Menu.GalaxMenu.galaxies[0].Stars[currentStar].selectionMenu.gameLevels.size()) + { + finishedLevels.clear(); + + if (currentStar < Menu.GalaxMenu.galaxies[0].Stars.size() - 1) + { + currentStar += 1; + } + } + + boost::property_tree::ptree userProgressJson; + + userProgressJson.put("currentStar", currentStar); + + boost::property_tree::ptree finishedLevelsTree; + for (int index : finishedLevels) + { + boost::property_tree::ptree finishedLevel; + finishedLevel.put_value(index); + finishedLevelsTree.push_back(std::make_pair("", finishedLevel)); + } + + userProgressJson.put_child("finishedLevels", finishedLevelsTree); + boost::property_tree::json_parser::write_json(ST::PathToResources + "levels/user_progress.json", userProgressJson); +} + + void TMyApplication::InnerInit() { - Application = this; #ifdef TARGET_WIN32 @@ -76,6 +140,8 @@ void TMyApplication::InnerInit() ST::PathToResources = ""; #endif + LoadUserProgress(); + if (Console != NULL) { *Console<<"APP INIT\n"; @@ -93,6 +159,7 @@ void TMyApplication::InnerInit() //ResourceManager->ShaderManager.AddShader("DefaultShader", "shaders/texture-shader.vertex", "shaders/texture-shader.fragment"); ResourceManager->ShaderManager.AddShader("DefaultShader", "shaders/gui_transparent.vertex", "shaders/gui_transparent.fragment"); + ResourceManager->ShaderManager.AddShader("HoverableButtonShader", "shaders/gui_transparent.vertex", "shaders/hoverable-button.fragment"); ResourceManager->ShaderManager.AddShader("BlackAndWhiteShader", "shaders/gui_transparent_blackandwhite.vertex", "shaders/gui_transparent_blackandwhite.fragment"); ResourceManager->ShaderManager.AddShader("ColorShader", "shaders/color-shader.vertex", "shaders/color-shader.fragment"); ResourceManager->ShaderManager.AddShader("FrameShader", "shaders/frameshader_vertex.txt", "shaders/frameshader_fragment.txt"); @@ -137,7 +204,6 @@ void TMyApplication::InnerInit() std::cout << "menu error" << std::endl; } - } void TMyApplication::InnerDeinit() @@ -403,9 +469,7 @@ void TMyApplication::InnerDraw() { //glDisable(GL_DEPTH_TEST); - Renderer->PushShader("DefaultShader"); OnDrawSignal(); - Renderer->PopShader(); } @@ -755,17 +819,7 @@ void TMyApplication::InnerOnMouseMove(TMouseState& mouseState) { bool TMyApplication::IsLevelOpened(int levelStar, int levelIndex) { - if (levelStar == 0) - { - return true; - } - - if (levelStar == 1 && levelIndex < 3) - { - return true; - } - - return false; + return levelStar <= currentStar; } diff --git a/game/main_code.h b/game/main_code.h index 809174b..4cc3afc 100755 --- a/game/main_code.h +++ b/game/main_code.h @@ -137,6 +137,9 @@ public: bool Loaded; TMyApplication() : TApplication(), Loaded(false), Inited(false) { } + + void LoadUserProgress(); + void SaveUserProgress(int levelStar, int levelIndex); virtual void InnerInit();