Fix string parsing emscripten side when loading data. Hamster color choice added.

This commit is contained in:
sigonasr2 2024-08-25 02:47:36 -05:00
parent 8a7ae48031
commit c3fc8ec00a
7 changed files with 68 additions and 18 deletions

View File

@ -1 +1 @@
2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 0.7 0.6 OneLoneHam Yellow
2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 0.7 0.6 OneLoneHam Black

BIN
assets/smallbutton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 756 B

View File

@ -231,9 +231,9 @@ void Hamster::CreateHamsters(const HamsterGame::GameMode mode){
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(vf2d{},PLAYER_HAMSTER_IMAGE,PLAYER_CONTROLLED);
playerHamster=&HAMSTER_LIST.emplace_back(vf2d{},HamsterGame::Game().GetPlayerHamsterImage(),PLAYER_CONTROLLED);
std::vector<std::string>hamsterColorChoices{NPC_HAMSTER_IMAGES};
std::erase(hamsterColorChoices,PLAYER_HAMSTER_IMAGE);
std::erase(hamsterColorChoices,HamsterGame::Game().GetPlayerHamsterImage());
for(int i:std::ranges::iota_view(0U,NPC_HAMSTER_COUNT)){
std::string colorChoice{hamsterColorChoices.at(util::random()%hamsterColorChoices.size())};
std::erase(hamsterColorChoices,colorChoice);

View File

@ -34,7 +34,7 @@ bool HamsterGame::OnUserCreate(){
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);
*((float*)(arg))=stof(rawMetadata.substr(0,length));
std::cout<<std::format("Success! Loaded BGM Volume {}",*((float*)(arg)))<<std::endl;
},
[](void*arg){
@ -43,7 +43,7 @@ bool HamsterGame::OnUserCreate(){
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);
*((float*)(arg))=stof(rawMetadata.substr(0,length));
std::cout<<std::format("Success! Loaded SFX Volume {}",*((float*)(arg)))<<std::endl;
},
[](void*arg){
@ -52,7 +52,7 @@ bool HamsterGame::OnUserCreate(){
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::string*)(arg))=rawMetadata.substr(0,length);
std::cout<<std::format("Success! Loaded Player Name {}",*((std::string*)(arg)))<<std::endl;
},
[](void*arg){
@ -61,7 +61,7 @@ bool HamsterGame::OnUserCreate(){
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::string*)(arg))=rawMetadata.substr(0,length);
std::cout<<std::format("Success! Loaded Hamster Color {}",*((std::string*)(arg)))<<std::endl;
},
[](void*arg){
@ -160,6 +160,8 @@ void HamsterGame::LoadGraphics(){
_LoadImage("highlight_button3.png");
_LoadImage("button4.png");
_LoadImage("highlight_button4.png");
_LoadImage("smallbutton.png");
_LoadImage("smallhighlight_button.png");
}
void HamsterGame::LoadAnimations(){
@ -651,7 +653,7 @@ void HamsterGame::LoadPBs(){
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::cout<<rawMetadata<<std::endl;
HamsterGame::mapPBs[*((std::string*)(arg))]=stoi(rawMetadata);
HamsterGame::mapPBs[*((std::string*)(arg))]=stoi(rawMetadata.substr(0,length));
std::cout<<std::format("Success! PB for {} is {}",*((std::string*)(arg)),HamsterGame::mapPBs[*((std::string*)(arg))])<<std::endl;
},
[](void*arg){
@ -770,6 +772,10 @@ void HamsterGame::QuitGame(){
gameIsRunning=false;
}
const std::string&HamsterGame::GetPlayerHamsterImage()const{
return std::format("hamster{}.png",std::distance(hamsterColorNames.begin(),std::find(hamsterColorNames.begin(),hamsterColorNames.end(),hamsterColor))+1);
}
int main()
{
HamsterGame game("Project Hamster");

View File

@ -114,6 +114,7 @@ public:
void QuitGame();
std::string playerName{"OneLoneHamster"};
void SaveOptions();
const std::string&GetPlayerHamsterImage()const;
private:
void UpdateGame(const float fElapsedTime);
void DrawGame();
@ -159,6 +160,16 @@ private:
#endif
#endif
bool netInitialized{false};
std::vector<std::string>hamsterColorNames{
"Yellow",
"Pink",
"Cyan",
"Black",
"Green",
"Purple"
"Red",
"Blue",
};
std::vector<std::string>mapNameList{
"StageI.tmx",
"StageII.tmx",
@ -178,16 +189,6 @@ private:
"Marathon",
};
std::string emscripten_temp_val{"123456"};
std::vector<std::string>hamsterColorNames{
"Yellow",
"Pink",
"Cyan",
"Black",
"Green",
"Purple"
"Red",
"Blue",
};
HamsterLeaderboard leaderboard;
std::queue<std::string>mapSetList{};
Menu menu;

View File

@ -171,6 +171,28 @@ std::vector<Menu::Button>Menu::GetMenuButtons(const MenuType type){
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(HamsterGame::SCREEN_FRAME.size/2+vf2d{0.f,64.f},"","smallbutton.png","smallhighlight_button.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
int colorInd{0};
for(int ind{0};const std::string&color:HamsterGame::Game().hamsterColorNames){
if(color==HamsterGame::Game().hamsterColor){
colorInd=ind;
break;
}
ind++;
}
HamsterGame::Game().hamsterColor=HamsterGame::Game().hamsterColorNames[(colorInd+1)%HamsterGame::Game().hamsterColorNames.size()];
HamsterGame::Game().emscripten_temp_val=HamsterGame::Game().hamsterColor;
#ifdef __EMSCRIPTEN__
emscripten_idb_async_store("hamster",HamsterGame::Game().hamsterColorLabel.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(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;
}
@ -349,7 +371,28 @@ void Menu::Button::Draw(HamsterGame&game,const vf2d&offset,std::optional<std::re
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 if(buttonImg=="smallbutton.png"){
int colorInd{0};
for(int ind{0};const std::string&color:game.hamsterColorNames){
if(color==game.hamsterColor){
colorInd=ind;
break;
}
ind++;
}
game.DrawPartialRotatedDecal(pos+offset,game.GetGFX(std::format("hamster{}.png",colorInd+1)).Decal(),0.f,{8.f,6.f},{64.f,64.f},{16.f,12.f});
}else game.DrawRotatedStringPropDecal(pos+offset,buttonText,0.f,game.GetTextSizeProp(buttonText)/2,highlightTextCol);
}else if(buttonImg=="smallbutton.png"){
game.DrawRotatedDecal(pos+offset,game.GetGFX(buttonImg).Decal(),0.f,game.GetGFX(buttonImg).Sprite()->Size()/2);
int colorInd{0};
for(int ind{0};const std::string&color:game.hamsterColorNames){
if(color==game.hamsterColor){
colorInd=ind;
break;
}
ind++;
}
game.DrawPartialRotatedDecal(pos+offset,game.GetGFX(std::format("hamster{}.png",colorInd+1)).Decal(),0.f,{8.f,6.f},{64.f,64.f},{16.f,12.f});
}else{
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);