From 36eb4ce61eae74d88e0fc0a6814b0d1fbced6e1b Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 16 Aug 2024 20:25:54 -0500 Subject: [PATCH] Setup game camera and player transformed view following/offset. --- src/Hamster.cpp | 20 +++++++++++++++++--- src/Hamster.h | 8 ++++++-- src/HamsterGame.cpp | 7 ++++++- src/olcUTIL_Camera2D.h | 4 ++-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Hamster.cpp b/src/Hamster.cpp index 5c0661e..c0c7985 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -42,14 +42,16 @@ All rights reserved. #include std::vectorHamster::HAMSTER_LIST; +const uint8_t Hamster::MAX_HAMSTER_COUNT{100U}; const uint8_t Hamster::NPC_HAMSTER_COUNT{5U}; const std::vectorHamster::NPC_HAMSTER_IMAGES{ "hamster.png", }; const std::string Hamster::PLAYER_HAMSTER_IMAGE{"hamster.png"}; +std::optionalHamster::playerHamster; -Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled playerControlled) -:pos(spawnPos),playerControlled(playerControlled){ +Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled) +:pos(spawnPos),IsPlayerControlled(IsPlayerControlled){ animations=HamsterGame::GetAnimations(img); animations.ChangeState(internalAnimState,HamsterGame::DEFAULT); } @@ -62,7 +64,10 @@ void Hamster::UpdateHamsters(const float fElapsedTime){ void Hamster::LoadHamsters(const vf2d startingLoc){ HAMSTER_LIST.clear(); - HAMSTER_LIST.emplace_back(startingLoc,PLAYER_HAMSTER_IMAGE,PLAYER_CONTROLLED); + playerHamster.reset(); + HAMSTER_LIST.reserve(MAX_HAMSTER_COUNT); + if(NPC_HAMSTER_COUNT+1>MAX_HAMSTER_COUNT)throw std::runtime_error{std::format("WARNING! Max hamster count is too high! Please expand the MAX_HAMSTER_COUNT if you want more hamsters. Requested {} hamsters.",MAX_HAMSTER_COUNT)}; + playerHamster=&HAMSTER_LIST.emplace_back(startingLoc,PLAYER_HAMSTER_IMAGE,PLAYER_CONTROLLED); for(int i:std::ranges::iota_view(0U,NPC_HAMSTER_COUNT)){ HAMSTER_LIST.emplace_back(startingLoc,NPC_HAMSTER_IMAGES.at(util::random()%NPC_HAMSTER_IMAGES.size()),NPC); } @@ -77,4 +82,13 @@ void Hamster::DrawHamsters(TransformedView&tv){ const Animate2D::Frame&Hamster::GetCurrentAnimation()const{ return animations.GetFrame(internalAnimState); +} + +const Hamster&Hamster::GetPlayer(){ + if(!playerHamster.has_value())throw std::runtime_error{std::format("WARNING! Player is not created at this time! There should not be any code referencing the player at this moment!")}; + return *playerHamster.value(); +} + +const vf2d&Hamster::GetPos()const{ + return pos; } \ No newline at end of file diff --git a/src/Hamster.h b/src/Hamster.h index 0746748..da8b4f9 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -49,6 +49,7 @@ class Hamster{ static std::vectorHAMSTER_LIST; + static const uint8_t MAX_HAMSTER_COUNT; static const uint8_t NPC_HAMSTER_COUNT; static const std::vectorNPC_HAMSTER_IMAGES; @@ -60,11 +61,14 @@ class Hamster{ std::string img; Animate2D::Animationanimations; Animate2D::AnimationState internalAnimState; - PlayerControlled playerControlled; + PlayerControlled IsPlayerControlled; + static std::optionalplayerHamster; public: - Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled playerControlled=NPC); + Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC); + static const Hamster&GetPlayer(); static void UpdateHamsters(const float fElapsedTime); static void LoadHamsters(const vf2d startingLoc); static void DrawHamsters(TransformedView&tv); const Animate2D::Frame&GetCurrentAnimation()const; + const vf2d&GetPos()const; }; \ No newline at end of file diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index 266f1c7..b8498d4 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -13,7 +13,9 @@ HamsterGame::HamsterGame() } bool HamsterGame::OnUserCreate(){ - tv.Initialise({320,288},{1,1}); + camera=Camera2D{SCREEN_FRAME.size}; + camera.SetMode(Camera2D::Mode::LazyFollow); + tv.Initialise(SCREEN_FRAME.size,{1,1}); tv.SetWorldOffset(-SCREEN_FRAME.pos); LoadGraphics(); LoadAnimations(); @@ -54,9 +56,12 @@ void HamsterGame::LoadAnimations(){ void HamsterGame::LoadLevel(){ const vf2d levelSpawnLoc{50,50}; //TEMPORARY Hamster::LoadHamsters(levelSpawnLoc); + camera.SetTarget(Hamster::GetPlayer().GetPos()); } void HamsterGame::UpdateGame(const float fElapsedTime){ + camera.Update(fElapsedTime); + tv.SetWorldOffset(-SCREEN_FRAME.pos+camera.GetViewPosition()); Hamster::UpdateHamsters(fElapsedTime); } diff --git a/src/olcUTIL_Camera2D.h b/src/olcUTIL_Camera2D.h index a482b78..b4591f7 100644 --- a/src/olcUTIL_Camera2D.h +++ b/src/olcUTIL_Camera2D.h @@ -116,7 +116,7 @@ namespace olc::utils } // Set tracked point via pointer - inline void SetTarget(olc::vf2d& vTarget) + inline void SetTarget(const olc::vf2d& vTarget) { m_pTarget = &vTarget; } @@ -242,7 +242,7 @@ namespace olc::utils Mode m_nMode = Mode::Simple; // Target Vector2D object camera should follow (either ref or ptr) - olc::vf2d* m_pTarget = nullptr; + const olc::vf2d* m_pTarget = nullptr; olc::vf2d m_vLocalTarget; // World Boundary