Skip to content

Commit

Permalink
plan out attract state
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Oct 18, 2024
1 parent 489a664 commit 95182ed
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
2 changes: 1 addition & 1 deletion samples/golf/src/GolfGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ GolfGame::GolfGame()
m_stateStack.registerState<GCState>(StateID::GC);

m_stateStack.registerState<ScrubBackgroundState>(StateID::ScrubBackground, m_scrubData);
m_stateStack.registerState<ScrubAttractState>(StateID::ScrubAttract, m_sharedData);
m_stateStack.registerState<ScrubAttractState>(StateID::ScrubAttract, m_sharedData, m_scrubData);
m_stateStack.registerState<ScrubGameState>(StateID::ScrubGame, m_sharedData, m_scrubData);
m_stateStack.registerState<ScrubPauseState>(StateID::ScrubPause, m_sharedData, m_scrubData);

Expand Down
80 changes: 66 additions & 14 deletions samples/golf/src/scrub/ScrubAttractState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,34 @@ source distribution.
-----------------------------------------------------------------------*/

#include "ScrubAttractState.hpp"
#include "ScrubSharedData.hpp"
#include "ScrubConsts.hpp"
#include "../golf/SharedStateData.hpp"
#include "../golf/GameConsts.hpp"

#include <crogine/core/App.hpp>

#include <crogine/ecs/components/Callback.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/components/AudioEmitter.hpp>

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

#include <crogine/graphics/Font.hpp>

ScrubAttractState::ScrubAttractState(cro::StateStack& ss, cro::State::Context ctx, SharedStateData& sd)
: cro::State(ss, ctx),
m_sharedData(sd),
m_uiScene (ctx.appInstance.getMessageBus())
ScrubAttractState::ScrubAttractState(cro::StateStack& ss, cro::State::Context ctx, SharedStateData& sd, SharedScrubData& sc)
: cro::State (ss, ctx),
m_sharedData (sd),
m_sharedScrubData (sc),
m_uiScene (ctx.appInstance.getMessageBus())
{
addSystems();
buildScene();
Expand Down Expand Up @@ -105,16 +112,37 @@ void ScrubAttractState::render()
void ScrubAttractState::addSystems()
{
auto& mb = cro::App::getInstance().getMessageBus();
m_uiScene.addSystem<cro::CallbackSystem>(mb);
m_uiScene.addSystem<cro::CommandSystem>(mb);
m_uiScene.addSystem<cro::TextSystem>(mb);
m_uiScene.addSystem<cro::CameraSystem>(mb);
m_uiScene.addSystem<cro::RenderSystem2D>(mb);
m_uiScene.addSystem<cro::AudioSystem>(mb);
}

void ScrubAttractState::loadAssets()
{
//TODO load menu music
}

void ScrubAttractState::buildScene()
{
const std::string str =
R"(
//title tab - image of ball washer in centre,
//text SCRUB spins in the middle of it

//TODO these tabs could have a callback to restart
//any animation a tab might have when the tab is shown
//TODO the tabs need a child node for scaling the sprites
m_tabs[TabID::Title] = m_uiScene.createEntity();
m_tabs[TabID::Title].addComponent<cro::Transform>();


//how to play tab
m_tabs[TabID::HowTo] = m_uiScene.createEntity();
m_tabs[TabID::HowTo].addComponent<cro::Transform>();

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
Expand All @@ -125,25 +153,42 @@ Press ESCAPE or Start to Pause the game.
Press SPACE or Controller A to begin.
)";

const auto& font = m_sharedData.sharedResources->fonts.get(FontID::UI);
const auto& font = m_sharedScrubData.fonts->get(sc::FontID::Body);

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::Transform>().setPosition(glm::vec3(size / 2.f, sc::TextDepth));
entity.getComponent<cro::Transform>().move({ 0.f, 60.f * getViewScale() });
entity.addComponent<cro::Drawable2D>();
entity.addComponent<cro::Text>(font).setString(str);
entity.getComponent<cro::Text>().setCharacterSize(16);
entity.getComponent<cro::Text>().setCharacterSize(sc::MediumTextSize * getViewScale());
entity.getComponent<cro::Text>().setAlignment(cro::Text::Alignment::Centre);
entity.getComponent<cro::Text>().setFillColour(TextNormalColour);
entity.getComponent<cro::Text>().setShadowColour(LeaderboardTextDark);
entity.getComponent<cro::Text>().setShadowOffset({ 2.f, -2.f });
entity.addComponent<cro::CommandTarget>().ID = CommandID::UI::UIElement;
entity.addComponent<UIElement>().relativePosition = glm::vec2(0.5f);
entity.getComponent<UIElement>().absolutePosition = { 0.f, 60.f };
entity.getComponent<UIElement>().characterSize = sc::MediumTextSize;
entity.getComponent<UIElement>().depth = sc::TextDepth;

m_tabs[TabID::HowTo].getComponent<cro::Transform>().addChild(entity.getComponent<cro::Transform>());


//high scores tab (or personal best in non-steam)
m_tabs[TabID::Scores] = m_uiScene.createEntity();
m_tabs[TabID::Scores].addComponent<cro::Transform>();




//TODO we want to scale the sprites down a la game state
//tho this will be per-tab rather than globally
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);
cam.setOrthographic(0.f, size.x, 0.f, size.y, -10.f, 10.f);

//send messge to UI elements to reposition them
cro::Command cmd;
Expand All @@ -154,9 +199,16 @@ Press SPACE or Controller A to begin.
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?

if (ui.characterSize)
{
e.getComponent<cro::Text>().setCharacterSize(ui.characterSize * getViewScale());
e.getComponent<cro::Transform>().setPosition(glm::vec3(glm::vec2((ui.absolutePosition * getViewScale()) + glm::vec2(x, y)), ui.depth));
}
else
{
e.getComponent<cro::Transform>().setPosition(glm::vec3(glm::vec2((ui.absolutePosition) + glm::vec2(x, y)), ui.depth));
}
};
m_uiScene.getSystem<cro::CommandSystem>()->sendCommand(cmd);
};
Expand Down
17 changes: 16 additions & 1 deletion samples/golf/src/scrub/ScrubAttractState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ source distribution.
#include <crogine/ecs/Scene.hpp>
#include <crogine/graphics/ModelDefinition.hpp>

#include <array>

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

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

Expand All @@ -51,9 +54,21 @@ class ScrubAttractState final : public cro::State

private:
SharedStateData& m_sharedData;
SharedScrubData& m_sharedScrubData;
cro::Scene m_uiScene;
cro::ResourceCollection m_resources;

struct TabID final
{
enum
{
Title, HowTo, Scores,
Count
};
};
std::array<cro::Entity, TabID::Count> m_tabs = {};

void addSystems();
void loadAssets();
void buildScene();
};
3 changes: 2 additions & 1 deletion samples/golf/src/scrub/ScrubBackgroundState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ ScrubBackgroundState::ScrubBackgroundState(cro::StateStack& ss, cro::State::Cont
cacheState(StateID::ScrubPause);
});
#ifdef CRO_DEBUG_
requestStackPush(StateID::ScrubGame);
//requestStackPush(StateID::ScrubGame);
requestStackPush(StateID::ScrubAttract);
#else
requestStackPush(StateID::ScrubAttract);
#endif
Expand Down

0 comments on commit 95182ed

Please sign in to comment.