From 220beffeae25bd9fecbe9f4c1e1c8560f85e4fd2 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 20 Aug 2024 05:42:01 -0500 Subject: [PATCH] Hamster Jet platform was lagging behind. Moved platform following to after hamster movement code. Remove all string_view conversions being converted into strings. VERY EXPENSIVE OPERATIONS apparently. --- src/Hamster.cpp | 5 +++-- src/Hamster.h | 2 +- src/HamsterGame.cpp | 22 ++++++++++++---------- src/HamsterGame.h | 6 +++--- src/HamsterJet.cpp | 5 ++++- src/HamsterJet.h | 3 ++- src/SpecialRenderable.cpp | 2 +- src/SpecialRenderable.h | 2 +- 8 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Hamster.cpp b/src/Hamster.cpp index abec064..9f544aa 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -50,7 +50,7 @@ const std::vectorHamster::NPC_HAMSTER_IMAGES{ const std::string Hamster::PLAYER_HAMSTER_IMAGE{"hamster.png"}; std::optionalHamster::playerHamster; -Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled) +Hamster::Hamster(const vf2d spawnPos,const std::string&img,const PlayerControlled IsPlayerControlled) :pos(spawnPos),IsPlayerControlled(IsPlayerControlled){ animations=HamsterGame::GetAnimations(img); animations.ChangeState(internalAnimState,HamsterGame::DEFAULT); @@ -116,7 +116,6 @@ void Hamster::UpdateHamsters(const float fElapsedTime){ } }break; } - if(h.hamsterJet.has_value())h.hamsterJet.value().Update(fElapsedTime); if(h.state!=FLYING){ if((h.GetTerrainStandingOn()==Terrain::OCEAN||h.GetTerrainStandingOn()==Terrain::VOID||!h.HasPowerup(Powerup::SWAMP)&&h.GetTerrainStandingOn()==Terrain::SWAMP)&&h.state!=DROWNING&&h.state!=WAIT)h.drownTimer+=fElapsedTime; else if((!h.HasPowerup(Powerup::LAVA)&&h.GetTerrainStandingOn()==Terrain::LAVA)&&h.state!=BURNING&&h.state!=WAIT)h.burnTimer+=fElapsedTime; @@ -133,6 +132,7 @@ void Hamster::UpdateHamsters(const float fElapsedTime){ h.state=BURNING; } } + if(h.hamsterJet.has_value())h.hamsterJet.value().Update(fElapsedTime); h.TurnTowardsTargetDirection(); h.MoveHamster(); if(h.IsPlayerControlled){ @@ -285,6 +285,7 @@ void Hamster::MoveHamster(){ vel=vf2d{std::max(0.f,currentVel.polar().x-GetFriction()*HamsterGame::Game().GetElapsedTime()),currentVel.polar().y}.cart(); } #pragma endregion + if(hamsterJet.has_value()&&hamsterJet.value().GetState()==HamsterJet::HAMSTER_CONTROL)hamsterJet.value().SetPos(GetPos()); //Hamster Jet lagging behind hack fix. } void Hamster::HandleCollision(){ diff --git a/src/Hamster.h b/src/Hamster.h index f9196ad..9fef6bb 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -111,7 +111,7 @@ class Hamster{ float jetFuelDisplayAmt{0.f}; float knockoutTimer{0.f}; public: - Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC); + Hamster(const vf2d spawnPos,const std::string&img,const PlayerControlled IsPlayerControlled=NPC); static const Hamster&GetPlayer(); static void UpdateHamsters(const float fElapsedTime); static void LoadHamsters(const vf2d startingLoc); diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index 8e1fb72..c8625ca 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -59,14 +59,14 @@ void HamsterGame::LoadGraphics(){ } void HamsterGame::LoadAnimations(){ - auto LoadImageIfRequired=[this](const std::string_view img){if(!GFX.count(std::string(img)))_LoadImage(img);}; - auto LoadStillAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string_view img){ + auto LoadImageIfRequired=[this](const std::string&img){if(!GFX.count(std::string(img)))_LoadImage(img);}; + auto LoadStillAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string&img){ Animate2D::FrameSequence stillAnimation{0.f,Animate2D::Style::OneShot}; LoadImageIfRequired(img); stillAnimation.AddFrame(Animate2D::Frame{&GetGFX(img),{{},GetGFX(img).Sprite()->Size()}}); ANIMATIONS[std::string(img)].AddState(state,stillAnimation); }; - auto LoadAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string_view img,const std::vectorframes,const float frameDuration=0.1f,const Animate2D::Style style=Animate2D::Style::Repeat,vf2d frameSize={32,32}){ + auto LoadAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string&img,const std::vectorframes,const float frameDuration=0.1f,const Animate2D::Style style=Animate2D::Style::Repeat,vf2d frameSize={32,32}){ Animate2D::FrameSequence newAnimation{frameDuration,style}; LoadImageIfRequired(img); for(const vf2d&framePos:frames){ @@ -154,7 +154,9 @@ void HamsterGame::UpdateGame(const float fElapsedTime){ } void HamsterGame::DrawGame(){ + SetZ(-0.001f); tv.DrawPartialDecal({-3200,-3200},currentMap.value().GetData().GetMapData().MapSize*16+vf2d{6400,6400},animatedWaterTile.Decal(),{0,0},currentMap.value().GetData().GetMapData().MapSize*16+vf2d{6400,6400}); + SetZ(0.f); DrawLevelTiles(); Powerup::DrawPowerups(tv); Hamster::DrawHamsters(tv); @@ -275,13 +277,13 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){ return true; } -const Renderable&HamsterGame::GetGFX(const std::string_view img){ - if(!GFX.count(std::string(img)))throw std::runtime_error{std::format("Image {} does not exist!",img)}; - return GFX[std::string(img)]; +const Renderable&HamsterGame::GetGFX(const std::string&img){ + if(!GFX.count(img))throw std::runtime_error{std::format("Image {} does not exist!",img)}; + return GFX[img]; } -const Animate2D::Animation&HamsterGame::GetAnimations(const std::string_view img){ - if(!ANIMATIONS.count(std::string(img)))throw std::runtime_error{std::format("Animations for {} does not exist!",img)}; - return ANIMATIONS[std::string(img)]; +const Animate2D::Animation&HamsterGame::GetAnimations(const std::string&img){ + if(!ANIMATIONS.count(img))throw std::runtime_error{std::format("Animations for {} does not exist!",img)}; + return ANIMATIONS[img]; } bool HamsterGame::OnUserDestroy(){ @@ -433,7 +435,7 @@ void HamsterGame::Apply3DTransform(std::vector&decals){ std::copy(foregroundDecals.begin(),foregroundDecals.end(),std::back_inserter(decals)); } -const Animate2D::FrameSequence&HamsterGame::GetAnimation(const std::string_view img,const AnimationState state){ +const Animate2D::FrameSequence&HamsterGame::GetAnimation(const std::string&img,const AnimationState state){ return GetAnimations(img).GetFrames(state); } diff --git a/src/HamsterGame.h b/src/HamsterGame.h index d595d0a..4a923f6 100644 --- a/src/HamsterGame.h +++ b/src/HamsterGame.h @@ -75,9 +75,9 @@ public: bool OnUserUpdate(float fElapsedTime)override final; bool OnUserDestroy()override final; - static const Renderable&GetGFX(const std::string_view img); - static const Animate2D::Animation&GetAnimations(const std::string_view img); - static const Animate2D::FrameSequence&GetAnimation(const std::string_view img,const AnimationState state); + 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 state); static HamsterGame&Game(); static std::unordered_mapANIMATED_TILE_IDS; const double GetRuntime()const; diff --git a/src/HamsterJet.cpp b/src/HamsterJet.cpp index d3edb10..f6a7d2a 100644 --- a/src/HamsterJet.cpp +++ b/src/HamsterJet.cpp @@ -88,7 +88,6 @@ void HamsterJet::Update(const float fElapsedTime){ case HAMSTER_CONTROL:{ jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF; HandleJetControls(); - pos=hamster.GetPos(); }break; case LANDING:{ jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF; @@ -212,4 +211,8 @@ void HamsterJet::DrawOverlay()const{ float meterHeight{meterEndY-meterStartY}; HamsterGame::Game().DrawPartialDecal(HamsterGame::SCREEN_FRAME.pos+vf2d{0,222}-vf2d{0,(fallSpd/5.f)*meterHeight},HamsterGame::GetGFX("fallometer.png").Decal(),vf2d{0,223}-vf2d{0,(fallSpd/5.f)*meterHeight},vf2d{float(HamsterGame::GetGFX("fallometer.png").Sprite()->width),(fallSpd/5.f)*meterHeight}); } +} + +void HamsterJet::SetPos(const vf2d pos){ + this->pos=pos; } \ No newline at end of file diff --git a/src/HamsterJet.h b/src/HamsterJet.h index 400e761..6805d8b 100644 --- a/src/HamsterJet.h +++ b/src/HamsterJet.h @@ -75,7 +75,7 @@ private: SpecialRenderable jet; SpecialRenderable lights; float timer{}; - std::arrayjetState; + std::arrayjetState{}; float lastTappedSpace{}; public: HamsterJet(Hamster&hamster); @@ -84,4 +84,5 @@ public: void DrawOverlay()const; void HandleJetControls(); const State GetState()const; + void SetPos(const vf2d pos); }; \ No newline at end of file diff --git a/src/SpecialRenderable.cpp b/src/SpecialRenderable.cpp index b82258c..df2f557 100644 --- a/src/SpecialRenderable.cpp +++ b/src/SpecialRenderable.cpp @@ -42,7 +42,7 @@ All rights reserved. SpecialRenderable::SpecialRenderable(){}; -void SpecialRenderable::Initialize(std::string_view imgName,const Pixel overrideCol,const Pixel matrixCol){ +void SpecialRenderable::Initialize(const std::string&imgName,const Pixel overrideCol,const Pixel matrixCol){ IsInitialized=true; originalImgName=imgName; this->overrideCol=overrideCol; diff --git a/src/SpecialRenderable.h b/src/SpecialRenderable.h index 9f1b5b1..c99b529 100644 --- a/src/SpecialRenderable.h +++ b/src/SpecialRenderable.h @@ -48,7 +48,7 @@ class SpecialRenderable{ bool IsInitialized{false}; public: SpecialRenderable(); - void Initialize(std::string_view imgName,const Pixel overrideCol,const Pixel matrixCol); + void Initialize(const std::string&imgName,const Pixel overrideCol,const Pixel matrixCol); void Update(const float fElapsedTime); const Renderable&Get()const; Decal*Decal()const;