Peoplemon  0.1.0
Peoplemon 3 game source documentation
MapExplorer.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Cameras.hpp>
4 #include <BLIB/Logging.hpp>
5 #include <Core/Maps/Map.hpp>
6 #include <Core/Properties.hpp>
8 
9 namespace game
10 {
11 namespace state
12 {
13 namespace
14 {
15 constexpr float Padding = 12.f;
16 constexpr float FadeTime = 0.5f;
17 const sf::Color BoxColor(30, 30, 30, 180);
18 const sf::Color BoxOutlineColor(20, 200, 20, 180);
19 const sf::Color TextColor(220, 220, 220);
20 
21 class ExplorerCameraController : public bl::cam::CameraController2D {
22 public:
23  ExplorerCameraController(core::system::Systems& systems)
24  : position(systems.player().position().transform->getLocalPosition())
25  , zoom(1.5f) {}
26 
27 private:
28  glm::vec2 position;
29  float zoom;
30 
31  virtual void update(float dt) override {
32  const float PixelsPerSecond =
33  0.5f * zoom * static_cast<float>(core::Properties::WindowWidth());
34  const float ZoomPerSecond = std::max(0.5f, zoom * 0.5f);
35 
36  if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { position.y -= PixelsPerSecond * dt; }
37  if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { position.x += PixelsPerSecond * dt; }
38  if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { position.y += PixelsPerSecond * dt; }
39  if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { position.x -= PixelsPerSecond * dt; }
40  if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) {
41  zoom -= ZoomPerSecond * dt;
42  if (zoom < 0.1f) zoom = 0.1f;
43  }
44  if (sf::Keyboard::isKeyPressed(sf::Keyboard::V)) { zoom += ZoomPerSecond * dt; }
45 
46  const sf::Vector2f size = zoom * core::Properties::WindowSize();
47  camera().setCenter(position);
48  camera().setSize({size.x, size.y});
49  }
50 };
51 
52 } // namespace
53 
54 bl::engine::State::Ptr MapExplorer::create(core::system::Systems& systems) {
55  return bl::engine::State::Ptr(new MapExplorer(systems));
56 }
57 
58 MapExplorer::MapExplorer(core::system::Systems& systems)
59 : State(systems, bl::engine::StateMask::Menu) {
60  hintBox.create(systems.engine(), {100.f, 100.f});
61  hintBox.setFillColor(BoxColor);
62  hintBox.setOutlineColor(BoxOutlineColor);
63  hintBox.setOutlineThickness(4.f);
64  hintBox.getTransform().setPosition(5.f, 5.f);
65 
66  hintText.create(systems.engine(),
68  "Move around: WASD\nZoom out: V\nZoom in: C\nExit: Esc",
69  28,
70  TextColor);
71  hintText.getTransform().setPosition(Padding, Padding);
72  hintText.setParent(hintBox);
73  hintBox.setSize(hintText.getLocalSize() + glm::vec2(Padding * 2.f));
74 }
75 
76 const char* MapExplorer::name() const { return "MapExplorer"; }
77 
78 void MapExplorer::activate(bl::engine::Engine& engine) {
79  bl::event::Dispatcher::subscribe(this);
81  static_cast<bl::cam::Camera2D*>(engine.renderer().getObserver().getCurrentCamera())
82  ->setController<ExplorerCameraController>(systems);
83 
84  bl::rc::Overlay* overlay = engine.renderer().getObserver().getOrCreateSceneOverlay();
85  hintBox.addToScene(overlay, bl::rc::UpdateSpeed::Static);
86  hintText.addToScene(overlay, bl::rc::UpdateSpeed::Static);
87  hintState = Hidden;
88  hintBox.setHidden(true);
89  hintTime = 0.f;
90 }
91 
92 void MapExplorer::deactivate(bl::engine::Engine& engine) {
93  bl::event::Dispatcher::unsubscribe(this);
96  hintBox.removeFromScene();
97 }
98 
99 void MapExplorer::update(bl::engine::Engine&, float dt, float) {
100  switch (hintState) {
101  case Hidden:
102  hintTime += dt;
103  if (hintTime >= 4.f) {
104  hintState = Showing;
105  hintTime = 0.f;
106  hintBox.setHidden(false);
107  hintBox.setFillColor(BoxColor);
108  hintBox.setOutlineColor(BoxOutlineColor);
109  hintText.getSection().setFillColor(TextColor);
110  }
111  break;
112  case Fading:
113  hintTime += dt;
114  if (hintTime <= FadeTime) {
115  const float p = (1.f - hintTime / FadeTime);
116  const float ab = p * static_cast<float>(BoxColor.a);
117  const float a = p * 255.f;
118  hintBox.setFillColor(sf::Color(BoxColor.r, BoxColor.g, BoxColor.b, ab));
119  hintBox.setOutlineColor(
120  sf::Color(BoxOutlineColor.r, BoxOutlineColor.g, BoxOutlineColor.b, ab));
121  hintText.getSection().setFillColor(sf::Color(TextColor.r, TextColor.g, TextColor.b, a));
122  }
123  else {
124  hintBox.setHidden(true);
125  hintState = Hidden;
126  }
127  break;
128  }
129 }
130 
131 void MapExplorer::observe(const sf::Event& event) {
132  if (event.type == sf::Event::KeyPressed) {
133  if (hintState == Showing) { hintState = Fading; }
134  else if (hintState == Hidden) { hintTime = 0.f; }
135  if (event.key.code == sf::Keyboard::Escape) { systems.engine().popState(); }
136  }
137  else if (event.type == sf::Event::MouseMoved) {
138  glm::vec2 wpos = systems.engine().renderer().getObserver().transformToWorldSpace(
139  {event.mouseMove.x, event.mouseMove.y});
140  const glm::i32vec2 tiles = wpos / static_cast<float>(core::Properties::PixelsPerTile());
141  const bl::tmap::Position pos(0, {tiles.x, tiles.y}, bl::tmap::Direction::Up);
142  core::event::EntityMoved move(bl::ecs::InvalidEntity, pos, pos);
143  systems.world().activeMap().observe(move);
144  }
145  else if (event.type == sf::Event::MouseButtonPressed &&
146  event.mouseButton.button == sf::Mouse::Left) {
147  const glm::i32vec2 wpos = systems.engine().renderer().getObserver().transformToWorldSpace(
148  {event.mouseButton.x, event.mouseButton.y});
149  bl::tmap::Position npos = systems.player().position();
150  npos.position = wpos / core::Properties::PixelsPerTile();
151  if (systems.world().activeMap().contains(npos)) {
152  const bl::tmap::Position ogPos = systems.player().position();
153  bl::tmap::Position& playerPos =
154  *systems.engine().ecs().getComponent<bl::tmap::Position>(systems.player().player());
155  playerPos = npos;
156  playerPos.syncTransform(core::Properties::PixelsPerTile());
157  bl::event::Dispatcher::dispatch<core::event::EntityMoved>(
158  {systems.player().player(), ogPos, playerPos});
159  }
160  }
161 }
162 
163 } // namespace state
164 } // namespace game
Parent namespace for all functionality unique to the game.
Fired after an entity begins moving from one position to another.
Definition: EntityMoved.hpp:18
virtual void observe(const event::EntityMoved &moveEvent) override
Event listener for moving entities. Used to trigger map events.
Definition: Map.cpp:385
void setupCamera(system::Systems &systems)
Configures the game camera for the player in the map.
Definition: Map.cpp:160
bool contains(const bl::tmap::Position &position) const
Returns whether or not the map contains the given position.
Definition: Map.cpp:271
static int WindowWidth()
Definition: Properties.cpp:250
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static int PixelsPerTile()
Definition: Properties.cpp:279
static sf::Vector2f WindowSize()
Definition: Properties.cpp:262
const bl::tmap::Position & position() const
Returns the current position of the player.
Definition: Player.cpp:57
void removePlayerControlled(bl::ecs::Entity entity)
Removes the PlayerControlled component from the given entity, if any.
Definition: Player.cpp:91
bl::ecs::Entity player() const
Returns the id of the player entity.
Definition: Player.cpp:55
bool makePlayerControlled(bl::ecs::Entity entity)
Makes the given entity controlled by the player. Only one entity may be player controlled at a time.
Definition: Player.cpp:76
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
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
Debug only state that allows free roaming the map with a special camera.
Definition: MapExplorer.hpp:20
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates a new MapExplorer state.
Definition: MapExplorer.cpp:54
virtual void update(bl::engine::Engine &engine, float dt, float) override
Updates the game world logic and the hint fadeout.
Definition: MapExplorer.cpp:99
virtual const char * name() const override
Returns "MapExplorer".
Definition: MapExplorer.cpp:76
virtual void deactivate(bl::engine::Engine &engine) override
Reconnects the player to their entity and pops the camera.
Definition: MapExplorer.cpp:92
virtual void activate(bl::engine::Engine &engine) override
Activates the state and camera. Disconnects the player from their entity.
Definition: MapExplorer.cpp:78
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