From b1e418bbc73589cd79dae5c2133743fb4d9d046e Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 17 Aug 2024 18:46:03 -0500 Subject: [PATCH] Add powerup collection and powerup collection effect. --- assets/shadow.png | Bin 0 -> 587 bytes hamster.vcxproj | 4 ++++ src/Hamster.cpp | 18 ++++++++++++++++-- src/Hamster.h | 4 ++++ src/HamsterGame.cpp | 20 ++++++++++++++++++++ src/Powerup.cpp | 27 ++++++++++++++++++++++++--- src/Powerup.h | 7 ++++++- 7 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 assets/shadow.png diff --git a/assets/shadow.png b/assets/shadow.png new file mode 100644 index 0000000000000000000000000000000000000000..a1a92885eee91a8de300161508d354f35c86ccdc GIT binary patch literal 587 zcmV-R0<`^!P)EX>4Tx04R}tkv&MmKpe$iQ^g_`MT3YqWT;MdQ4z;d#UfZJZG~1HOfLO`CJjl7 zi=*ILaPVWX>fqw6tAnc`2!4P#J2)x2NQwVT3N2ziIPS;0dyl(!fKV?p&FYE(nr@rP zSX9bnSEZg;1TX**gBX^XWyDj9X>=T4_we!cF3ht!pZjz4sX2=QK8ZNO4AUmwAfDZ{ z4bJ<-A}h)&@j3CNNf#u3*o ztDLtuYvn3y-jlyDnA2BMT&FpP7?u!60umHdQ9>Cu!nEq7SV+=-w1a=t^(V-skgEhn zjs;YpL3aJ%fAG6oD?dHuB?Y5E_lx6vi~~KpK%?e3-^Y&AI05|6z?I(eSL(prC+W48 z7CHj@w}Ff6mZt0hmpj17lOdb3EBR>(`8@D`M&FbLhHio0HLthkK29HiGaVH_?!A0000PbVXQnLvL+uWo~o;Lvm$dbY)~9cWHEJAV*0}P*;Ht7XSbN zHAzH4R9M69(m@IUAP4|K{r{u$2~j9@m^q6JVgvvHz+-thzqLevel3 %(PreprocessorDefinitions);WIN32;_WINDOWS;CMAKE_INTDIR="Debug" $(IntDir) + true %(PreprocessorDefinitions);WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\" @@ -149,6 +150,7 @@ $(IntDir) + true %(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\" @@ -193,6 +195,7 @@ $(IntDir) + true %(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"MinSizeRel\" @@ -236,6 +239,7 @@ Level3 %(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="RelWithDebInfo" $(IntDir) + true %(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"RelWithDebInfo\" diff --git a/src/Hamster.cpp b/src/Hamster.cpp index edd6579..b17469d 100644 --- a/src/Hamster.cpp +++ b/src/Hamster.cpp @@ -99,9 +99,9 @@ void Hamster::DrawHamsters(TransformedView&tv){ const Animate2D::Frame&img{h.animations.GetState(h.internalAnimState)==HamsterGame::DEFAULT?anim.GetFrame(h.distanceTravelled/100.f):h.GetCurrentAnimation()}; const Animate2D::Frame&wheelTopImg{wheelTopAnim.GetFrame(h.distanceTravelled/80.f)}; const Animate2D::Frame&wheelBottomImg{wheelBottomAnim.GetFrame(h.distanceTravelled/80.f)}; - tv.DrawPartialRotatedDecal(h.pos,wheelBottomImg.GetSourceImage()->Decal(),h.rot,wheelBottomImg.GetSourceRect().size/2,wheelBottomImg.GetSourceRect().pos,wheelBottomImg.GetSourceRect().size); + if(h.HasPowerup(Powerup::WHEEL))tv.DrawPartialRotatedDecal(h.pos,wheelBottomImg.GetSourceImage()->Decal(),h.rot,wheelBottomImg.GetSourceRect().size/2,wheelBottomImg.GetSourceRect().pos,wheelBottomImg.GetSourceRect().size); tv.DrawPartialRotatedDecal(h.pos,img.GetSourceImage()->Decal(),h.rot,img.GetSourceRect().size/2,img.GetSourceRect().pos,img.GetSourceRect().size); - tv.DrawPartialRotatedDecal(h.pos,wheelTopImg.GetSourceImage()->Decal(),h.rot,wheelTopImg.GetSourceRect().size/2,wheelTopImg.GetSourceRect().pos,wheelTopImg.GetSourceRect().size,{1.f,1.f},{255,255,255,192}); + if(h.HasPowerup(Powerup::WHEEL))tv.DrawPartialRotatedDecal(h.pos,wheelTopImg.GetSourceImage()->Decal(),h.rot,wheelTopImg.GetSourceRect().size/2,wheelTopImg.GetSourceRect().pos,wheelTopImg.GetSourceRect().size,{1.f,1.f},{255,255,255,192}); } } @@ -186,6 +186,12 @@ void Hamster::HandleCollision(){ bumpTimer=h.bumpTimer=0.12f; } } + for(Powerup&powerup:Powerup::GetPowerups()){ + if(!HasPowerup(powerup.GetType())&&geom2d::overlaps(geom2d::circle(GetPos(),collisionRadius),geom2d::circle(powerup.GetPos(),20.f))){ + ObtainPowerup(powerup.GetType()); + powerup.OnPowerupObtain(); + } + } } const float Hamster::GetRadius()const{ @@ -239,4 +245,12 @@ const float Hamster::GetTurnSpeed()const{ const float Hamster::GetBumpAmount()const{ float finalBumpAmt{DEFAULT_BUMP_AMT}; return finalBumpAmt*GetMaxSpeed()/DEFAULT_MAX_SPD; +} + +void Hamster::ObtainPowerup(const Powerup::PowerupType powerup){ + powerups.insert(powerup); + +} +const bool Hamster::HasPowerup(const Powerup::PowerupType powerup)const{ + return powerups.count(powerup); } \ No newline at end of file diff --git a/src/Hamster.h b/src/Hamster.h index 2f0c659..0f79c1b 100644 --- a/src/Hamster.h +++ b/src/Hamster.h @@ -41,6 +41,7 @@ All rights reserved. #include "olcUTIL_Geometry2D.h" #include "olcUTIL_Animate2D.h" #include "Terrain.h" +#include class Hamster{ enum PlayerControlled{ @@ -81,6 +82,7 @@ class Hamster{ PlayerControlled IsPlayerControlled; static std::optionalplayerHamster; HamsterState state{NORMAL}; + std::unordered_setpowerups; public: Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC); static const Hamster&GetPlayer(); @@ -100,4 +102,6 @@ public: const float GetFriction()const; const float GetTurnSpeed()const; const float GetBumpAmount()const; + void ObtainPowerup(const Powerup::PowerupType powerup); + const bool HasPowerup(const Powerup::PowerupType powerup)const; }; \ No newline at end of file diff --git a/src/HamsterGame.cpp b/src/HamsterGame.cpp index 20b192e..2b8778c 100644 --- a/src/HamsterGame.cpp +++ b/src/HamsterGame.cpp @@ -38,6 +38,7 @@ void HamsterGame::_LoadImage(const std::string_view img){ void HamsterGame::LoadGraphics(){ _LoadImage("border.png"); _LoadImage("gametiles.png"); + _LoadImage("shadow.png"); } void HamsterGame::LoadAnimations(){ @@ -96,6 +97,7 @@ void HamsterGame::UpdateGame(const float fElapsedTime){ camera.Update(fElapsedTime); tv.SetWorldOffset(-SCREEN_FRAME.pos+camera.GetViewPosition()); Hamster::UpdateHamsters(fElapsedTime); + Powerup::UpdatePowerups(fElapsedTime); border.Update(fElapsedTime); } @@ -106,6 +108,24 @@ void HamsterGame::DrawGame(){ border.Draw(); DrawStringDecal(SCREEN_FRAME.pos+vf2d{1,1},"Terrain Type: "+Terrain::TerrainToString(Hamster::GetPlayer().GetTerrainStandingOn()),BLACK); DrawStringDecal(SCREEN_FRAME.pos,"Terrain Type: "+Terrain::TerrainToString(Hamster::GetPlayer().GetTerrainStandingOn())); + + for(int y:std::ranges::iota_view(0,4)){ + for(int x:std::ranges::iota_view(0,2)){ + const int powerupInd{y*2+x}; + const float drawX{x*32.f+12.f}; + const float drawY{y*32.f+12.f+96.f}; + const Powerup::PowerupType powerupType{Powerup::PowerupType(powerupInd)}; + const geom2d::rectpowerupSubimageRect{Powerup::GetPowerupSubimageRect(powerupType)}; + if(Hamster::GetPlayer().HasPowerup(powerupType)){ + SetDecalMode(DecalMode::ADDITIVE); + DrawPartialRotatedDecal(vf2d{drawX,drawY}+16,GetGFX("gametiles.png").Decal(),0.f,{16.f,16.f},powerupSubimageRect.pos,powerupSubimageRect.size,{1.1f,1.1f}); + SetDecalMode(DecalMode::NORMAL); + DrawPartialDecal({drawX,drawY},GetGFX("gametiles.png").Decal(),powerupSubimageRect.pos,powerupSubimageRect.size); + }else{ + DrawPartialDecal({drawX,drawY},GetGFX("gametiles.png").Decal(),powerupSubimageRect.pos,powerupSubimageRect.size,{1.f,1.f},VERY_DARK_GREY); + } + } + } } const Terrain::TerrainType HamsterGame::GetTerrainTypeAtPos(const vf2d pos)const{ diff --git a/src/Powerup.cpp b/src/Powerup.cpp index 77be9d3..1180734 100644 --- a/src/Powerup.cpp +++ b/src/Powerup.cpp @@ -55,7 +55,7 @@ const vf2d&Powerup::GetPos()const{ const Powerup::PowerupType&Powerup::GetType()const{ return type; } -const std::vector&Powerup::GetPowerups(){ +std::vector&Powerup::GetPowerups(){ return powerupList; } @@ -88,9 +88,30 @@ const Powerup::PowerupType Powerup::TileIDPowerupType(const int tileId){ return powerupIds.at(tileId).first; } +void Powerup::UpdatePowerups(const float fElapsedTime){ + for(Powerup&powerup:powerupList){ + powerup.z=abs(pow(sin(HamsterGame::Game().GetRuntime()/1.5f*geom2d::pi),4.f)*4)+5; + if(powerup.spinSpd>0.f){ + powerup.spinSpd+=fElapsedTime; + if(powerup.spinSpd>=3.f)powerup.spinSpd=0.f; + } + } +} + void Powerup::DrawPowerups(TransformedView&tv){ for(const Powerup&powerup:powerupList){ - geom2d::rectspriteRect{POWERUP_TILESET_STARTING_POS+vf2d{int(powerup.GetType())*32.f,0.f},{32,32}}; - tv.DrawPartialRotatedDecal(powerup.GetPos(),HamsterGame::GetGFX("gametiles.png").Decal(),0.f,{16,16},spriteRect.pos,spriteRect.size); + geom2d::rectspriteRect{GetPowerupSubimageRect(powerup.GetType())}; + tv.DrawRotatedDecal(powerup.GetPos(),HamsterGame::GetGFX("shadow.png").Decal(),0.f,{16,16}); + float scaleX{1.f}; + if(powerup.spinSpd>0.f)scaleX=sin(geom2d::pi*HamsterGame::Game().GetRuntime()/powerup.spinSpd); + tv.DrawPartialRotatedDecal(powerup.GetPos()-vf2d{0,powerup.z},HamsterGame::GetGFX("gametiles.png").Decal(),0.f,{16,16},spriteRect.pos,spriteRect.size,{scaleX,1.f}); } +} + +const geom2d::rectPowerup::GetPowerupSubimageRect(const PowerupType powerupType){ + return {POWERUP_TILESET_STARTING_POS+vf2d{int(powerupType)*32.f,0.f},{32,32}}; +} + +void Powerup::OnPowerupObtain(){ + spinSpd=0.3f; } \ No newline at end of file diff --git a/src/Powerup.h b/src/Powerup.h index a44f880..a8ec92e 100644 --- a/src/Powerup.h +++ b/src/Powerup.h @@ -62,17 +62,22 @@ private: static std::unordered_map>powerupIds; static const vf2d POWERUP_TILESET_STARTING_POS; vf2d pos; + float z{5.f}; + float spinSpd{}; PowerupType type; public: Powerup(const vf2d pos,const PowerupType type); static void Initialize(const std::vector&powerupList); const vf2d&GetPos()const; const PowerupType&GetType()const; - static const std::vector&GetPowerups(); + static std::vector&GetPowerups(); static void AddOrUpdatePowerupIdList(const int powerupId,const PowerupType powerupType); static void AddOrUpdatePowerupIdList(const int powerupId,const TileType powerupTileType); static const bool TileIDIsPowerupTile(const int tileId); static const bool TileIDIsUpperLeftPowerupTile(const int tileId); static const PowerupType TileIDPowerupType(const int tileId); + static void UpdatePowerups(const float fElapsedTime); static void DrawPowerups(TransformedView&tv); + static const geom2d::rectGetPowerupSubimageRect(const PowerupType powerupType); + void OnPowerupObtain(); }; \ No newline at end of file