Peoplemon  0.1.0
Peoplemon 3 game source documentation
MainMenu.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Engine.hpp>
4 #include <BLIB/Logging.hpp>
5 #include <BLIB/Util/FileUtil.hpp>
6 #include <Core/Properties.hpp>
7 #include <Core/Resources.hpp>
11 
12 namespace game
13 {
14 namespace state
15 {
16 using namespace core::input;
17 
18 bl::engine::State::Ptr MainMenu::create(core::system::Systems& systems) {
19  return Ptr(new MainMenu(systems));
20 }
21 
22 MainMenu::MainMenu(core::system::Systems& systems)
23 : State(systems, bl::engine::StateMask::Menu)
24 , hintTimer(0.f) {}
25 
26 const char* MainMenu::name() const { return "MainMenu"; }
27 
28 void MainMenu::activate(bl::engine::Engine& engine) {
29  // TODO - music
30 
31  if (!backgroundTxtr) {
32  using bl::menu::Item;
33  using bl::menu::TextItem;
34 
35  menu.create(engine,
36  engine.renderer().getObserver(),
37  bl::menu::ArrowSelector::create(14.f, sf::Color::Black));
38  backgroundTxtr = engine.renderer().texturePool().getOrLoadTexture(
39  bl::util::FileUtil::joinPath(core::Properties::MenuImagePath(), "mainMenu.png"));
40  background.create(engine, backgroundTxtr);
41 
42  newGame = TextItem::create(
43  "New Game", core::Properties::MenuFont(), sf::Color::Black, 32, sf::Text::Bold);
44  newGame->getSignal(Item::Activated).willCall([this]() {
45  BL_LOG_INFO << "New Game selected";
46  systems.engine().replaceState(NewGame::create(systems));
47  });
48 
49  loadGame = TextItem::create(
50  "Load Game", core::Properties::MenuFont(), sf::Color::Black, 32, sf::Text::Bold);
51  loadGame->getSignal(Item::Activated).willCall([this]() {
52  BL_LOG_INFO << "Load Game selected";
53  systems.engine().pushState(LoadGame::create(systems));
54  });
55 
56  settings = TextItem::create(
57  "Settings", core::Properties::MenuFont(), sf::Color::Black, 32, sf::Text::Bold);
58  settings->getSignal(Item::Activated).willCall([this]() {
59  BL_LOG_INFO << "Settings selected";
61  });
62 
63  quit = TextItem::create(
64  "Quit", core::Properties::MenuFont(), sf::Color::Black, 32, sf::Text::Bold);
65  quit->getSignal(Item::Activated).willCall([this]() {
66  BL_LOG_INFO << "Quit selected";
67  this->systems.engine().flags().set(bl::engine::Flags::PopState);
68  });
69 
70  menu.setRootItem(newGame);
71  menu.addItem(loadGame, newGame.get(), Item::Bottom);
72  menu.addItem(settings, loadGame.get(), Item::Bottom);
73  menu.addItem(quit, settings.get(), Item::Bottom);
74  menu.setMinHeight(38.f);
75  menu.setSelectedItem(newGame.get());
76  menu.setPosition(
78 
79  const glm::vec2 HintPosition(core::Properties::WindowSize().x * 0.15f,
80  core::Properties::WindowSize().y * 0.63f);
81  const glm::vec2 HintPadding{4.f, 4.f};
82  const auto& a = systems.engine().inputSystem().getActor();
83  const sf::Color controlColor(3, 146, 168);
84  hint.create(engine,
86  "Controls:",
87  18,
88  sf::Color::Black,
89  sf::Text::Bold);
90  hint.addSection("\nMove up:", 16, sf::Color::Black);
91  hint.addSection(a.getKBMTriggerControl(Control::MoveUp).toString(), 16, controlColor);
92  hint.addSection("\nMove right:", 16, sf::Color::Black);
93  hint.addSection(a.getKBMTriggerControl(Control::MoveRight).toString(), 16, controlColor);
94  hint.addSection("\nMove down:", 16, sf::Color::Black);
95  hint.addSection(a.getKBMTriggerControl(Control::MoveDown).toString(), 16, controlColor);
96  hint.addSection("\nMove left:", 16, sf::Color::Black);
97  hint.addSection(a.getKBMTriggerControl(Control::MoveLeft).toString(), 16, controlColor);
98  hint.addSection("\nSprint:", 16, sf::Color::Black);
99  hint.addSection(a.getKBMTriggerControl(Control::Sprint).toString(), 16, controlColor);
100  hint.addSection("\nInteract:", 16, sf::Color::Black);
101  hint.addSection(a.getKBMTriggerControl(Control::Interact).toString(), 16, controlColor);
102  hint.addSection("\nBack:", 16, sf::Color::Black);
103  hint.addSection(a.getKBMTriggerControl(Control::Back).toString(), 16, controlColor);
104  hint.addSection("\nPause:", 16, sf::Color::Black);
105  hint.addSection(a.getKBMTriggerControl(Control::Pause).toString(), 16, controlColor);
106 
107  hint.getTransform().setPosition(HintPadding);
108  const sf::FloatRect hintBounds = hint.getLocalBounds();
109  const glm::vec2 hintSize(hintBounds.width + hintBounds.left,
110  hintBounds.height + hintBounds.top);
111 
112  hintBox.create(engine, hintSize + HintPadding * 2.f);
113  hintBox.getOverlayScaler().positionInParentSpace({0.15f, 0.63f});
114  hintBox.setFillColor(sf::Color(252, 248, 212));
115  hintBox.setOutlineColor(sf::Color::Black);
116  hintBox.setOutlineThickness(2.f);
117  hint.setParent(hintBox);
118  hintBox.setParent(background);
119  }
120 
121  inputDriver.drive(&menu);
122  systems.engine().inputSystem().getActor().addListener(*this);
123  hintTimer = 0.f;
124  hintBox.setHidden(true);
125 
126  auto overlay = engine.renderer().getObserver().pushScene<bl::rc::Overlay>();
127  background.addToScene(overlay, bl::rc::UpdateSpeed::Static);
128  hintBox.addToScene(overlay, bl::rc::UpdateSpeed::Static);
129  hint.addToScene(overlay, bl::rc::UpdateSpeed::Static);
130  menu.addToOverlay(background.entity());
131 }
132 
133 void MainMenu::deactivate(bl::engine::Engine& engine) {
134  engine.renderer().getObserver().popScene();
135  systems.engine().inputSystem().getActor().removeListener(*this);
136 }
137 
138 void MainMenu::update(bl::engine::Engine&, float dt, float) {
139  hintTimer += dt;
140  if (hintTimer >= 5.f) { hintBox.setHidden(false); }
141 }
142 
143 bool MainMenu::observe(const bl::input::Actor&, unsigned int activatedControl,
144  bl::input::DispatchType, bool eventTriggered) {
145  inputDriver.sendControl(activatedControl, eventTriggered);
146  hintTimer = 0.f;
147  hintBox.setHidden(true);
148  return true;
149 }
150 
151 } // namespace state
152 } // namespace game
Peoplemon specific input functionality around the BLIB input system.
Definition: Control.hpp:18
Parent namespace for all functionality unique to the game.
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
static sf::Vector2f WindowSize()
Definition: Properties.cpp:262
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Definition: Systems.cpp:35
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates the load game state.
Definition: LoadGame.cpp:18
Provides the main menu as an engine state.
Definition: MainMenu.hpp:22
virtual void deactivate(bl::engine::Engine &engine) override
Unsubscribes the menu to the engine event bus.
Definition: MainMenu.cpp:133
virtual void update(bl::engine::Engine &, float dt, float) override
Does nothing.
Definition: MainMenu.cpp:138
virtual const char * name() const override
Returns "MainMenu";.
Definition: MainMenu.cpp:26
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates a new MainMenu state.
Definition: MainMenu.cpp:18
virtual void activate(bl::engine::Engine &engine) override
Subscribes the menu to the engine event bus.
Definition: MainMenu.cpp:28
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates the new game state.
Definition: NewGame.cpp:20
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates the settings menu state.
Parent to all game states. Provides some commonly required data like core game systems.
Definition: State.hpp:29
core::system::Systems & systems
Definition: State.hpp:66