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.

sigonasr2 3 months ago
parent c85b8ebf2c
commit 220beffeae
  1. 5
      src/Hamster.cpp
  2. 2
      src/Hamster.h
  3. 22
      src/HamsterGame.cpp
  4. 6
      src/HamsterGame.h
  5. 5
      src/HamsterJet.cpp
  6. 3
      src/HamsterJet.h
  7. 2
      src/SpecialRenderable.cpp
  8. 2
      src/SpecialRenderable.h

@ -50,7 +50,7 @@ const std::vector<std::string>Hamster::NPC_HAMSTER_IMAGES{
const std::string Hamster::PLAYER_HAMSTER_IMAGE{"hamster.png"}; const std::string Hamster::PLAYER_HAMSTER_IMAGE{"hamster.png"};
std::optional<Hamster*>Hamster::playerHamster; std::optional<Hamster*>Hamster::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){ :pos(spawnPos),IsPlayerControlled(IsPlayerControlled){
animations=HamsterGame::GetAnimations(img); animations=HamsterGame::GetAnimations(img);
animations.ChangeState(internalAnimState,HamsterGame::DEFAULT); animations.ChangeState(internalAnimState,HamsterGame::DEFAULT);
@ -116,7 +116,6 @@ void Hamster::UpdateHamsters(const float fElapsedTime){
} }
}break; }break;
} }
if(h.hamsterJet.has_value())h.hamsterJet.value().Update(fElapsedTime);
if(h.state!=FLYING){ 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; 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; 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; h.state=BURNING;
} }
} }
if(h.hamsterJet.has_value())h.hamsterJet.value().Update(fElapsedTime);
h.TurnTowardsTargetDirection(); h.TurnTowardsTargetDirection();
h.MoveHamster(); h.MoveHamster();
if(h.IsPlayerControlled){ 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(); vel=vf2d{std::max(0.f,currentVel.polar().x-GetFriction()*HamsterGame::Game().GetElapsedTime()),currentVel.polar().y}.cart();
} }
#pragma endregion #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(){ void Hamster::HandleCollision(){

@ -111,7 +111,7 @@ class Hamster{
float jetFuelDisplayAmt{0.f}; float jetFuelDisplayAmt{0.f};
float knockoutTimer{0.f}; float knockoutTimer{0.f};
public: 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 const Hamster&GetPlayer();
static void UpdateHamsters(const float fElapsedTime); static void UpdateHamsters(const float fElapsedTime);
static void LoadHamsters(const vf2d startingLoc); static void LoadHamsters(const vf2d startingLoc);

@ -59,14 +59,14 @@ void HamsterGame::LoadGraphics(){
} }
void HamsterGame::LoadAnimations(){ void HamsterGame::LoadAnimations(){
auto LoadImageIfRequired=[this](const std::string_view img){if(!GFX.count(std::string(img)))_LoadImage(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_view img){ auto LoadStillAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string&img){
Animate2D::FrameSequence stillAnimation{0.f,Animate2D::Style::OneShot}; Animate2D::FrameSequence stillAnimation{0.f,Animate2D::Style::OneShot};
LoadImageIfRequired(img); LoadImageIfRequired(img);
stillAnimation.AddFrame(Animate2D::Frame{&GetGFX(img),{{},GetGFX(img).Sprite()->Size()}}); stillAnimation.AddFrame(Animate2D::Frame{&GetGFX(img),{{},GetGFX(img).Sprite()->Size()}});
ANIMATIONS[std::string(img)].AddState(state,stillAnimation); ANIMATIONS[std::string(img)].AddState(state,stillAnimation);
}; };
auto LoadAnimation=[this,&LoadImageIfRequired](const AnimationState state,const std::string_view img,const std::vector<vf2d>frames,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::vector<vf2d>frames,const float frameDuration=0.1f,const Animate2D::Style style=Animate2D::Style::Repeat,vf2d frameSize={32,32}){
Animate2D::FrameSequence newAnimation{frameDuration,style}; Animate2D::FrameSequence newAnimation{frameDuration,style};
LoadImageIfRequired(img); LoadImageIfRequired(img);
for(const vf2d&framePos:frames){ for(const vf2d&framePos:frames){
@ -154,7 +154,9 @@ void HamsterGame::UpdateGame(const float fElapsedTime){
} }
void HamsterGame::DrawGame(){ 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}); 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(); DrawLevelTiles();
Powerup::DrawPowerups(tv); Powerup::DrawPowerups(tv);
Hamster::DrawHamsters(tv); Hamster::DrawHamsters(tv);
@ -275,13 +277,13 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){
return true; return true;
} }
const Renderable&HamsterGame::GetGFX(const std::string_view img){ const Renderable&HamsterGame::GetGFX(const std::string&img){
if(!GFX.count(std::string(img)))throw std::runtime_error{std::format("Image {} does not exist!",img)}; if(!GFX.count(img))throw std::runtime_error{std::format("Image {} does not exist!",img)};
return GFX[std::string(img)]; return GFX[img];
} }
const Animate2D::Animation<HamsterGame::AnimationState>&HamsterGame::GetAnimations(const std::string_view img){ const Animate2D::Animation<HamsterGame::AnimationState>&HamsterGame::GetAnimations(const std::string&img){
if(!ANIMATIONS.count(std::string(img)))throw std::runtime_error{std::format("Animations for {} does not exist!",img)}; if(!ANIMATIONS.count(img))throw std::runtime_error{std::format("Animations for {} does not exist!",img)};
return ANIMATIONS[std::string(img)]; return ANIMATIONS[img];
} }
bool HamsterGame::OnUserDestroy(){ bool HamsterGame::OnUserDestroy(){
@ -433,7 +435,7 @@ void HamsterGame::Apply3DTransform(std::vector<DecalInstance>&decals){
std::copy(foregroundDecals.begin(),foregroundDecals.end(),std::back_inserter(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); return GetAnimations(img).GetFrames(state);
} }

@ -75,9 +75,9 @@ public:
bool OnUserUpdate(float fElapsedTime)override final; bool OnUserUpdate(float fElapsedTime)override final;
bool OnUserDestroy()override final; bool OnUserDestroy()override final;
static const Renderable&GetGFX(const std::string_view img); static const Renderable&GetGFX(const std::string&img);
static const Animate2D::Animation<HamsterGame::AnimationState>&GetAnimations(const std::string_view img); static const Animate2D::Animation<HamsterGame::AnimationState>&GetAnimations(const std::string&img);
static const Animate2D::FrameSequence&GetAnimation(const std::string_view img,const AnimationState state); static const Animate2D::FrameSequence&GetAnimation(const std::string&img,const AnimationState state);
static HamsterGame&Game(); static HamsterGame&Game();
static std::unordered_map<uint32_t,Animate2D::FrameSequence>ANIMATED_TILE_IDS; static std::unordered_map<uint32_t,Animate2D::FrameSequence>ANIMATED_TILE_IDS;
const double GetRuntime()const; const double GetRuntime()const;

@ -88,7 +88,6 @@ void HamsterJet::Update(const float fElapsedTime){
case HAMSTER_CONTROL:{ case HAMSTER_CONTROL:{
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF; jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF;
HandleJetControls(); HandleJetControls();
pos=hamster.GetPos();
}break; }break;
case LANDING:{ case LANDING:{
jetState[TOP_LEFT]=jetState[BOTTOM_LEFT]=jetState[BOTTOM_RIGHT]=jetState[TOP_RIGHT]=OFF; 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}; 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}); 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;
} }

@ -75,7 +75,7 @@ private:
SpecialRenderable jet; SpecialRenderable jet;
SpecialRenderable lights; SpecialRenderable lights;
float timer{}; float timer{};
std::array<JetState,4>jetState; std::array<JetState,4>jetState{};
float lastTappedSpace{}; float lastTappedSpace{};
public: public:
HamsterJet(Hamster&hamster); HamsterJet(Hamster&hamster);
@ -84,4 +84,5 @@ public:
void DrawOverlay()const; void DrawOverlay()const;
void HandleJetControls(); void HandleJetControls();
const State GetState()const; const State GetState()const;
void SetPos(const vf2d pos);
}; };

@ -42,7 +42,7 @@ All rights reserved.
SpecialRenderable::SpecialRenderable(){}; 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; IsInitialized=true;
originalImgName=imgName; originalImgName=imgName;
this->overrideCol=overrideCol; this->overrideCol=overrideCol;

@ -48,7 +48,7 @@ class SpecialRenderable{
bool IsInitialized{false}; bool IsInitialized{false};
public: public:
SpecialRenderable(); 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); void Update(const float fElapsedTime);
const Renderable&Get()const; const Renderable&Get()const;
Decal*Decal()const; Decal*Decal()const;

Loading…
Cancel
Save