From 6895c569736ea4749afb7145f0bfcd8fadce4b05 Mon Sep 17 00:00:00 2001 From: Historn Date: Sun, 29 Jan 2023 15:12:39 +0100 Subject: [PATCH] Gametimer, Checkpoint bug fixed --- .../Game/Game.vcxproj | 2 +- .../Game/Source/Item.cpp | 1 + .../Game/Source/Player.cpp | 8 ++- .../Game/Source/Scene.cpp | 63 +++++++++++++------ .../Game/Source/Scene.h | 3 + .../Game/Source/TitleScreen.cpp | 4 ++ .../Game/Source/UI.cpp | 22 +++++-- .../Game/Source/UI.h | 2 + .../Output/README.md | 4 +- .../Output/config.xml | 1 + .../Output/save_game.xml | 13 ++-- 11 files changed, 92 insertions(+), 31 deletions(-) diff --git a/citm_desvj_project_template-L07/Game/Game.vcxproj b/citm_desvj_project_template-L07/Game/Game.vcxproj index d021acb..f96e36a 100644 --- a/citm_desvj_project_template-L07/Game/Game.vcxproj +++ b/citm_desvj_project_template-L07/Game/Game.vcxproj @@ -87,7 +87,7 @@ true true Windows - SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;OptickCore.lib;SDL2_ttf.lib;ox2D.lib;%(AdditionalDependencies) + SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;OptickCore.lib;SDL2_ttf.lib;Box2D.lib;%(AdditionalDependencies) $(ProjectDir)Source\External\SDL_ttf\libx86;$(ProjectDir)Source\External\Optick\lib\releaseLib;$(ProjectDir)Source\External\Optick\lib\debugLib;$(ProjectDir)Source\External\SDL\libx86;$(ProjectDir)Source\External\SDL_image\libx86;$(ProjectDir)Source\External\SDL_mixer\libx86;$(ProjectDir)Source\External\Box2D\libx86\ReleaseLib diff --git a/citm_desvj_project_template-L07/Game/Source/Item.cpp b/citm_desvj_project_template-L07/Game/Source/Item.cpp index 978b4d5..769d172 100644 --- a/citm_desvj_project_template-L07/Game/Source/Item.cpp +++ b/citm_desvj_project_template-L07/Game/Source/Item.cpp @@ -111,6 +111,7 @@ void Item::OnCollision(PhysBody* physA, PhysBody* physB) { if (iType == "life" && app->scene->player->lives < 3) { + app->scene->itemLivesCount++; pbody->body->SetActive(false); this->Disable(); } diff --git a/citm_desvj_project_template-L07/Game/Source/Player.cpp b/citm_desvj_project_template-L07/Game/Source/Player.cpp index ea2271e..0996765 100644 --- a/citm_desvj_project_template-L07/Game/Source/Player.cpp +++ b/citm_desvj_project_template-L07/Game/Source/Player.cpp @@ -286,6 +286,12 @@ bool Player::Update() currentAnim->Update(); } + if (gameTimer <= 0) { + lives = 0; + dead = true; + app->audio->PlayFx(dieSFX); + app->fade->FadeToBlack((Module*)app->scene, (Module*)app->endingscreen, 60); + } return true; } @@ -400,7 +406,7 @@ void Player::OnCollision(PhysBody* physA, PhysBody* physB) { break; case ColliderType::BAT_HITBOX: LOG("Collison BAT HEAD HITBOX"); - + Jump(); app->scene->bat->lives--; app->scene->bat->onCollision = true; if (app->scene->bat->lives <= 0) { diff --git a/citm_desvj_project_template-L07/Game/Source/Scene.cpp b/citm_desvj_project_template-L07/Game/Source/Scene.cpp index 862ed89..f77fca9 100644 --- a/citm_desvj_project_template-L07/Game/Source/Scene.cpp +++ b/citm_desvj_project_template-L07/Game/Source/Scene.cpp @@ -63,6 +63,7 @@ bool Scene::Start() { item = (Item*)app->entityManager->CreateEntity(EntityType::ITEM); item->parameters = itemNode; + livesCollectedList.Add(item); } //L02: DONE 3: Instantiate the player using the entity manager @@ -179,12 +180,6 @@ bool Scene::Update(float dt) app->LoadGameRequest(); app->audio->PlayFx(selectSFX); } - - if (app->input->GetKey(SDL_SCANCODE_L) == KEY_DOWN) - { - app->render->limitFPS = !app->render->limitFPS; - app->audio->PlayFx(selectSFX); - } // God Mode key if (app->input->GetKey(SDL_SCANCODE_F10) == KEY_DOWN) @@ -212,10 +207,10 @@ bool Scene::Update(float dt) } - // Cap FPS to 30 + // Cap FPS to 60 if (app->input->GetKey(SDL_SCANCODE_F11) == KEY_DOWN) { - capTo30fps = !capTo30fps; + app->render->limitFPS = !app->render->limitFPS; app->audio->PlayFx(selectSFX); } @@ -389,12 +384,17 @@ void Scene::ResetScene() { app->audio->PlayMusic("Assets/Audio/Music/song1.ogg", 1.0f); - if (checkpointEnabled == false) { + pugi::xml_document gameStateFile; + pugi::xml_parse_result result = gameStateFile.load_file("save_game.xml"); + + if (checkpointEnabled == false || result == NULL) { + checkpointEnabled = false; player->ResetPlayerPos(); player->lives = 3; } - else + else if (checkpointEnabled == true && result != NULL) { app->LoadGameRequest(); + } } bool Scene::LoadState(pugi::xml_node& data) @@ -410,21 +410,39 @@ bool Scene::LoadState(pugi::xml_node& data) //Load previous saved player number of lives app->scene->player->lives = data.child("playerLives").attribute("playerLives").as_float(); - /*app->scene->player->coins = data.child("coins").attribute("coins").as_int(); + app->scene->player->coins = data.child("coins").attribute("coins").as_int(); ListItem* coinsCollected; - coinsCollected = coinsList.end; + coinsCollected = coinsList.start; int countCoins = app->scene->player->coins; while(countCoins >= 0 && coinsCollected != NULL){ - if (coinsCollected->data->isPicked == true) { - coinsCollected->data->ResetCoin(); + if (coinsCollected->data->isPicked == false) { + coinsCollected->data->isPicked = true; countCoins--; } - coinsCollected = coinsCollected->prev; - }*/ + coinsCollected = coinsCollected->next; + } + itemLivesCount = data.child("itemLives").attribute("itemLives").as_int(); + ListItem* livesCollected; + livesCollected = livesCollectedList.start; + int countLives = itemLivesCount; + while(countLives >= 0 && livesCollected != NULL){ + + if (livesCollected->data->isPicked == false) { + livesCollected->data->isPicked = true; + countLives--; + } + + + livesCollected = livesCollected->next; + } + + //Load previous saved player number of lives + checkpointEnabled = data.child("checkpointEnabled").attribute("checkpointEnabled").as_bool(); + // Load previous saved slime position b2Vec2 slimePos = { data.child("slimePosition").attribute("x").as_float(), data.child("slimePosition").attribute("y").as_float() }; app->scene->slime->pbody->body->SetTransform(slimePos, 0); @@ -463,9 +481,16 @@ bool Scene::SaveState(pugi::xml_node& data) playerLives.append_attribute("playerLives") = app->scene->player->lives; // Save current player number of coins - /*pugi::xml_node playerCoins = data.append_child("coins"); - playerCoins.append_attribute("coins") = app->scene->player->coins;*/ - + pugi::xml_node playerCoins = data.append_child("coins"); + playerCoins.append_attribute("coins") = app->scene->player->coins; + + // Save current player number of coins + pugi::xml_node itemLives = data.append_child("itemLives"); + itemLives.append_attribute("itemLives") = itemLives; + + // Save current player number of coins + pugi::xml_node checkpointEnabled = data.append_child("checkpointEnabled"); + checkpointEnabled.append_attribute("checkpointEnabled") = checkpointEnabled; // Save current slime position pugi::xml_node slimePos = data.append_child("slimePosition"); diff --git a/citm_desvj_project_template-L07/Game/Source/Scene.h b/citm_desvj_project_template-L07/Game/Source/Scene.h index de672e8..c2350ee 100644 --- a/citm_desvj_project_template-L07/Game/Source/Scene.h +++ b/citm_desvj_project_template-L07/Game/Source/Scene.h @@ -91,6 +91,9 @@ class Scene : public Module List coinsList; + List livesCollectedList; + int itemLivesCount = 0; + private: SDL_Texture* img_pause; iPoint startPosition; diff --git a/citm_desvj_project_template-L07/Game/Source/TitleScreen.cpp b/citm_desvj_project_template-L07/Game/Source/TitleScreen.cpp index 38646ff..558daae 100644 --- a/citm_desvj_project_template-L07/Game/Source/TitleScreen.cpp +++ b/citm_desvj_project_template-L07/Game/Source/TitleScreen.cpp @@ -290,6 +290,10 @@ bool TitleScreen::OnGuiMouseClickEvent(GuiControl* control) // Play button app->fade->FadeToBlack(this, (Module*)app->scene, 90); app->audio->PlayFx(startSFX); + if (remove("save_game.xml") != 0) + LOG("Error at Deleting Save Game"); + else + LOG("Save Game Successfully Deleted"); break; case 2: diff --git a/citm_desvj_project_template-L07/Game/Source/UI.cpp b/citm_desvj_project_template-L07/Game/Source/UI.cpp index 12ef1bd..b6017ae 100644 --- a/citm_desvj_project_template-L07/Game/Source/UI.cpp +++ b/citm_desvj_project_template-L07/Game/Source/UI.cpp @@ -39,6 +39,7 @@ bool UI::Start() /*Initialize*/ font1Path = app->configNode.child("ui").child("font1").attribute("texturepath").as_string(); font2Path = app->configNode.child("ui").child("font2").attribute("texturepath").as_string(); + font2_RedPath = app->configNode.child("ui").child("font2Red").attribute("texturepath").as_string(); livesTexPath = app->configNode.child("scene").child("life").attribute("texturepath").as_string(); coinsTexPath = app->configNode.child("scene").child("coin").attribute("texturepath").as_string(); @@ -48,6 +49,9 @@ bool UI::Start() char lookupTableFont2[] = { "! %&'()*+,-./0123456789:;<=>abcdefghijklmnopqrstuvwxyz" }; font2_id = app->fonts->Load(font2Path, lookupTableFont2, 1); + + char lookupTableFont2Red[] = { "! %&'()*+,-./0123456789:;<=>abcdefghijklmnopqrstuvwxyz" }; + font2Red_id = app->fonts->Load(font2_RedPath, lookupTableFont2Red, 1); livesTex = app->tex->Load(livesTexPath); coinsTex = app->tex->Load(coinsTexPath); @@ -122,9 +126,19 @@ void UI::BlitLives() void UI::BlitTimer() { - char time[20]; - sprintf_s(time, 20, "time: %.f", app->scene->player->gameTimer); - app->fonts->BlitText(400, 15, font2_id, time); + if (app->scene->player->gameTimer < 50) + { + char time[20]; + sprintf_s(time, 20, "time: %.f", app->scene->player->gameTimer); + app->fonts->BlitText(425, 15, font2Red_id, time); + } + else + { + char time[20]; + sprintf_s(time, 20, "time: %.f", app->scene->player->gameTimer); + app->fonts->BlitText(425, 15, font2_id, time); + } + } void UI::BlitCoins() @@ -187,7 +201,7 @@ void UI::BlitFPS() { char fps[25]; sprintf_s(fps, 25, "fps: %d", app->GetFPS()); - app->fonts->BlitText(825, 15, font2_id, fps); + app->fonts->BlitText(870, 15, font2_id, fps); } void UI::BlitAverageFPS() diff --git a/citm_desvj_project_template-L07/Game/Source/UI.h b/citm_desvj_project_template-L07/Game/Source/UI.h index 0baec0f..ba60222 100644 --- a/citm_desvj_project_template-L07/Game/Source/UI.h +++ b/citm_desvj_project_template-L07/Game/Source/UI.h @@ -51,6 +51,7 @@ class UI : public Module uint font1_id = -1; uint font2_id = -1; + uint font2Red_id = -1; SDL_Texture* livesTex; const char* livesTexPath; @@ -62,6 +63,7 @@ class UI : public Module const char* font1Path; const char* font2Path; + const char* font2_RedPath; }; diff --git a/citm_desvj_project_template-L07/Output/README.md b/citm_desvj_project_template-L07/Output/README.md index 4704fda..619bb46 100644 --- a/citm_desvj_project_template-L07/Output/README.md +++ b/citm_desvj_project_template-L07/Output/README.md @@ -10,7 +10,9 @@ - {F3} - Start from the beginning of the current level - {F5} - Save current game state (player & enemies) - {F6} - Load the previous game state (player & enemies) + - {F7} - Move between checkpoints - {F9} - View colliders/logic/paths + - {F8} - View GUI bounds rectangles and state - {F10} - God Mode - {F11} - Enable/Disable FPS cap to 30 @@ -28,7 +30,7 @@ - {SPACEBAR} - Player's jump - {SPACEBAR + SPACEBAR} - Player's DOUBLE jump - {ENTER} - Pass to the next screen - - {ESCAPE} - Exit the game + - {ESCAPE} - Pause the game ## Developers diff --git a/citm_desvj_project_template-L07/Output/config.xml b/citm_desvj_project_template-L07/Output/config.xml index 3f65da2..93e734b 100644 --- a/citm_desvj_project_template-L07/Output/config.xml +++ b/citm_desvj_project_template-L07/Output/config.xml @@ -25,6 +25,7 @@ + diff --git a/citm_desvj_project_template-L07/Output/save_game.xml b/citm_desvj_project_template-L07/Output/save_game.xml index d6e9e19..9a012d5 100644 --- a/citm_desvj_project_template-L07/Output/save_game.xml +++ b/citm_desvj_project_template-L07/Output/save_game.xml @@ -11,14 +11,17 @@ <:anonymous /> - + - + + + + - - + + @@ -28,6 +31,6 @@ - +