Skip to content

Commit

Permalink
add ability to quit back to clubhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Oct 28, 2024
1 parent f644333 commit 435639d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
30 changes: 27 additions & 3 deletions samples/golf/src/scrub/ScrubAttractState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ ScrubAttractState::ScrubAttractState(cro::StateStack& ss, cro::State::Context ct
//public
bool ScrubAttractState::handleEvent(const cro::Event& evt)
{
const auto quit = [&]()
{
requestStackClear();
requestStackPush(StateID::Clubhouse);
};

if (evt.type == SDL_KEYDOWN)
{
switch (evt.key.keysym.sym)
Expand All @@ -189,13 +195,22 @@ bool ScrubAttractState::handleEvent(const cro::Event& evt)
case SDLK_RIGHT:
nextTab();
break;
case SDLK_BACKSPACE:
#ifndef CRO_DEBUG_
case SDLK_ESCAPE:
#endif
quit();
break;
}
}
else if (evt.type == SDL_CONTROLLERBUTTONDOWN)
{
switch (evt.cbutton.button)
{
default: break;
case cro::GameController::ButtonB:
quit();
break;
case cro::GameController::ButtonA:
requestStackPop();
requestStackPush(StateID::ScrubGame);
Expand Down Expand Up @@ -609,21 +624,30 @@ void ScrubAttractState::buildScene()
{
if (cro::GameController::hasPSLayout(m_controllerIndex))
{
e.getComponent<cro::Text>().setString("Press " + cro::String(ButtonCross) + " To Start");
e.getComponent<cro::Text>().setString("Press " + cro::String(ButtonCross) + " To Start, " + cro::String(ButtonCircle) + " To Quit");
}
else
{
e.getComponent<cro::Text>().setString("Press " + cro::String(ButtonA) + " To Start");
e.getComponent<cro::Text>().setString("Press " + cro::String(ButtonA) + " To Start, " + cro::String(ButtonB) + " To Quit");
}
}
else
{
e.getComponent<cro::Text>().setString("Press SPACE To Start");
e.getComponent<cro::Text>().setString("Press SPACE To Start, ESCAPE To Quit");
}
};




const auto& infoFont = m_sharedData.sharedResources->fonts.get(FontID::Info);
entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>().setPosition({ 6.f, 14.f });
entity.addComponent<cro::Drawable2D>();
entity.addComponent<cro::Text>(infoFont).setString("Music by David KBD");
entity.getComponent<cro::Text>().setCharacterSize(InfoTextSize);


auto resize = [&](cro::Camera& cam)
{
glm::vec2 size(cro::App::getWindow().getSize());
Expand Down
24 changes: 22 additions & 2 deletions samples/golf/src/scrub/ScrubGameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ ScrubGameState::ScrubGameState(cro::StateStack& stack, cro::State::Context conte
m_soundDirector (nullptr),
m_gameScene (context.appInstance.getMessageBus()),
m_uiScene (context.appInstance.getMessageBus(), 512),
m_axisPosition (0)
m_axisPosition (0),
m_controllerIndex (0)
{
//this is a pre-cached state
addSystems();
Expand Down Expand Up @@ -224,6 +225,12 @@ bool ScrubGameState::handleEvent(const cro::Event& evt)
case SDLK_ESCAPE:
pause();
break;
#ifdef CRO_DEBUG_
case SDLK_l:
m_score.remainingTime = 0.f;
break;
#endif

}


Expand Down Expand Up @@ -274,6 +281,7 @@ bool ScrubGameState::handleEvent(const cro::Event& evt)
pause();
break;
}
m_controllerIndex = cro::GameController::controllerID(evt.cbutton.which);
}
else if (evt.type == SDL_CONTROLLERAXISMOTION)
{
Expand All @@ -297,6 +305,12 @@ bool ScrubGameState::handleEvent(const cro::Event& evt)

m_axisPosition = v;
}

if (evt.caxis.value < -cro::GameController::LeftThumbDeadZone
|| evt.caxis.value > cro::GameController::LeftThumbDeadZone)
{
m_controllerIndex = cro::GameController::controllerID(evt.caxis.which);
}
}
else if (evt.type == SDL_CONTROLLERDEVICEREMOVED)
{
Expand Down Expand Up @@ -408,12 +422,18 @@ bool ScrubGameState::simulate(float dt)

attachText(entity);

auto mins = std::int32_t(m_score.totalRunTime) / 60;
auto secs = m_score.totalRunTime - (mins * 60);
std::stringstream ss;
ss.precision(2);
ss << "\nTotal Time: " << mins << "m" << secs << "s";

const auto& font2 = m_sharedScrubData.fonts->get(sc::FontID::Body);
entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>().setPosition({ size.x / 2.f, size.y / 2.f, sc::TextDepth });
entity.getComponent<cro::Transform>().move({ 0.f, -14.f * getViewScale() });
entity.addComponent<cro::Drawable2D>();
entity.addComponent<cro::Text>(font2).setString("Total Score: " + std::to_string(m_score.totalScore) + "\n\nPress Any Button");
entity.addComponent<cro::Text>(font2).setString("Total Score: " + std::to_string(m_score.totalScore) + ss.str() + "\n\nPress Any Button");
entity.getComponent<cro::Text>().setCharacterSize(sc::MediumTextSize);
entity.getComponent<cro::Text>().setFillColour(TextNormalColour);
entity.getComponent<cro::Text>().setShadowColour(LeaderboardTextDark);
Expand Down
2 changes: 2 additions & 0 deletions samples/golf/src/scrub/ScrubGameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class ScrubGameState final : public cro::State, public cro::GuiClient
static constexpr float TimeBonus = 2.5f;
}m_score;

std::int32_t m_controllerIndex;

void onCachedPush() override;
void onCachedPop() override;

Expand Down

0 comments on commit 435639d

Please sign in to comment.