Add Player Options Menu

sigonasr2 3 months ago
parent c358f7e3c8
commit 8a7ae48031
  1. 2
      assets/PBData
  2. BIN
      assets/longbutton2.png
  3. BIN
      assets/longhighlight_button2.png
  4. 4
      src/HamsterAI.cpp
  5. 1
      src/HamsterAI.h
  6. 137
      src/HamsterGame.cpp
  7. 12
      src/HamsterGame.h
  8. 89
      src/Menu.cpp
  9. 8
      src/Menu.h
  10. 2
      src/TODO.txt
  11. 39
      src/olcPixelGameEngine.h

@ -1 +1 @@
48889 2147483647 2147483647 2147483647 109086 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 0.7 0.6 OneLoneHam Yellow

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

@ -148,4 +148,8 @@ const HamsterAI::ActionOptRef HamsterAI::PeekNextAction(){
void HamsterAI::OnBoost(const vi2d pos){ void HamsterAI::OnBoost(const vi2d pos){
recordedActions.emplace_back(pos,Action::BOOST); recordedActions.emplace_back(pos,Action::BOOST);
}
const bool HamsterAI::IsRecording(){
return recordingMode;
} }

@ -82,6 +82,7 @@ public:
static void Update(const float fElapsedTime); static void Update(const float fElapsedTime);
static void DrawOverlay(); static void DrawOverlay();
static const bool IsRecording();
private: private:
static bool recordingMode; static bool recordingMode;
static std::vector<Action>recordedActions; static std::vector<Action>recordedActions;

@ -24,8 +24,51 @@ HamsterGame::HamsterGame(const std::string&appName){
bool HamsterGame::OnUserCreate(){ bool HamsterGame::OnUserCreate(){
CreateLayer(); CreateLayer();
EnableLayer(1U,true); EnableLayer(1U,true);
for(const std::string&map:mapNameList){
mapPBs[map]=std::numeric_limits<int>::max();
}
LoadPBs(); LoadPBs();
audio.SetBackgroundPlay(true); audio.SetBackgroundPlay(true);
#ifdef __EMSCRIPTEN__
emscripten_idb_async_load("hamster",Game().bgmVolLabel.c_str(),&Game().bgmVol,[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
*((float*)(arg))=stof(rawMetadata);
std::cout<<std::format("Success! Loaded BGM Volume {}",*((float*)(arg)))<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to load BGM Volume")<<std::endl;
});
emscripten_idb_async_load("hamster",Game().sfxVolLabel.c_str(),&Game().sfxVol,[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
*((float*)(arg))=stof(rawMetadata);
std::cout<<std::format("Success! Loaded SFX Volume {}",*((float*)(arg)))<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to load SFX Volume")<<std::endl;
});
emscripten_idb_async_load("hamster",Game().playerNameLabel.c_str(),&Game().playerName,[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
*((std::string*)(arg))=rawMetadata;
std::cout<<std::format("Success! Loaded Player Name {}",*((std::string*)(arg)))<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to load Player Name")<<std::endl;
});
emscripten_idb_async_load("hamster",Game().hamsterColorLabel.c_str(),&Game().hamsterColor,[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
*((std::string*)(arg))=std::string(rawMetadata);
std::cout<<std::format("Success! Loaded Hamster Color {}",*((std::string*)(arg)))<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to load Hamster Color")<<std::endl;
});
#endif
olc::GFX3D::ConfigureDisplay(); olc::GFX3D::ConfigureDisplay();
camera=Camera2D{SCREEN_FRAME.size}; camera=Camera2D{SCREEN_FRAME.size};
camera.SetMode(Camera2D::Mode::LazyFollow); camera.SetMode(Camera2D::Mode::LazyFollow);
@ -111,6 +154,8 @@ void HamsterGame::LoadGraphics(){
_LoadImage("highlight_button.png"); _LoadImage("highlight_button.png");
_LoadImage("button2.png"); _LoadImage("button2.png");
_LoadImage("highlight_button2.png"); _LoadImage("highlight_button2.png");
_LoadImage("longbutton2.png");
_LoadImage("longhighlight_button2.png");
_LoadImage("button3.png"); _LoadImage("button3.png");
_LoadImage("highlight_button3.png"); _LoadImage("highlight_button3.png");
_LoadImage("button4.png"); _LoadImage("button4.png");
@ -359,9 +404,8 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){
if(!netInitialized){ if(!netInitialized){
net.InitSession(); net.InitSession();
netInitialized=true; netInitialized=true;
net.SetName(playerName);
net.SetName("OneLoneHamster"); net.SetColor(hamsterColor);
net.SetColor(hamsterColorNames[0]);
} }
runTime+=fElapsedTime; runTime+=fElapsedTime;
@ -549,7 +593,8 @@ const std::string&HamsterGame::GetCurrentMapName()const{
} }
void HamsterGame::OnTextEntryComplete(const std::string& sText){ void HamsterGame::OnTextEntryComplete(const std::string& sText){
HamsterAI::OnTextEntryComplete(sText); if(HamsterAI::IsRecording())HamsterAI::OnTextEntryComplete(sText);
else menu.OnTextEntryComplete(sText);
} }
const Difficulty&HamsterGame::GetMapDifficulty()const{ const Difficulty&HamsterGame::GetMapDifficulty()const{
@ -562,6 +607,17 @@ void HamsterGame::OnPlayerFinishedRace(){
if(result.second)HamsterGame::SavePB(GetCurrentMapName(),result.first); if(result.second)HamsterGame::SavePB(GetCurrentMapName(),result.first);
} }
void HamsterGame::SaveOptions(){
#ifndef __EMSCRIPTEN__
std::ofstream file{ASSETS_DIR+"PBData"};
for(const std::string&mapName:Game().mapNameList){
file<<mapPBs[mapName]<<" ";
}
file<<Game().bgmVol<<" "<<Game().sfxVol<<" "<<Game().playerName<<" "<<Game().hamsterColor;
file.close();
#endif
}
void HamsterGame::SavePB(const std::string&mapName,int ms){ void HamsterGame::SavePB(const std::string&mapName,int ms){
auto it=std::find(Game().mapNameList.begin(),Game().mapNameList.end(),mapName); auto it=std::find(Game().mapNameList.begin(),Game().mapNameList.end(),mapName);
if(it==Game().mapNameList.end())throw std::runtime_error{std::format("WARNING! Somehow got a map name that is invalid! Map Name: {}. THIS SHOULD NOT BE HAPPENING!",mapName)}; if(it==Game().mapNameList.end())throw std::runtime_error{std::format("WARNING! Somehow got a map name that is invalid! Map Name: {}. THIS SHOULD NOT BE HAPPENING!",mapName)};
@ -571,7 +627,7 @@ void HamsterGame::SavePB(const std::string&mapName,int ms){
mapPBs[mapName]=ms; mapPBs[mapName]=ms;
Game().emscripten_temp_val=std::to_string(ms); Game().emscripten_temp_val=std::to_string(ms);
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",Game().mapNameList[std::distance(Game().mapNameList.begin(),it)].c_str(),&Game().emscripten_temp_val,Game().emscripten_temp_val.length(),0,[](void*args){ emscripten_idb_async_store("hamster",Game().mapNameList[std::distance(Game().mapNameList.begin(),it)].c_str(),Game().emscripten_temp_val.data(),Game().emscripten_temp_val.length(),0,[](void*args){
std::cout<<"Success!"<<std::endl; std::cout<<"Success!"<<std::endl;
}, },
[](void*args){ [](void*args){
@ -582,38 +638,59 @@ void HamsterGame::SavePB(const std::string&mapName,int ms){
for(const std::string&mapName:Game().mapNameList){ for(const std::string&mapName:Game().mapNameList){
file<<mapPBs[mapName]<<" "; file<<mapPBs[mapName]<<" ";
} }
file<<Game().bgmVol<<" "<<Game().sfxVol<<" "<<Game().playerName<<" "<<Game().hamsterColor;
file.close(); file.close();
#endif #endif
} }
} }
void HamsterGame::LoadPBs(){ void HamsterGame::LoadPBs(){
for(int i{0};const std::string&mapName:Game().mapNameList){ #ifdef __EMSCRIPTEN__
mapPBs[mapName]=UNPLAYED; for(int i{0};const std::string&mapName:Game().mapNameList){
#ifdef __EMSCRIPTEN__ mapPBs[mapName]=UNPLAYED;
emscripten_idb_async_load("hamster",Game().mapNameList[i].c_str(),&Game().mapNameList[i],[](void*arg,void*data,int length){ emscripten_idb_async_load("hamster",Game().mapNameList[i].c_str(),&Game().mapNameList[i],[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data; std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl; std::cout<<rawMetadata<<std::endl;
HamsterGame::mapPBs[*((std::string*)(arg))]=stoi(rawMetadata); HamsterGame::mapPBs[*((std::string*)(arg))]=stoi(rawMetadata);
std::cout<<std::format("Success! PB for {} is {}",*((std::string*)(arg)),HamsterGame::mapPBs[*((std::string*)(arg))])<<std::endl; std::cout<<std::format("Success! PB for {} is {}",*((std::string*)(arg)),HamsterGame::mapPBs[*((std::string*)(arg))])<<std::endl;
}, },
[](void*arg){ [](void*arg){
std::cout<<std::format("Failed to retrieve PB for {}",*((std::string*)(arg)))<<std::endl; std::cout<<std::format("Failed to retrieve PB for {}",*((std::string*)(arg)))<<std::endl;
}); });
#else i++;
std::ifstream file{ASSETS_DIR+"PBData"}; }
int readCounter{0}; #endif
while(file.good()){ #ifndef __EMSCRIPTEN__
if(readCounter>=Game().mapNameList.size())break; std::ifstream file{ASSETS_DIR+"PBData"};
int readVal; int readCounter{0};
file>>readVal; while(file.good()){
mapPBs[Game().mapNameList[readCounter]]=readVal; switch(readCounter){
readCounter++; case 16:{
file>>Game().bgmVol;
std::cout<<Game().bgmVol<<std::endl;
}break;
case 17:{
file>>Game().sfxVol;
std::cout<<Game().sfxVol<<std::endl;
}break;
case 18:{
file>>Game().playerName;
std::cout<<Game().playerName<<std::endl;
}break;
case 19:{
file>>Game().hamsterColor;
std::cout<<Game().hamsterColor<<std::endl;
}break;
default:{
int readVal;
file>>readVal;
mapPBs[Game().mapNameList[readCounter]]=readVal;
}
} }
file.close(); readCounter++;
#endif }
i++; file.close();
} #endif
} }
const HamsterGame::GameMode HamsterGame::GetGameMode(){ const HamsterGame::GameMode HamsterGame::GetGameMode(){

@ -112,6 +112,8 @@ public:
const std::string PopNextMap(); const std::string PopNextMap();
void ProcessMap(); void ProcessMap();
void QuitGame(); void QuitGame();
std::string playerName{"OneLoneHamster"};
void SaveOptions();
private: private:
void UpdateGame(const float fElapsedTime); void UpdateGame(const float fElapsedTime);
void DrawGame(); void DrawGame();
@ -173,6 +175,7 @@ private:
"Grand Prix I", "Grand Prix I",
"Grand Prix II", "Grand Prix II",
"Grand Prix III", "Grand Prix III",
"Marathon",
}; };
std::string emscripten_temp_val{"123456"}; std::string emscripten_temp_val{"123456"};
std::vector<std::string>hamsterColorNames{ std::vector<std::string>hamsterColorNames{
@ -195,4 +198,11 @@ private:
int totalOperationsCount{}; int totalOperationsCount{};
int operationsProgress{}; int operationsProgress{};
bool gameIsRunning{true}; bool gameIsRunning{true};
}; std::string bgmVolLabel{"bgmVol"};
std::string sfxVolLabel{"sfxVol"};
std::string playerNameLabel{"playerName"};
std::string hamsterColorLabel{"hamsterColor"};
float bgmVol{0.7f};
float sfxVol{0.7f};
std::string hamsterColor{hamsterColorNames[0]};
};

@ -42,6 +42,7 @@ All rights reserved.
#include "Hamster.h" #include "Hamster.h"
void Menu::UpdateAndDraw(HamsterGame&game,const float fElapsedTime){ void Menu::UpdateAndDraw(HamsterGame&game,const float fElapsedTime){
if(HamsterGame::Game().IsTextEntryEnabled()||ignoreInputs)goto Drawing;
menuTransitionRefreshTimer-=fElapsedTime; menuTransitionRefreshTimer-=fElapsedTime;
for(int i{0};const Button&b:menuButtons){ for(int i{0};const Button&b:menuButtons){
@ -74,6 +75,7 @@ void Menu::UpdateAndDraw(HamsterGame&game,const float fElapsedTime){
} }
} }
Drawing:
game.EnableLayer(0U,menuTimer>0.f); game.EnableLayer(0U,menuTimer>0.f);
switch(currentMenu){ switch(currentMenu){
@ -109,6 +111,7 @@ void Menu::UpdateAndDraw(HamsterGame&game,const float fElapsedTime){
Draw(game,currentMenu,game.SCREEN_FRAME.pos); Draw(game,currentMenu,game.SCREEN_FRAME.pos);
} }
game.border.Update(fElapsedTime); game.border.Update(fElapsedTime);
ignoreInputs=false;
} }
void Menu::Transition(const TransitionType type,const MenuType gotoMenu,const float transitionTime){ void Menu::Transition(const TransitionType type,const MenuType gotoMenu,const float transitionTime){
if(menuTimer>0.f)return; if(menuTimer>0.f)return;
@ -121,22 +124,54 @@ std::vector<Menu::Button>Menu::GetMenuButtons(const MenuType type){
std::vector<Menu::Button>buttons; std::vector<Menu::Button>buttons;
switch(type){ switch(type){
case MAIN_MENU:{ case MAIN_MENU:{
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,-32.f},"Grand Prix","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](){Transition(SHIFT_LEFT,GRAND_PRIX,0.5f);}); buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,-32.f},"Grand Prix","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](Button&self){Transition(SHIFT_LEFT,GRAND_PRIX,0.5f);});
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,0.f},"Single Race","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](){Transition(SHIFT_UP,SINGLE_RACE,0.5f);}); buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,0.f},"Single Race","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](Button&self){Transition(SHIFT_UP,SINGLE_RACE,0.5f);});
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,32.f},"Options","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](){Transition(SHIFT_RIGHT,OPTIONS,0.5f);}); buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,32.f},"Options","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](Button&self){Transition(SHIFT_RIGHT,OPTIONS,0.5f);});
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,64.f},"Quit","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](){Transition(SHIFT_DOWN,QUIT,0.5f);}); buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,64.f},"Quit","button.png","highlight_button.png",Pixel{165,208,96},Pixel{37,134,139},[this](Button&self){Transition(SHIFT_DOWN,QUIT,0.5f);});
}break; }break;
case GRAND_PRIX:{ case GRAND_PRIX:{
//Add more buttons up here! //Add more buttons up here!
buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button3.png","highlight_button3.png",Pixel{145,199,255},Pixel{145,199,255},[this](){Transition(SHIFT_RIGHT,MAIN_MENU,0.5f);}); buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button3.png","highlight_button3.png",Pixel{145,199,255},Pixel{145,199,255},[this](Button&self){Transition(SHIFT_RIGHT,MAIN_MENU,0.5f);});
}break; }break;
case SINGLE_RACE:{ case SINGLE_RACE:{
//Add more buttons up here! //Add more buttons up here!
buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button4.png","highlight_button4.png",Pixel{220,185,155},Pixel{180,140,152},[this](){Transition(SHIFT_DOWN,MAIN_MENU,0.5f);}); buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button4.png","highlight_button4.png",Pixel{220,185,155},Pixel{180,140,152},[this](Button&self){Transition(SHIFT_DOWN,MAIN_MENU,0.5f);});
}break; }break;
case OPTIONS:{ case OPTIONS:{
//Add more buttons up here! buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,-32.f},std::format("BGM: {}",int(round(HamsterGame::Game().bgmVol*100))),"button2.png","highlight_button2.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button2.png","highlight_button2.png",Pixel{114,109,163},Pixel{79,81,128},[this](){Transition(SHIFT_LEFT,MAIN_MENU,0.5f);}); HamsterGame::Game().bgmVol=((int(round(HamsterGame::Game().bgmVol*100))+10)%110)/100.f;
self.buttonText=std::format("BGM: {}",int(round(HamsterGame::Game().bgmVol*100)));
HamsterGame::Game().emscripten_temp_val=std::to_string(HamsterGame::Game().bgmVol);
#ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",HamsterGame::Game().bgmVolLabel.c_str(),HamsterGame::Game().emscripten_temp_val.data(),HamsterGame::Game().emscripten_temp_val.length(),0,[](void*args){
std::cout<<"Success!"<<std::endl;
},
[](void*args){
std::cout<<"Failed"<<std::endl;
});
#else
HamsterGame::Game().SaveOptions();
#endif
});
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,0.f},std::format("SFX: {}",int(round(HamsterGame::Game().sfxVol*100))),"button2.png","highlight_button2.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
HamsterGame::Game().sfxVol=((int(round(HamsterGame::Game().sfxVol*100))+10)%110)/100.f;
self.buttonText=std::format("SFX: {}",int(round(HamsterGame::Game().sfxVol*100)));
HamsterGame::Game().emscripten_temp_val=std::to_string(HamsterGame::Game().sfxVol);
#ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",HamsterGame::Game().sfxVolLabel.c_str(),HamsterGame::Game().emscripten_temp_val.data(),HamsterGame::Game().emscripten_temp_val.length(),0,[](void*args){
std::cout<<"Success!"<<std::endl;
},
[](void*args){
std::cout<<"Failed"<<std::endl;
});
#else
HamsterGame::Game().SaveOptions();
#endif
});
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,32.f},std::format("Player Name: {}",HamsterGame::Game().playerName),"longbutton2.png","longhighlight_button2.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
HamsterGame::Game().TextEntryEnable(true,HamsterGame::Game().playerName);
});
buttons.emplace_back(vf2d{54.f,HamsterGame::SCREEN_FRAME.size.y-24.f},"< Back","button2.png","highlight_button2.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){Transition(SHIFT_LEFT,MAIN_MENU,0.5f);});
}break; }break;
} }
return buttons; return buttons;
@ -281,7 +316,11 @@ void Menu::OnLevelLoaded(){
Powerup::Initialize(HamsterGame::Game().mapPowerupsTemp); Powerup::Initialize(HamsterGame::Game().mapPowerupsTemp);
Checkpoint::Initialize(HamsterGame::Game().checkpointsTemp); Checkpoint::Initialize(HamsterGame::Game().checkpointsTemp);
HamsterGame::Game().audio.SetVolume(HamsterGame::Game().bgm.at(HamsterGame::Game().currentMap.value().GetData().GetBGM()),HamsterGame::Game().bgmVol);
HamsterGame::Game().audio.Play(HamsterGame::Game().bgm.at(HamsterGame::Game().currentMap.value().GetData().GetBGM()),true); HamsterGame::Game().audio.Play(HamsterGame::Game().bgm.at(HamsterGame::Game().currentMap.value().GetData().GetBGM()),true);
HamsterGame::Game().net.SetName(HamsterGame::Game().playerName);
HamsterGame::Game().net.SetColor(HamsterGame::Game().hamsterColor);
Hamster::MoveHamstersToSpawn(HamsterGame::Game().currentMap.value().GetData().GetSpawnZone()); Hamster::MoveHamstersToSpawn(HamsterGame::Game().currentMap.value().GetData().GetSpawnZone());
HamsterGame::Game().countdownTimer=3.f; HamsterGame::Game().countdownTimer=3.f;
@ -294,7 +333,7 @@ void Menu::UpdateLoadingProgress(const float pctLoaded){
loadingPct=pctLoaded; loadingPct=pctLoaded;
} }
Menu::Button::Button(const vf2d pos,const std::string&buttonText,const std::string&buttonImg,const std::string&highlightButtonImg,const Pixel textCol,const Pixel highlightTextCol,const std::function<void()>onClick) Menu::Button::Button(const vf2d pos,const std::string&buttonText,const std::string&buttonImg,const std::string&highlightButtonImg,const Pixel textCol,const Pixel highlightTextCol,const std::function<void(Button&self)>onClick)
:pos(pos),buttonText(buttonText),buttonImg(buttonImg),highlightButtonImg(highlightButtonImg),onClick(onClick),textCol(textCol),highlightTextCol(highlightTextCol){} :pos(pos),buttonText(buttonText),buttonImg(buttonImg),highlightButtonImg(highlightButtonImg),onClick(onClick),textCol(textCol),highlightTextCol(highlightTextCol){}
const bool Menu::Button::IsHovered(const vf2d&offset)const{ const bool Menu::Button::IsHovered(const vf2d&offset)const{
@ -303,7 +342,14 @@ const bool Menu::Button::IsHovered(const vf2d&offset)const{
void Menu::Button::Draw(HamsterGame&game,const vf2d&offset,std::optional<std::reference_wrapper<Button>>selectedButton)const{ void Menu::Button::Draw(HamsterGame&game,const vf2d&offset,std::optional<std::reference_wrapper<Button>>selectedButton)const{
if(selectedButton.has_value()&&&selectedButton.value().get()==this){ if(selectedButton.has_value()&&&selectedButton.value().get()==this){
game.DrawRotatedDecal(pos+offset,game.GetGFX(highlightButtonImg).Decal(),0.f,game.GetGFX(highlightButtonImg).Sprite()->Size()/2); game.DrawRotatedDecal(pos+offset,game.GetGFX(highlightButtonImg).Decal(),0.f,game.GetGFX(highlightButtonImg).Sprite()->Size()/2);
game.DrawRotatedStringPropDecal(pos+offset,buttonText,0.f,game.GetTextSizeProp(buttonText)/2,highlightTextCol); if(game.IsTextEntryEnabled()&&buttonText.starts_with("Player Name")){
std::string blinkingCursor{" "};
if(fmod(game.GetRuntime(),1.f)<0.5f)blinkingCursor="|";
game.DrawRotatedStringPropDecal(pos+offset,std::format("Player Name: {}{}",game.TextEntryGetString(),blinkingCursor),0.f,game.GetTextSizeProp(buttonText)/2,highlightTextCol);
std::string helpText{"Press <ENTER> or <ESC> to finish name entry."};
const vf2d helpTextSize{game.GetTextSizeProp(helpText)};
game.DrawShadowRotatedStringPropDecal(pos+offset+vf2d{0,12.f},helpText,0.f,helpTextSize/2);
}else game.DrawRotatedStringPropDecal(pos+offset,buttonText,0.f,game.GetTextSizeProp(buttonText)/2,highlightTextCol);
}else{ }else{
game.DrawRotatedDecal(pos+offset,game.GetGFX(buttonImg).Decal(),0.f,game.GetGFX(buttonImg).Sprite()->Size()/2); game.DrawRotatedDecal(pos+offset,game.GetGFX(buttonImg).Decal(),0.f,game.GetGFX(buttonImg).Sprite()->Size()/2);
game.DrawRotatedStringPropDecal(pos+offset,buttonText,0.f,game.GetTextSizeProp(buttonText)/2,textCol); game.DrawRotatedStringPropDecal(pos+offset,buttonText,0.f,game.GetTextSizeProp(buttonText)/2,textCol);
@ -311,5 +357,26 @@ void Menu::Button::Draw(HamsterGame&game,const vf2d&offset,std::optional<std::re
} }
void Menu::Button::OnClick(){ void Menu::Button::OnClick(){
onClick(); onClick(*this);
}
void Menu::OnTextEntryComplete(const std::string&text){
HamsterGame::Game().playerName=text.substr(0,30);
HamsterGame::Game().emscripten_temp_val=HamsterGame::Game().playerName;
#ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",HamsterGame::Game().playerNameLabel.c_str(),HamsterGame::Game().emscripten_temp_val.data(),HamsterGame::Game().emscripten_temp_val.length(),0,[](void*args){
std::cout<<"Success!"<<std::endl;
},
[](void*args){
std::cout<<"Failed"<<std::endl;
});
#else
HamsterGame::Game().SaveOptions();
#endif
for(Button&b:menuButtons){
if(b.buttonText.starts_with("Player Name")){
b.buttonText=std::format("Player Name: {}",HamsterGame::Game().playerName);
}
}
ignoreInputs=true;
} }

@ -43,15 +43,15 @@ All rights reserved.
class HamsterGame; class HamsterGame;
class Menu{ class Menu{
class Button{ class Button{
std::string buttonText;
vf2d pos; vf2d pos;
std::string buttonImg; std::string buttonImg;
std::string highlightButtonImg; std::string highlightButtonImg;
Pixel textCol; Pixel textCol;
Pixel highlightTextCol; Pixel highlightTextCol;
std::function<void()>onClick; std::function<void(Button&self)>onClick;
public: public:
Button(const vf2d pos,const std::string&buttonText,const std::string&buttonImg,const std::string&highlightButtonImg,const Pixel textCol,const Pixel highlightTextCol,const std::function<void()>onClick={}); std::string buttonText;
Button(const vf2d pos,const std::string&buttonText,const std::string&buttonImg,const std::string&highlightButtonImg,const Pixel textCol,const Pixel highlightTextCol,const std::function<void(Button&self)>onClick={});
const bool IsHovered(const vf2d&offset)const; const bool IsHovered(const vf2d&offset)const;
void OnClick(); void OnClick();
void Draw(HamsterGame&game,const vf2d&pos,std::optional<std::reference_wrapper<Button>>selectedButton={})const; void Draw(HamsterGame&game,const vf2d&pos,std::optional<std::reference_wrapper<Button>>selectedButton={})const;
@ -103,8 +103,10 @@ class Menu{
void DrawTransition(HamsterGame&game); void DrawTransition(HamsterGame&game);
void OnMenuTransition(); void OnMenuTransition();
std::vector<Button>GetMenuButtons(const MenuType type); std::vector<Button>GetMenuButtons(const MenuType type);
bool ignoreInputs{false};
public: public:
void UpdateAndDraw(HamsterGame&game,const float fElapsedTime); void UpdateAndDraw(HamsterGame&game,const float fElapsedTime);
void OnLevelLoaded(); void OnLevelLoaded();
void UpdateLoadingProgress(const float pctLoaded); void UpdateLoadingProgress(const float pctLoaded);
void OnTextEntryComplete(const std::string&text);
}; };

@ -187,6 +187,8 @@ Unlocks (1 hour)
Leaderboard Management (1 hour) Leaderboard Management (1 hour)
Tutorial Stuff (3 hours) Tutorial Stuff (3 hours)
DO NOT DISTRIBUTE PB FILE!! REMOVE IT BEFORE LAUNCH!
Menus Menus
=========== ===========
Title Screen Title Screen

@ -3879,10 +3879,6 @@ namespace olc
} }
// Check for command characters // Check for command characters
if (GetKey(olc::Key::LEFT).bPressed)
nTextEntryCursor = std::max(0, nTextEntryCursor - 1);
if (GetKey(olc::Key::RIGHT).bPressed)
nTextEntryCursor = std::min(int32_t(sTextEntryString.size()), nTextEntryCursor + 1);
if (GetKey(olc::Key::BACK).bPressed && nTextEntryCursor > 0) if (GetKey(olc::Key::BACK).bPressed && nTextEntryCursor > 0)
{ {
sTextEntryString.erase(nTextEntryCursor-1, 1); sTextEntryString.erase(nTextEntryCursor-1, 1);
@ -3891,40 +3887,9 @@ namespace olc
if (GetKey(olc::Key::DEL).bPressed && nTextEntryCursor < sTextEntryString.size()) if (GetKey(olc::Key::DEL).bPressed && nTextEntryCursor < sTextEntryString.size())
sTextEntryString.erase(nTextEntryCursor, 1); sTextEntryString.erase(nTextEntryCursor, 1);
if (GetKey(olc::Key::UP).bPressed)
{
if (!sCommandHistory.empty())
{
if (sCommandHistoryIt != sCommandHistory.begin())
sCommandHistoryIt--;
nTextEntryCursor = int32_t(sCommandHistoryIt->size());
sTextEntryString = *sCommandHistoryIt;
}
}
if (GetKey(olc::Key::DOWN).bPressed)
{
if (!sCommandHistory.empty())
{
if (sCommandHistoryIt != sCommandHistory.end())
{
sCommandHistoryIt++;
if (sCommandHistoryIt != sCommandHistory.end())
{
nTextEntryCursor = int32_t(sCommandHistoryIt->size());
sTextEntryString = *sCommandHistoryIt;
}
else
{
nTextEntryCursor = 0;
sTextEntryString = "";
}
}
}
}
if (GetKey(olc::Key::ENTER).bPressed) if (GetKey(olc::Key::ENTER).bPressed||GetKey(olc::Key::ESCAPE).bPressed)
{ {
if (bConsoleShow) if (bConsoleShow)
{ {

Loading…
Cancel
Save