3 #include <BLIB/Cameras.hpp>
4 #include <BLIB/Logging.hpp>
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);
21 class ExplorerCameraController :
public bl::cam::CameraController2D {
24 : position(systems.player().position().transform->getLocalPosition())
31 virtual void update(
float dt)
override {
32 const float PixelsPerSecond =
34 const float ZoomPerSecond = std::max(0.5f, zoom * 0.5f);
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;
44 if (sf::Keyboard::isKeyPressed(sf::Keyboard::V)) { zoom += ZoomPerSecond * dt; }
47 camera().setCenter(position);
48 camera().setSize({size.x, size.y});
59 :
State(systems,
bl::engine::StateMask::Menu) {
61 hintBox.setFillColor(BoxColor);
62 hintBox.setOutlineColor(BoxOutlineColor);
63 hintBox.setOutlineThickness(4.f);
64 hintBox.getTransform().setPosition(5.f, 5.f);
68 "Move around: WASD\nZoom out: V\nZoom in: C\nExit: Esc",
71 hintText.getTransform().setPosition(Padding, Padding);
72 hintText.setParent(hintBox);
73 hintBox.setSize(hintText.getLocalSize() + glm::vec2(Padding * 2.f));
79 bl::event::Dispatcher::subscribe(
this);
81 static_cast<bl::cam::Camera2D*
>(engine.renderer().getObserver().getCurrentCamera())
82 ->setController<ExplorerCameraController>(
systems);
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);
88 hintBox.setHidden(
true);
93 bl::event::Dispatcher::unsubscribe(
this);
96 hintBox.removeFromScene();
103 if (hintTime >= 4.f) {
106 hintBox.setHidden(
false);
107 hintBox.setFillColor(BoxColor);
108 hintBox.setOutlineColor(BoxOutlineColor);
109 hintText.getSection().setFillColor(TextColor);
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));
124 hintBox.setHidden(
true);
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(); }
137 else if (event.type == sf::Event::MouseMoved) {
138 glm::vec2 wpos =
systems.
engine().renderer().getObserver().transformToWorldSpace(
139 {
event.mouseMove.x,
event.mouseMove.y});
141 const bl::tmap::Position pos(0, {tiles.x, tiles.y}, bl::tmap::Direction::Up);
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});
153 bl::tmap::Position& playerPos =
157 bl::event::Dispatcher::dispatch<core::event::EntityMoved>(
Parent namespace for all functionality unique to the game.
Fired after an entity begins moving from one position to another.
virtual void observe(const event::EntityMoved &moveEvent) override
Event listener for moving entities. Used to trigger map events.
void setupCamera(system::Systems &systems)
Configures the game camera for the player in the map.
bool contains(const bl::tmap::Position &position) const
Returns whether or not the map contains the given position.
static const sf::VulkanFont & MenuFont()
static int PixelsPerTile()
static sf::Vector2f WindowSize()
const bl::tmap::Position & position() const
Returns the current position of the player.
void removePlayerControlled(bl::ecs::Entity entity)
Removes the PlayerControlled component from the given entity, if any.
bl::ecs::Entity player() const
Returns the id of the player entity.
bool makePlayerControlled(bl::ecs::Entity entity)
Makes the given entity controlled by the player. Only one entity may be player controlled at a time.
Owns all primary systems and a reference to the engine.
const bl::engine::Engine & engine() const
Const accessor for the Engine.
Player & player()
Returns the player system.
World & world()
Modifiable accessor for the world system.
map::Map & activeMap()
Returns a reference to the active map.
Debug only state that allows free roaming the map with a special camera.
static bl::engine::State::Ptr create(core::system::Systems &systems)
Creates a new MapExplorer state.
virtual void update(bl::engine::Engine &engine, float dt, float) override
Updates the game world logic and the hint fadeout.
virtual const char * name() const override
Returns "MapExplorer".
virtual void deactivate(bl::engine::Engine &engine) override
Reconnects the player to their entity and pops the camera.
virtual void activate(bl::engine::Engine &engine) override
Activates the state and camera. Disconnects the player from their entity.
Parent to all game states. Provides some commonly required data like core game systems.
core::system::Systems & systems