Skip to content

Commit

Permalink
add state flow for Scrub
Browse files Browse the repository at this point in the history
add overlay messages
  • Loading branch information
fallahn committed Oct 14, 2024
1 parent b3b4ad6 commit ceb1ddb
Show file tree
Hide file tree
Showing 12 changed files with 493 additions and 10 deletions.
6 changes: 6 additions & 0 deletions samples/scratchpad/scratchpad.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,10 @@
<ClCompile Include="src\retro\RetroState.cpp" />
<ClCompile Include="src\rolling\RollingState.cpp" />
<ClCompile Include="src\rolling\RollingSystem.cpp" />
<ClCompile Include="src\scrub\ScrubAttractState.cpp" />
<ClCompile Include="src\scrub\ScrubBackgroundState.cpp" />
<ClCompile Include="src\scrub\ScrubGameState.cpp" />
<ClCompile Include="src\scrub\ScrubPauseState.cpp" />
<ClCompile Include="src\ssao\LightVolumeSystem.cpp" />
<ClCompile Include="src\ssao\SSAOState.cpp" />
<ClCompile Include="src\swingput\Swingput.cpp" />
Expand Down Expand Up @@ -406,7 +409,10 @@
<ClInclude Include="src\rolling\RollingState.hpp" />
<ClInclude Include="src\rolling\RollingSystem.hpp" />
<ClInclude Include="src\rolling\Utility.hpp" />
<ClInclude Include="src\scrub\ScrubAttractState.hpp" />
<ClInclude Include="src\scrub\ScrubBackgroundState.hpp" />
<ClInclude Include="src\scrub\ScrubGameState.hpp" />
<ClInclude Include="src\scrub\ScrubPauseState.hpp" />
<ClInclude Include="src\ssao\LightVolumeSystem.hpp" />
<ClInclude Include="src\ssao\SSAOState.hpp" />
<ClInclude Include="src\StateIDs.hpp" />
Expand Down
18 changes: 18 additions & 0 deletions samples/scratchpad/scratchpad.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@
<ClCompile Include="src\scrub\ScrubGameState.cpp">
<Filter>Source Files\scrub</Filter>
</ClCompile>
<ClCompile Include="src\scrub\ScrubBackgroundState.cpp">
<Filter>Source Files\scrub</Filter>
</ClCompile>
<ClCompile Include="src\scrub\ScrubAttractState.cpp">
<Filter>Source Files\scrub</Filter>
</ClCompile>
<ClCompile Include="src\scrub\ScrubPauseState.cpp">
<Filter>Source Files\scrub</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ErrorCheck.hpp">
Expand Down Expand Up @@ -524,6 +533,15 @@
<ClInclude Include="src\scrub\ScrubGameState.hpp">
<Filter>Header Files\scrub</Filter>
</ClInclude>
<ClInclude Include="src\scrub\ScrubBackgroundState.hpp">
<Filter>Header Files\scrub</Filter>
</ClInclude>
<ClInclude Include="src\scrub\ScrubAttractState.hpp">
<Filter>Header Files\scrub</Filter>
</ClInclude>
<ClInclude Include="src\scrub\ScrubPauseState.hpp">
<Filter>Header Files\scrub</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\voxels\Tables.inl">
Expand Down
10 changes: 9 additions & 1 deletion samples/scratchpad/src/MyApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ source distribution.
#include "pseuthe/PseutheBackgroundState.hpp"
#include "pseuthe/PseutheGameState.hpp"
#include "pseuthe/PseutheMenuState.hpp"

#include "scrub/ScrubBackgroundState.hpp"
#include "scrub/ScrubAttractState.hpp"
#include "scrub/ScrubGameState.hpp"
#include "scrub/ScrubPauseState.hpp"

#include <crogine/core/Clock.hpp>

Expand Down Expand Up @@ -177,15 +181,19 @@ bool MyApp::initialise()
m_stateStack.registerState<InteriorMappingState>(States::ScratchPad::InteriorMapping);
m_stateStack.registerState<EndlessDrivingState>(States::ScratchPad::EndlessDriving);
m_stateStack.registerState<TrackOverlayState>(States::ScratchPad::TrackOverlay);

m_stateStack.registerState<ScrubGameState>(States::ScratchPad::Scrub);
m_stateStack.registerState<ScrubAttractState>(States::ScratchPad::ScrubAttract);
m_stateStack.registerState<ScrubBackgroundState>(States::ScratchPad::ScrubBackground);
m_stateStack.registerState<ScrubPauseState>(States::ScratchPad::ScrubPause);

m_stateStack.registerState<PseutheBackgroundState>(States::ScratchPad::PseutheBackground);
m_stateStack.registerState<PseutheGameState>(States::ScratchPad::PseutheGame);
m_stateStack.registerState<PseutheMenuState>(States::ScratchPad::PseutheMenu);

#ifdef CRO_DEBUG_
//m_stateStack.pushState(States::ScratchPad::TrackOverlay);
m_stateStack.pushState(States::ScratchPad::Scrub);
m_stateStack.pushState(States::ScratchPad::ScrubBackground);
//m_stateStack.pushState(States::ScratchPad::MainMenu);
#else
//m_stateStack.pushState(States::ScratchPad::MainMenu);
Expand Down
3 changes: 3 additions & 0 deletions samples/scratchpad/src/StateIDs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ namespace States
PseutheGame,
PseutheMenu,
Scrub,
ScrubBackground,
ScrubAttract,
ScrubPause,

Count
};
Expand Down
134 changes: 134 additions & 0 deletions samples/scratchpad/src/scrub/ScrubAttractState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "ScrubAttractState.hpp"

#include <crogine/core/App.hpp>

#include <crogine/ecs/components/Transform.hpp>
#include <crogine/ecs/components/Drawable2D.hpp>
#include <crogine/ecs/components/Text.hpp>
#include <crogine/ecs/components/Camera.hpp>

#include <crogine/ecs/systems/TextSystem.hpp>
#include <crogine/ecs/systems/CameraSystem.hpp>
#include <crogine/ecs/systems/RenderSystem2D.hpp>

#include <crogine/graphics/Font.hpp>

ScrubAttractState::ScrubAttractState(cro::StateStack& ss, cro::State::Context ctx)
: cro::State(ss, ctx),
m_uiScene (ctx.appInstance.getMessageBus())
{
addSystems();
buildScene();
}

//public
bool ScrubAttractState::handleEvent(const cro::Event& evt)
{
if (evt.type == SDL_KEYDOWN)
{
switch (evt.key.keysym.sym)
{
default: break;
case SDLK_SPACE:
requestStackPop();
requestStackPush(States::ScratchPad::Scrub);
break;
}
}
else if (evt.type == SDL_CONTROLLERBUTTONDOWN)
{
switch (evt.cbutton.button)
{
default: break;
case cro::GameController::ButtonA:
requestStackPop();
requestStackPush(States::ScratchPad::Scrub);
break;
}
}


m_uiScene.forwardEvent(evt);
return false;
}

void ScrubAttractState::handleMessage(const cro::Message& msg)
{
m_uiScene.forwardMessage(msg);
}

bool ScrubAttractState::simulate(float dt)
{
m_uiScene.simulate(dt);
return false;
}

void ScrubAttractState::render()
{
m_uiScene.render();
}

//private
void ScrubAttractState::addSystems()
{
auto& mb = cro::App::getInstance().getMessageBus();
m_uiScene.addSystem<cro::TextSystem>(mb);
m_uiScene.addSystem<cro::CameraSystem>(mb);
m_uiScene.addSystem<cro::RenderSystem2D>(mb);
}

void ScrubAttractState::buildScene()
{
const std::string str =
R"(
Use A/D to scrub or right thumb stick
Use Q to insert and E to remove a ball (or LB and RB on the controller)
Press SPACE or Controller A to add more soap
Press ESCAPE or Start to Pause the game.
Press SPACE or Controller A to begin.
)";

//TODO use shared resources
const std::int32_t fontID = 1;
m_resources.fonts.load(fontID, "assets/golf/fonts/IBM_CGA.ttf");

const auto& font = m_resources.fonts.get(fontID);

auto size = glm::vec2(cro::App::getWindow().getSize());
auto entity = m_uiScene.createEntity();
entity.addComponent<cro::Transform>().setPosition(size / 2.f);
entity.getComponent<cro::Transform>().move({ 0.f, 60.f });
entity.addComponent<cro::Drawable2D>();
entity.addComponent<cro::Text>(font).setString(str);
entity.getComponent<cro::Text>().setCharacterSize(16);
entity.getComponent<cro::Text>().setAlignment(cro::Text::Alignment::Centre);

auto resize = [&](cro::Camera& cam)
{
glm::vec2 size(cro::App::getWindow().getSize());
cam.viewport = { 0.f, 0.f, 1.f, 1.f };
cam.setOrthographic(0.f, size.x, 0.f, size.y, -0.1f, 10.f);

//send messge to UI elements to reposition them
//cro::Command cmd;
//cmd.targetFlags = CommandID::UI::UIElement;
//cmd.action =
// [size](cro::Entity e, float)
// {
// const auto& ui = e.getComponent<UIElement>();
// float x = std::floor(size.x * ui.relativePosition.x);
// float y = std::floor(size.y * ui.relativePosition.y);
// e.getComponent<cro::Transform>().setPosition(glm::vec3(glm::vec2(ui.absolutePosition + glm::vec2(x, y)), ui.depth));

// //TODO probably want to rescale downwards too?
// };
//m_uiScene.getSystem<cro::CommandSystem>()->sendCommand(cmd);
};

auto& cam = m_uiScene.getActiveCamera().getComponent<cro::Camera>();
cam.resizeCallback = resize;
resize(cam);
}
29 changes: 29 additions & 0 deletions samples/scratchpad/src/scrub/ScrubAttractState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "../StateIDs.hpp"

#include <crogine/core/State.hpp>

#include <crogine/ecs/Scene.hpp>
#include <crogine/graphics/ModelDefinition.hpp>

class ScrubAttractState final : public cro::State
{
public:
ScrubAttractState(cro::StateStack&, cro::State::Context);

cro::StateID getStateID() const override { return States::ScratchPad::ScrubAttract; }

bool handleEvent(const cro::Event&) override;
void handleMessage(const cro::Message&) override;
bool simulate(float) override;
void render() override;

private:

cro::Scene m_uiScene;
cro::ResourceCollection m_resources;

void addSystems();
void buildScene();
};
40 changes: 40 additions & 0 deletions samples/scratchpad/src/scrub/ScrubBackgroundState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "ScrubBackgroundState.hpp"

ScrubBackgroundState::ScrubBackgroundState(cro::StateStack& ss, cro::State::Context ctx)
:cro::State(ss, ctx)
{
ctx.mainWindow.loadResources([this]()
{
//addSystems();
//loadAssets();
//createScene();
//createUI();

cacheState(States::ScratchPad::Scrub);
cacheState(States::ScratchPad::ScrubAttract);
cacheState(States::ScratchPad::ScrubPause);
});

requestStackPush(States::ScratchPad::ScrubAttract);
}

//public
bool ScrubBackgroundState::handleEvent(const cro::Event&)
{
return false;
}

void ScrubBackgroundState::handleMessage(const cro::Message&)
{

}

bool ScrubBackgroundState::simulate(float)
{
return false;
}

void ScrubBackgroundState::render()
{

}
21 changes: 21 additions & 0 deletions samples/scratchpad/src/scrub/ScrubBackgroundState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "../StateIDs.hpp"

#include <crogine/core/State.hpp>

class ScrubBackgroundState final : public cro::State
{
public:
ScrubBackgroundState(cro::StateStack&, cro::State::Context);

cro::StateID getStateID() const override { return States::ScratchPad::ScrubBackground; }

bool handleEvent(const cro::Event&) override;
void handleMessage(const cro::Message&) override;
bool simulate(float) override;
void render() override;

private:

};
Loading

0 comments on commit ceb1ddb

Please sign in to comment.