From c8e02a23a8f95b828b07e3e4ea5a890d32739de1 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 24 Aug 2024 05:02:04 -0500 Subject: [PATCH] Added PB tracking for both emscripten and native builds of the game. --- PBData | 1 + src/Hamster.cpp | 1 + src/Hamster.h | 2 +- src/HamsterGame.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++-- src/HamsterGame.h | 22 +++++++++++++++ src/HamsterNet.cpp | 9 ++++-- src/HamsterNet.h | 4 ++- 7 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 PBData diff --git a/PBData b/PBData new file mode 100644 index 0000000..5ecd86b --- /dev/null +++ b/PBData @@ -0,0 +1 @@ +2147483647 2147483647 2147483647 2147483647 109086 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 \ No newline at end of file diff --git a/src/Hamster.cpp b/src/Hamster.cpp index bfafeb6..0f2a8dd 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -289,6 +289,7 @@ void Hamster::CreateHamsters(const HamsterGame::GameMode mode){ } void Hamster::MoveHamstersToSpawn(const geom2d::rectstartingLoc){ + if(HAMSTER_LIST.size()==0)Hamster::CreateHamsters(HamsterGame::Game().GetGameMode()); int MAX_AI_FILES{100}; int aiFileCount{0}; while(MAX_AI_FILES>0){ diff --git a/src/Hamster.h b/src/Hamster.h index d557dd6..15f3d12 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -129,7 +129,7 @@ class Hamster{ float boostTimer{}; float canCollectWheelPowerupTimer{}; float SEARCH_RANGE{1.f}; - HamsterAI::AIType aiLevel; + HamsterAI::AIType aiLevel{HamsterAI::AIType::NORMAL}; public: Hamster(const vf2d spawnPos,const std::string&img,const PlayerControlled IsPlayerControlled=NPC); static const Hamster&GetPlayer(); diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index cbbdb40..bccdc04 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -13,6 +13,8 @@ std::unordered_mapHamsterGame::GFX; const std::string HamsterGame::ASSETS_DIR{"assets/"}; HamsterGame*HamsterGame::self{nullptr}; std::unordered_mapHamsterGame::ANIMATED_TILE_IDS; +std::unordered_mapHamsterGame::mapPBs; +const int HamsterGame::UNPLAYED{std::numeric_limits::max()}; HamsterGame::HamsterGame(const std::string&appName){ sAppName=appName; @@ -20,6 +22,7 @@ HamsterGame::HamsterGame(const std::string&appName){ } bool HamsterGame::OnUserCreate(){ + LoadPBs(); audio.SetBackgroundPlay(true); olc::GFX3D::ConfigureDisplay(); camera=Camera2D{SCREEN_FRAME.size}; @@ -195,6 +198,7 @@ void HamsterGame::LoadLevel(const std::string&mapName){ SetDrawTarget(nullptr); audio.Play(bgm.at(currentMap.value().GetData().GetBGM()),true); + Hamster::MoveHamstersToSpawn(currentMap.value().GetData().GetSpawnZone()); } void HamsterGame::UpdateGame(const float fElapsedTime){ @@ -368,11 +372,10 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){ net.SetName("Sig"); net.SetColor("Yellow"); LoadLevel("StageV.tmx"); //THIS IS TEMPORARY. - Hamster::CreateHamsters(mode); - Hamster::MoveHamstersToSpawn(currentMap.value().GetData().GetSpawnZone()); camera.SetTarget(Hamster::GetPlayer().GetPos()); net.StartRace(currentMapName); } + runTime+=fElapsedTime; UpdateGame(fElapsedTime); DrawGame(); @@ -592,7 +595,67 @@ const Difficulty&HamsterGame::GetMapDifficulty()const{ } void HamsterGame::OnPlayerFinishedRace(){ - net.FinishRace(); + std::pairresult{net.FinishRace()}; + std::cout<<"Finish Time: "<=Game().mapNameList.size())break; + int readVal; + file>>readVal; + mapPBs[Game().mapNameList[readCounter]]=readVal; + readCounter++; + } + file.close(); + #endif + i++; + } +} + +const HamsterGame::GameMode HamsterGame::GetGameMode(){ + return mode; } int main() diff --git a/src/HamsterGame.h b/src/HamsterGame.h index 7014a30..9d36ab3 100644 --- a/src/HamsterGame.h +++ b/src/HamsterGame.h @@ -70,6 +70,7 @@ public: MARATHON, }; const static std::string ASSETS_DIR; + const static int UNPLAYED; HamsterGame()=delete; HamsterGame(const std::string&appName); static geom2d::rectSCREEN_FRAME; @@ -81,6 +82,7 @@ public: static const Renderable&GetGFX(const std::string&img); static const Animate2D::Animation&GetAnimations(const std::string&img); static const Animate2D::FrameSequence&GetAnimation(const std::string&img,const AnimationState::AnimationState state); + static std::unordered_mapmapPBs; static HamsterGame&Game(); static std::unordered_mapANIMATED_TILE_IDS; const double GetRuntime()const; @@ -96,6 +98,9 @@ public: virtual void OnTextEntryComplete(const std::string& sText)override; const Difficulty&GetMapDifficulty()const; void OnPlayerFinishedRace(); + const GameMode GetGameMode(); + static void SavePB(const std::string&mapName,int ms); + static void LoadPBs(); private: void UpdateGame(const float fElapsedTime); void DrawGame(); @@ -135,7 +140,24 @@ private: GameMode mode{GameMode::SINGLE_RACE}; HamsterNet net; #ifndef __EMSCRIPTEN__ + #ifndef __DEBUG__ SplashScreen splash; #endif + #endif bool netInitialized{false}; + std::vectormapNameList{ + "StageI.tmx", + "StageII.tmx", + "StageIII.tmx", + "StageIV.tmx", + "StageV.tmx", + "StageVI.tmx", + "StageVII.tmx", + "StageVIII.tmx", + "StageIX.tmx", + "StageX.tmx", + "StageXI.tmx", + "StageXII.tmx", + }; + std::string emscripten_temp_val{"123456"}; }; \ No newline at end of file diff --git a/src/HamsterNet.cpp b/src/HamsterNet.cpp index 594b471..2e528fe 100644 --- a/src/HamsterNet.cpp +++ b/src/HamsterNet.cpp @@ -227,13 +227,18 @@ bool HamsterNet::StartRace(const std::string& map) return (hamsterNet__startRace() == 1); } -bool HamsterNet::FinishRace() +int HamsterNet::GetCurrentRaceTime(const std::string& map){ + std::chrono::duration duration = std::chrono::system_clock::now() - m_tp1; + return static_cast(duration.count()); +} + +std::pair HamsterNet::FinishRace() { m_tp2 = std::chrono::system_clock::now(); std::chrono::duration duration = m_tp2 - m_tp1; m_time = static_cast(duration.count()); - return (hamsterNet__finishRace(m_map.c_str(), m_color.c_str(), m_time) == 1); + return {m_time,(hamsterNet__finishRace(m_map.c_str(), m_color.c_str(), m_time) == 1)}; } std::vector HamsterNet::GetLeaderboard(const std::string& map, const int offset, const int limit, const std::string& sortBy, bool ascending) diff --git a/src/HamsterNet.h b/src/HamsterNet.h index 13c0db5..c4718ff 100644 --- a/src/HamsterNet.h +++ b/src/HamsterNet.h @@ -28,7 +28,9 @@ public: bool SetName(const std::string& name); bool StartRace(const std::string& map); - bool FinishRace(); + using FinishTime=int; + int GetCurrentRaceTime(const std::string& map); + std::pair FinishRace(); std::vector GetLeaderboard(const std::string& map, const int offset = 0, const int limit = 100, const std::string& sortBy = "time", bool ascending = true);