Peoplemon  0.1.0
Peoplemon 3 game source documentation
FlyMap.cpp
Go to the documentation of this file.
1 #include <Game/States/FlyMap.hpp>
2 
3 #include <Core/Maps/Map.hpp>
4 #include <Core/Properties.hpp>
5 #include <Core/Resources.hpp>
6 
7 namespace game
8 {
9 namespace state
10 {
11 namespace
12 {
13 constexpr float FlashOn = 0.3f;
14 constexpr float FlashOff = 0.4f;
15 } // namespace
16 
17 bl::engine::State::Ptr FlyMap::create(core::system::Systems& s, bool& up) {
18  return Ptr{new FlyMap(s, up)};
19 }
20 
21 FlyMap::FlyMap(core::system::Systems& s, bool& up)
22 : State(s, bl::engine::StateMask::Menu)
23 , unpause(up)
24 , selected(std::numeric_limits<unsigned int>::max()) {
25  const auto& joinPath = bl::util::FileUtil::joinPath;
26  const auto& ImgPath = core::Properties::MenuImagePath();
27  auto& tp = s.engine().renderer().texturePool();
28 
29  townMenu.create(s.engine(),
30  s.engine().renderer().getObserver(),
31  bl::menu::ArrowSelector::create(8.f, sf::Color::Black));
32 
33  mapTxtr = tp.getOrLoadTexture(joinPath(ImgPath, "FlyMap/background.png"));
34  map.create(s.engine(), mapTxtr);
35 
36  townTxtr = tp.getOrLoadTexture(joinPath(ImgPath, "FlyMap/town.png"));
37  towns.create(s.engine(), townTxtr);
38  towns.setParent(map);
39 
40  cursorTxtr = tp.getOrLoadTexture(joinPath(ImgPath, "FlyMap/cursor.png"));
41  cursor.create(s.engine(), cursorTxtr);
42  cursor.getTransform().setOrigin(cursorTxtr->size() * 0.5f);
43  cursor.setParent(map);
44 
46  const sf::Vector2f ws = systems.world().activeMap().sizePixels();
47  const glm::vec2 pp =
49  const glm::vec2 ms(mapTxtr->size());
50  const std::string path =
51  std::string("FlyMap/") + (systems.player().state().sex == core::player::Gender::Boy ?
52  "boyHead.png" :
53  "girlHead.png");
54  playerTxtr = tp.getOrLoadTexture(joinPath(ImgPath, path));
55  player.create(s.engine(), playerTxtr);
56  player.getTransform().setOrigin(playerTxtr->size() * 0.5f);
57  player.getTransform().setPosition(pp.x / ws.x * ms.x, pp.y / ws.y * ms.y);
58  player.setParent(map);
59  }
60 
61  panelTxtr = tp.getOrLoadTexture(joinPath(ImgPath, "FlyMap/sidePanel.png"));
62  panel.create(s.engine(), panelTxtr);
63  panel.getTransform().setPosition(mapTxtr->size().x, 0.f);
64 
65  townName.create(s.engine(), core::Properties::MenuFont(), "", 20, sf::Color(20, 40, 65));
66  townName.getTransform().setPosition(100.f, 375.f);
67  townName.wordWrap(180.f);
68  townName.setParent(panel);
69 
70  townDesc.create(s.engine(), core::Properties::MenuFont(), "", 16, sf::Color::Black);
71  townDesc.wordWrap(180.f);
72  townDesc.getTransform().setPosition(10.f, 415.f);
73  townDesc.setParent(panel);
74 
75  const auto& visited = systems.player().state().visitedTowns;
76  bl::menu::Item::Ptr back =
77  bl::menu::TextItem::create("Back", core::Properties::MenuFont(), sf::Color::Black, 18);
78  back->getSignal(bl::menu::Item::Selected).willAlwaysCall(std::bind(&FlyMap::clearHover, this));
79  back->getSignal(bl::menu::Item::Activated).willAlwaysCall(std::bind(&FlyMap::close, this));
80  townMenu.setRootItem(back);
81 
82  bl::menu::Item* prev = back.get();
83  bl::gfx::BatchSprite townSprite;
84  townSprite.disableAutoCommit(true);
85  townSprite.getLocalTransform().setOrigin(townTxtr->size() * 0.5f);
86  const sf::FloatRect src(0.f, 0.f, townTxtr->size().x, townTxtr->size().y);
87  for (unsigned int i = 0; i < core::map::Map::FlyMapTowns().size(); ++i) {
88  const auto& t = core::map::Map::FlyMapTowns()[i];
89  if (visited.find(t.name) != visited.end() && t.mapPos != sf::Vector2i{} && t.pcSpawn > 0) {
90  bl::menu::Item::Ptr it = bl::menu::TextItem::create(
91  t.name, core::Properties::MenuFont(), sf::Color::Black, 18);
92  it->getSignal(bl::menu::Item::Selected)
93  .willAlwaysCall(std::bind(&FlyMap::hoverTown, this, i));
94  it->getSignal(bl::menu::Item::Activated)
95  .willAlwaysCall(std::bind(&FlyMap::selectTown, this, i));
96  townMenu.addItem(it, prev, bl::menu::Item::Top);
97  prev = it.get();
98  selected = i;
99  }
100 
101  townSprite.create(s.engine(), towns, src);
102  townSprite.getLocalTransform().setPosition(t.mapPos.x, t.mapPos.y);
103  townSprite.orphan();
104  }
105  if (prev != back.get()) { townMenu.attachExisting(prev, back.get(), bl::menu::Item::Bottom); }
106  townMenu.setSelectedItem(prev);
107  townMenu.setPosition({8.f, 4.f});
108  townMenu.setMaximumSize({200.f, 316.f});
109  inputDriver.drive(&townMenu);
110 }
111 
112 const char* FlyMap::name() const { return "FlyMap"; }
113 
114 void FlyMap::activate(bl::engine::Engine& engine) {
115  systems.engine().inputSystem().getActor().addListener(*this);
116 
117  auto overlay = engine.renderer().getObserver().pushScene<bl::rc::Overlay>();
118  map.addToScene(overlay, bl::rc::UpdateSpeed::Static);
119  towns.addToScene(overlay, bl::rc::UpdateSpeed::Static);
120  if (player.entity() != bl::ecs::InvalidEntity) {
121  player.addToScene(overlay, bl::rc::UpdateSpeed::Static);
122  }
123  cursor.addToScene(overlay, bl::rc::UpdateSpeed::Static);
124  panel.addToScene(overlay, bl::rc::UpdateSpeed::Static);
125  townName.addToScene(overlay, bl::rc::UpdateSpeed::Static);
126  townDesc.addToScene(overlay, bl::rc::UpdateSpeed::Static);
127  townMenu.addToOverlay(panel.entity());
128  if (selected < core::map::Map::FlyMapTowns().size()) { hoverTown(selected); }
129 }
130 
131 void FlyMap::deactivate(bl::engine::Engine& engine) {
132  systems.engine().inputSystem().getActor().removeListener(*this);
133  engine.renderer().getObserver().popScene();
134 }
135 
136 void FlyMap::update(bl::engine::Engine&, float, float) {}
137 
138 bool FlyMap::observe(const bl::input::Actor&, unsigned int activatedControl,
139  bl::input::DispatchType, bool fromEvent) {
140  if (activatedControl == core::input::Control::Back) { systems.engine().popState(); }
141  else { inputDriver.sendControl(activatedControl, fromEvent); }
142  return true;
143 }
144 
145 void FlyMap::clearHover() {
146  townName.getSection().setString("");
147  townDesc.getSection().setString("Select a location to fly to.");
148  cursor.stopFlashing();
149  cursor.setHidden(true);
150 }
151 
152 void FlyMap::hoverTown(unsigned int i) {
153  const auto& t = core::map::Map::FlyMapTowns()[i];
154  townName.getSection().setString(t.name);
155  townDesc.getSection().setString(t.description);
156  cursor.getTransform().setPosition(t.mapPos.x, t.mapPos.y);
157  cursor.setHidden(false);
158  cursor.flash(FlashOn, FlashOff);
159  townName.getTransform().setOrigin(townName.getLocalBounds().width * 0.5f,
160  townName.getLocalBounds().height * 0.5f);
161 }
162 
163 void FlyMap::selectTown(unsigned int i) {
164  const auto t = core::map::Map::FlyMapTowns()[i];
168  "Fly to " + t.name + "?",
169  {"Yes", "No"},
170  std::bind(&FlyMap::onFlyChoice, this, std::placeholders::_1, t));
171  }
172  else {
173  systems.hud().displayMessage("One day you may be able to quickly travel if you acquire "
174  "the right item... one day.",
175  std::bind(&FlyMap::messageDone, this));
176  }
177  }
178  else {
179  systems.hud().displayMessage("You cannot fly from here!",
180  std::bind(&FlyMap::messageDone, this));
181  }
182 }
183 
184 void FlyMap::onFlyChoice(const std::string& c, const core::map::Town& town) {
185  if (c == "Yes") {
186  if (systems.flight().startFlight(town.pcSpawn)) {
187  unpause = true;
188  close();
189  }
190  else {
191  systems.hud().displayMessage("You cannot fly from here!",
192  std::bind(&FlyMap::messageDone, this));
193  }
194  }
195 }
196 
197 void FlyMap::messageDone() {}
198 
199 void FlyMap::close() { systems.engine().popState(); }
200 
201 } // namespace state
202 } // namespace game
Parent namespace for all functionality unique to the game.
bool canFlyFromHere() const
Returns whether or not the player can fly from this map.
Definition: Map.cpp:603
static const std::vector< Town > & FlyMapTowns()
Returns the set of towns that can be flown to.
Definition: Map.cpp:587
sf::Vector2f sizePixels() const
Returns the size of the map in pixels.
Definition: Map.cpp:175
Represents a town, route, or region within a map. Maps may have many towns. Individual tiles are asso...
Definition: Town.hpp:22
std::uint16_t pcSpawn
Definition: Town.hpp:26
bool hasItem(item::Id item) const
Returns true if at least one of the given items is owned.
Definition: Bag.cpp:42
player::Gender sex
Definition: State.hpp:43
player::Bag bag
Definition: State.hpp:44
std::unordered_set< std::string > visitedTowns
Definition: State.hpp:48
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static int PixelsPerTile()
Definition: Properties.cpp:279
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
bool startFlight(unsigned int destSpawn)
Starts flight to the given spawn.
Definition: Flight.cpp:50
void displayMessage(const std::string &message, const Callback &cb=[](const std::string &) {})
Displays a message in the HUD textbox. Messages are queued in order that they arrive.
Definition: HUD.cpp:92
void promptUser(const std::string &prompt, const std::vector< std::string > &choices, const Callback &cb)
Asks the player a question through the HUD.
Definition: HUD.cpp:112
player::State & state()
Returns the state of the player.
Definition: Player.cpp:154
const bl::tmap::Position & position() const
Returns the current position of the player.
Definition: Player.cpp:57
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
Player & player()
Returns the player system.
Definition: Systems.cpp:59
Flight & flight()
Returns the flight system.
Definition: Systems.cpp:83
HUD & hud()
Returns the HUD.
Definition: Systems.cpp:69
World & world()
Modifiable accessor for the world system.
Definition: Systems.cpp:43
map::Map & activeMap()
Returns a reference to the active map.
Definition: World.cpp:84
Engine state for the fly map.
Definition: FlyMap.hpp:22
virtual void update(bl::engine::Engine &engine, float dt, float) override
Updates the state and menus and whatnot.
Definition: FlyMap.cpp:136
virtual void deactivate(bl::engine::Engine &engine) override
Deactivates the state.
Definition: FlyMap.cpp:131
virtual const char * name() const override
Returns "FlyMap".
Definition: FlyMap.cpp:112
virtual void activate(bl::engine::Engine &engine) override
Activates the state.
Definition: FlyMap.cpp:114
static bl::engine::State::Ptr create(core::system::Systems &systems, bool &unpause)
Creates the fly map state.
Definition: FlyMap.cpp:17
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