Add rotational movement to hamster controls and corresponding option to options menu.

post-jam-rotational-movement
sigonasr2 6 months ago
parent 448ef6a64d
commit bf63336733
  1. BIN
      assets/directionalmovement.png
  2. BIN
      assets/directionalmovement_selected.png
  3. BIN
      assets/rotationalmovement.png
  4. BIN
      assets/rotationalmovement_selected.png
  5. 28
      src/Hamster.cpp
  6. 117
      src/HamsterGame.cpp
  7. 9
      src/HamsterGame.h
  8. 68
      src/Menu.cpp
  9. 8
      src/Menu.h
  10. 10
      src/olcPixelGameEngine.h

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

@ -444,6 +444,8 @@ const vf2d&Hamster::GetPos()const{
void Hamster::HandlePlayerControls(){
lastTappedSpace+=HamsterGame::Game().GetElapsedTime();
vf2d aimingDir{};
switch(HamsterGame::Game().GetSteeringMode()){
case HamsterGame::SteeringMode::DIRECTIONAL:{
if(HamsterGame::Game().GetKey(W).bHeld||HamsterGame::Game().GetKey(UP).bHeld){
aimingDir+=vf2d{0,-1};
}
@ -463,6 +465,32 @@ void Hamster::HandlePlayerControls(){
vel=vf2d{std::min(GetMaxSpeed(),vel.polar().x),vel.polar().y}.cart();
frictionEnabled=false;
}
}break;
case HamsterGame::SteeringMode::ROTATIONAL:{
if(HamsterGame::Game().GetKey(W).bHeld||HamsterGame::Game().GetKey(UP).bHeld){
const vf2d currentVel{vel};
vel=vf2d{currentVel.polar().x+((GetMaxSpeed()/GetTimeToMaxSpeed())*HamsterGame::Game().GetElapsedTime()),rot}.cart();
vel=vf2d{std::min(GetMaxSpeed(),vel.polar().x),vel.polar().y}.cart();
frictionEnabled=false;
}
if(HamsterGame::Game().GetKey(A).bHeld||HamsterGame::Game().GetKey(LEFT).bHeld){
rot-=GetTurnSpeed()*HamsterGame::Game().GetElapsedTime();
targetRot-=GetTurnSpeed()*HamsterGame::Game().GetElapsedTime();
frictionEnabled=false;
}
if(HamsterGame::Game().GetKey(D).bHeld||HamsterGame::Game().GetKey(RIGHT).bHeld){
rot+=GetTurnSpeed()*HamsterGame::Game().GetElapsedTime();
targetRot+=GetTurnSpeed()*HamsterGame::Game().GetElapsedTime();
frictionEnabled=false;
}
if(HamsterGame::Game().GetKey(S).bHeld||HamsterGame::Game().GetKey(DOWN).bHeld){
const vf2d currentVel{vel};
vel=vf2d{currentVel.polar().x+((GetMaxSpeed()/GetTimeToMaxSpeed())*HamsterGame::Game().GetElapsedTime()),float(rot+geom2d::pi)}.cart();
vel=vf2d{std::min(GetMaxSpeed()*0.5f,vel.polar().x),vel.polar().y}.cart();
frictionEnabled=false;
}
}break;
}
if(HamsterGame::Game().GetKey(R).bPressed&&boostCounter>0){
boostCounter--;
boostTimer=1.f;

@ -30,45 +30,6 @@ bool HamsterGame::OnUserCreate(){
LoadPBs();
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.substr(0,length));
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.substr(0,length));
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.substr(0,length);
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))=rawMetadata.substr(0,length);
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();
camera=Camera2D{SCREEN_FRAME.size};
camera.SetMode(Camera2D::Mode::LazyFollow);
@ -165,6 +126,10 @@ void HamsterGame::LoadGraphics(){
_LoadImage("highlight_trackselectbutton.png");
_LoadImage("smallbutton3.png");
_LoadImage("highlight_smallbutton3.png");
_LoadImage("directionalmovement.png");
_LoadImage("directionalmovement_selected.png");
_LoadImage("rotationalmovement.png");
_LoadImage("rotationalmovement_selected.png");
}
void HamsterGame::LoadAnimations(){
@ -432,8 +397,58 @@ bool HamsterGame::OnUserUpdate(float fElapsedTime){
if(!netInitialized){
net.InitSession();
netInitialized=true;
net.SetName(playerName);
net.SetColor(hamsterColor);
#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.substr(0,length));
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.substr(0,length));
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.substr(0,length);
std::cout<<std::format("Success! Loaded Player Name {}",*((std::string*)(arg)))<<std::endl;
HamsterGame::Game().net.SetName(*((std::string*)(arg)));
},
[](void*arg){
std::cout<<std::format("Failed to load Player Name")<<std::endl;
HamsterGame::Game().net.SetName(*((std::string*)arg));
});
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))=rawMetadata.substr(0,length);
std::cout<<std::format("Success! Loaded Hamster Color {}",*((std::string*)(arg)))<<std::endl;
HamsterGame::Game().net.SetColor(*((std::string*)(arg)));
},
[](void*arg){
std::cout<<std::format("Failed to load Hamster Color")<<std::endl;
HamsterGame::Game().net.SetColor(*((std::string*)arg));
});
emscripten_idb_async_load("hamster",Game().steeringModeLabel.c_str(),&Game().steeringMode,[](void*arg,void*data,int length){
std::string rawMetadata=(char*)data;
std::cout<<rawMetadata<<std::endl;
*((HamsterGame::SteeringMode*)(arg))=HamsterGame::SteeringMode(stoi(rawMetadata.substr(0,length)));
std::cout<<std::format("Success! Loaded Steering Mode {}",int(*((HamsterGame::SteeringMode*)(arg))))<<std::endl;
},
[](void*arg){
std::cout<<std::format("Failed to load Steering Mode")<<std::endl;
});
#endif
}
runTime+=fElapsedTime;
@ -626,6 +641,9 @@ void HamsterGame::OnTextEntryComplete(const std::string& sText){
if(HamsterAI::IsRecording())HamsterAI::OnTextEntryComplete(sText);
else menu.OnTextEntryComplete(sText);
}
void HamsterGame::OnTextEntryCancelled(const std::string& sText){
menu.OnTextEntryCancelled(sText);
}
const Difficulty&HamsterGame::GetMapDifficulty()const{
return currentMap.value().GetData().GetMapDifficulty();
@ -645,7 +663,7 @@ void HamsterGame::SaveOptions(){
for(const std::string&mapName:Game().mapNameList){
file<<mapPBs[mapName]<<" ";
}
file<<Game().bgmVol<<" "<<Game().sfxVol<<" "<<Game().playerName<<" "<<Game().hamsterColor;
file<<Game().bgmVol<<" "<<Game().sfxVol<<" "<<Game().playerName<<" "<<Game().hamsterColor<<" "<<int(GetSteeringMode());
file.close();
#endif
}
@ -708,12 +726,20 @@ void HamsterGame::LoadPBs(){
}break;
case 18:{
file>>Game().playerName;
Game().net.SetName(Game().playerName);
std::cout<<Game().playerName<<std::endl;
}break;
case 19:{
file>>Game().hamsterColor;
Game().net.SetColor(Game().hamsterColor);
std::cout<<Game().hamsterColor<<std::endl;
}break;
case 20:{
int steeringModeNumb{};
file>>steeringModeNumb;
Game().steeringMode=HamsterGame::SteeringMode(steeringModeNumb);
std::cout<<int(Game().steeringMode)<<std::endl;
}break;
default:{
int readVal;
file>>readVal;
@ -825,6 +851,13 @@ const float HamsterGame::GetPlayerDifferentialTime()const{
return playerDifferentialTime;
}
const HamsterGame::SteeringMode HamsterGame::GetSteeringMode()const{
return steeringMode;
}
void HamsterGame::SetSteeringMode(const SteeringMode steeringMode){
this->steeringMode=steeringMode;
}
int main()
{
HamsterGame game("Project Hamster");

@ -65,6 +65,10 @@ class HamsterGame : public olc::PixelGameEngine
{
friend class Menu;
public:
enum class SteeringMode{
DIRECTIONAL,
ROTATIONAL,
};
enum class GameMode{
GRAND_PRIX_1,
GRAND_PRIX_2,
@ -132,6 +136,8 @@ public:
const float GetPlayerDifferentialTime()const;
float collectedPowerupTimer{};
std::string powerupHelpDisplay;
const SteeringMode GetSteeringMode()const;
void SetSteeringMode(const SteeringMode steeringMode);
private:
void UpdateGame(const float fElapsedTime);
void DrawGame();
@ -145,6 +151,7 @@ private:
Border border;
void DrawLevelTiles();
void LoadRace(const std::string&mapName);
virtual void OnTextEntryCancelled(const std::string&sText)override final;
std::optional<TMXParser>currentMap;
std::optional<TSXParser>currentTileset;
double runTime{};
@ -217,6 +224,7 @@ private:
std::string sfxVolLabel{"sfxVol"};
std::string playerNameLabel{"playerName"};
std::string hamsterColorLabel{"hamsterColor"};
std::string steeringModeLabel{"steeringMode"};
float bgmVol{0.7f};
float sfxVol{0.7f};
int lastDigitPlayedSound{};
@ -224,4 +232,5 @@ private:
float holdEscTimer{0.f};
std::optional<int>racePauseTime{};
bool raceStarted{false};
SteeringMode steeringMode{SteeringMode::DIRECTIONAL};
};

@ -297,7 +297,7 @@ 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){
buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{-16.f,64.f},"","smallbutton.png","smallhighlight_button.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
HamsterGame::PlaySFX("select_track_confirm_name_menu.wav");
int colorInd{0};
for(int ind{0};const std::string&color:HamsterGame::Game().hamsterColorNames){
@ -320,6 +320,33 @@ std::vector<Menu::Button>Menu::GetMenuButtons(const MenuType type){
HamsterGame::Game().SaveOptions();
#endif
});
Button&newButton{buttons.emplace_back(HamsterGame::SCREEN_FRAME.size/2+vf2d{16.f,64.f},"","directionalmovement.png","directionalmovement_selected.png",Pixel{114,109,163},Pixel{79,81,128},[this](Button&self){
HamsterGame::PlaySFX("select_track_confirm_name_menu.wav");
if(HamsterGame::Game().GetSteeringMode()==HamsterGame::SteeringMode::DIRECTIONAL){
HamsterGame::Game().SetSteeringMode(HamsterGame::SteeringMode::ROTATIONAL);
self.buttonImg="rotationalmovement.png";
self.highlightButtonImg="rotationalmovement_selected.png";
}else{
HamsterGame::Game().SetSteeringMode(HamsterGame::SteeringMode::DIRECTIONAL);
self.buttonImg="directionalmovement.png";
self.highlightButtonImg="directionalmovement_selected.png";
}
#ifdef __EMSCRIPTEN__
HamsterGame::Game().emscripten_temp_val=std::to_string(int(HamsterGame::Game().GetSteeringMode()));
emscripten_idb_async_store("hamster",HamsterGame::Game().steeringModeLabel.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
})};
if(HamsterGame::Game().GetSteeringMode()==HamsterGame::SteeringMode::ROTATIONAL){
newButton.buttonImg="rotationalmovement.png";
newButton.highlightButtonImg="rotationalmovement_selected.png";
}
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;
case GAMEPLAY_RESULTS:{
@ -597,8 +624,6 @@ void Menu::OnLevelLoaded(){
HamsterGame::Game().audio.SetVolume(HamsterGame::Game().bgm.at(HamsterGame::Game().currentMap.value().GetData().GetBGM()),HamsterGame::Game().bgmVol);
HamsterGame::Game().audio.Stop(HamsterGame::Game().bgm["Trevor Lentz - Guinea Pig Hero.ogg"]);
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());
HamsterGame::Game().countdownTimer=3.f;
@ -626,9 +651,25 @@ void Menu::Button::Draw(HamsterGame&game,const vf2d&offset,std::optional<std::re
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."};
Pixel helpTextCol{WHITE};
if(game.menu.badNameEntered){
helpText=" Invalid name entered! \nPlease try again, then press <ENTER> or <ESC>.";
helpTextCol=RED;
}
const vf2d helpTextSize{game.GetTextSizeProp(helpText)};
game.DrawShadowRotatedStringPropDecal(pos+offset+vf2d{0,12.f},helpText,0.f,helpTextSize/2);
}else if(buttonImg=="smallbutton.png"){
game.DrawShadowRotatedStringPropDecal(pos+offset+vf2d{0,12.f},helpText,0.f,helpTextSize/2,helpTextCol);
}else if(buttonImg.starts_with("directionalmovement")){
std::string helpText{"Hamster moves in direction of pressed key."};
Pixel helpTextCol{WHITE};
const vf2d helpTextSize{game.GetTextSizeProp(helpText)};
game.DrawShadowRotatedStringPropDecal(pos+offset+vf2d{0,12.f},helpText,0.f,helpTextSize/2,helpTextCol);
}else if(buttonImg.starts_with("rotationalmovement")){
std::string helpText{"Hamster rotates with LEFT/RIGHT.\nForwards/backwards with UP/DOWN."};
Pixel helpTextCol{WHITE};
const vf2d helpTextSize{game.GetTextSizeProp(helpText)};
game.DrawShadowRotatedStringPropDecal(pos+offset+vf2d{0,12.f},helpText,0.f,helpTextSize/2,helpTextCol);
}else
if(buttonImg=="smallbutton.png"){
int colorInd{0};
for(int ind{0};const std::string&color:game.hamsterColorNames){
if(color==game.hamsterColor){
@ -660,7 +701,19 @@ void Menu::Button::OnClick(){
onClick(*this);
}
void Menu::OnTextEntryCancelled(const std::string&text){
for(Button&b:menuButtons){
if(b.buttonText.starts_with("Player Name")){
b.buttonText=std::format("Player Name: {}",HamsterGame::Game().playerName);
}
}
ignoreInputs=true;
badNameEntered=false;
}
void Menu::OnTextEntryComplete(const std::string&text){
const bool netResponse{HamsterGame::Game().net.SetName(text.substr(0,30))};
if(netResponse){
HamsterGame::Game().playerName=text.substr(0,30);
HamsterGame::Game().emscripten_temp_val=HamsterGame::Game().playerName;
#ifdef __EMSCRIPTEN__
@ -680,5 +733,10 @@ void Menu::OnTextEntryComplete(const std::string&text){
}
ignoreInputs=true;
HamsterGame::PlaySFX("menu_set_name.wav");
badNameEntered=false;
}else{
badNameEntered=true;
HamsterGame::Game().TextEntryEnable(true,text.substr(0,30));
}
}

@ -45,13 +45,13 @@ class HamsterGame;
class Menu{
class Button{
vf2d pos;
std::string buttonImg;
std::string highlightButtonImg;
Pixel textCol;
Pixel highlightTextCol;
std::function<void(Button&self)>onClick;
public:
std::string buttonText;
std::string buttonImg;
std::string highlightButtonImg;
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;
void OnClick();
@ -93,7 +93,7 @@ private:
MenuType currentMenu{INITIALIZE};
MenuType nextMenu{TITLE_SCREEN};
float menuTimer{};
const float MENU_TRANSITION_REFRESH_RATE{0.1f};
const float MENU_TRANSITION_REFRESH_RATE{0.06f};
float menuTransitionRefreshTimer{MENU_TRANSITION_REFRESH_RATE};
float originalMenuTimer{};
vi2d oldLayerPos{};
@ -112,10 +112,12 @@ private:
std::vector<Button>GetMenuButtons(const MenuType type);
bool ignoreInputs{false};
std::vector<LeaderboardEntry>loadedLeaderboard;
bool badNameEntered{false};
public:
void UpdateAndDraw(HamsterGame&game,const float fElapsedTime);
void OnLevelLoaded();
void UpdateLoadingProgress(const float pctLoaded);
void OnTextEntryComplete(const std::string&text);
void OnTextEntryCancelled(const std::string&text);
void Transition(const TransitionType type,const MenuType gotoMenu,const float transitionTime);
};

@ -993,6 +993,7 @@ namespace olc
// Called when a text entry is confirmed with "enter" key
virtual void OnTextEntryComplete(const std::string& sText);
virtual void OnTextEntryCancelled(const std::string& sText);
// Called when a console command is executed
virtual bool OnConsoleCommand(const std::string& sCommand);
@ -3891,8 +3892,10 @@ namespace olc
sTextEntryString.erase(nTextEntryCursor, 1);
if (GetKey(olc::Key::ENTER).bPressed||GetKey(olc::Key::ESCAPE).bPressed)
if(GetKey(olc::Key::ESCAPE).bPressed){
OnTextEntryCancelled(sTextEntryString);
TextEntryEnable(false);
}else if (GetKey(olc::Key::ENTER).bPressed)
{
if (bConsoleShow)
{
@ -3907,8 +3910,8 @@ namespace olc
}
else
{
OnTextEntryComplete(sTextEntryString);
TextEntryEnable(false);
OnTextEntryComplete(sTextEntryString);
}
}
}
@ -3926,6 +3929,7 @@ namespace olc
bool PixelGameEngine::OnUserDestroy()
{ return true; }
void PixelGameEngine::OnTextEntryCancelled(const std::string& sText) { UNUSED(sText); }
void PixelGameEngine::OnTextEntryComplete(const std::string& sText) { UNUSED(sText); }
bool PixelGameEngine::OnConsoleCommand(const std::string& sCommand) { UNUSED(sCommand); return false; }

Loading…
Cancel
Save