UPD: Remove Dependency to Application for EventBus, closes #5
This commit is contained in:
parent
1b4cc8ef89
commit
203cf42c26
@ -28,7 +28,6 @@ public:
|
|||||||
|
|
||||||
static Application& getInstance();
|
static Application& getInstance();
|
||||||
[[nodiscard]] Window& getWindow() const {return *window;}
|
[[nodiscard]] Window& getWindow() const {return *window;}
|
||||||
EventBus& getEventBus() {return eventBus;}
|
|
||||||
std::unique_ptr<Keyboard> keyboard;
|
std::unique_ptr<Keyboard> keyboard;
|
||||||
std::unique_ptr<Mouse> mouse;
|
std::unique_ptr<Mouse> mouse;
|
||||||
std::unique_ptr<StateManager> stateManager;
|
std::unique_ptr<StateManager> stateManager;
|
||||||
@ -41,7 +40,6 @@ private:
|
|||||||
static Application* instance;
|
static Application* instance;
|
||||||
|
|
||||||
std::vector<Layer*> layers;
|
std::vector<Layer*> layers;
|
||||||
EventBus eventBus;
|
|
||||||
|
|
||||||
void updateTime();
|
void updateTime();
|
||||||
|
|
||||||
|
|||||||
@ -3,3 +3,10 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "EventBus.h"
|
#include "EventBus.h"
|
||||||
|
|
||||||
|
EventBus & EventBus::getInstance() {
|
||||||
|
static EventBus instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
class EventBus {
|
class EventBus {
|
||||||
public:
|
public:
|
||||||
|
~EventBus() = default;
|
||||||
|
static EventBus& getInstance();
|
||||||
template<typename Event>
|
template<typename Event>
|
||||||
using Handler = std::function<void(const Event&)>;
|
using Handler = std::function<void(const Event&)>;
|
||||||
|
|
||||||
@ -28,6 +30,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
EventBus() = default;
|
||||||
template<typename Event>
|
template<typename Event>
|
||||||
std::vector<Handler<Event>>& getHandlers() {
|
std::vector<Handler<Event>>& getHandlers() {
|
||||||
static std::vector<Handler<Event>> handlers;
|
static std::vector<Handler<Event>> handlers;
|
||||||
|
|||||||
@ -15,7 +15,7 @@ class GameMode;
|
|||||||
|
|
||||||
class Layer {
|
class Layer {
|
||||||
public:
|
public:
|
||||||
Layer() : events(Application::getInstance().getEventBus()) {}
|
Layer() = default;
|
||||||
virtual ~Layer() = default;
|
virtual ~Layer() = default;
|
||||||
virtual void onAttach() {}
|
virtual void onAttach() {}
|
||||||
virtual void onDetach() {}
|
virtual void onDetach() {}
|
||||||
@ -27,7 +27,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<GameMode> gameMode;
|
std::shared_ptr<GameMode> gameMode;
|
||||||
EventBus& events;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -70,14 +70,14 @@ void GameLayer::onAttach()
|
|||||||
AssetManager::loadTexture("upgrade", "assets/worldIcons/up-arrow.png", loader);
|
AssetManager::loadTexture("upgrade", "assets/worldIcons/up-arrow.png", loader);
|
||||||
|
|
||||||
|
|
||||||
events.subscribe<TurnChangedEvent>([this](const TurnChangedEvent& event) {
|
EventBus::getInstance().subscribe<TurnChangedEvent>([this](const TurnChangedEvent& event) {
|
||||||
ProducingSystem::onTurnEnded(*entityManager);
|
ProducingSystem::onTurnEnded(*entityManager);
|
||||||
});
|
});
|
||||||
|
|
||||||
gameInputUser = std::make_unique<GameInputUser>(*entityManager, *mousePicker, *camera, *gameMode);
|
gameInputUser = std::make_unique<GameInputUser>(*entityManager, *mousePicker, *camera, *gameMode);
|
||||||
Application::getInstance().stateManager->registerMouseUser(gameInputUser.get(), {StateRegistry::get().game});
|
Application::getInstance().stateManager->registerMouseUser(gameInputUser.get(), {StateRegistry::get().game});
|
||||||
|
|
||||||
events.subscribe<BuildingTypeSelectEvent>([this](const BuildingTypeSelectEvent& event) {
|
EventBus::getInstance().subscribe<BuildingTypeSelectEvent>([this](const BuildingTypeSelectEvent& event) {
|
||||||
gameMode->setActiveBuilding(event.selectedBuildingType);
|
gameMode->setActiveBuilding(event.selectedBuildingType);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,5 +29,5 @@ void GameWorldSystems::update(EntityManager &em, GameMode &gm, MousePicker &mp,
|
|||||||
|
|
||||||
void GameWorldSystems::onTurnEnd(EntityManager& em, GameMode& gm, TurnState& turnState) {
|
void GameWorldSystems::onTurnEnd(EntityManager& em, GameMode& gm, TurnState& turnState) {
|
||||||
ProducingSystem::onTurnEnded(em);
|
ProducingSystem::onTurnEnded(em);
|
||||||
TurnSystem::nextTurn(turnState, Application::getInstance().getEventBus());
|
TurnSystem::nextTurn(turnState, EventBus::getInstance());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -84,7 +84,7 @@ void UILayer::onAttach() {
|
|||||||
auto turnLabel = std::make_unique<UiText>(*mediumFont, "Runde: 1", turnStyle, glm::vec3(1,1,1));
|
auto turnLabel = std::make_unique<UiText>(*mediumFont, "Runde: 1", turnStyle, glm::vec3(1,1,1));
|
||||||
turnTextID = rootContainer->addChild(std::move(turnLabel));
|
turnTextID = rootContainer->addChild(std::move(turnLabel));
|
||||||
|
|
||||||
events.subscribe<TurnChangedEvent>([this](const TurnChangedEvent& event) {
|
EventBus::getInstance().subscribe<TurnChangedEvent>([this](const TurnChangedEvent& event) {
|
||||||
auto turnTextLabel = dynamic_cast<UiText*>(rootContainer->getChildAtIndex(turnTextID));
|
auto turnTextLabel = dynamic_cast<UiText*>(rootContainer->getChildAtIndex(turnTextID));
|
||||||
if (turnTextLabel) {
|
if (turnTextLabel) {
|
||||||
turnTextLabel->setText("Runde: " + std::to_string(event.newTurn));
|
turnTextLabel->setText("Runde: " + std::to_string(event.newTurn));
|
||||||
@ -118,7 +118,7 @@ void UILayer::onAttach() {
|
|||||||
auto buildingMenuContainer = std::make_unique<UiBuildingMenuContainer>(*smallFont);
|
auto buildingMenuContainer = std::make_unique<UiBuildingMenuContainer>(*smallFont);
|
||||||
buildingMenuContainer->addBuildingMenuBtnCallback([](UiBuildingMenuButtonClickEvent e) {
|
buildingMenuContainer->addBuildingMenuBtnCallback([](UiBuildingMenuButtonClickEvent e) {
|
||||||
if (e.originalEventData.isClick(MouseButton::LEFT)) {
|
if (e.originalEventData.isClick(MouseButton::LEFT)) {
|
||||||
Application::getInstance().getEventBus().emit(BuildingTypeSelectEvent{e.buildingType});
|
EventBus::getInstance().emit(BuildingTypeSelectEvent{e.buildingType});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user