diff --git a/src/engine/core/Application.h b/src/engine/core/Application.h index d15915f..28be5b5 100644 --- a/src/engine/core/Application.h +++ b/src/engine/core/Application.h @@ -28,7 +28,6 @@ public: static Application& getInstance(); [[nodiscard]] Window& getWindow() const {return *window;} - EventBus& getEventBus() {return eventBus;} std::unique_ptr keyboard; std::unique_ptr mouse; std::unique_ptr stateManager; @@ -41,7 +40,6 @@ private: static Application* instance; std::vector layers; - EventBus eventBus; void updateTime(); diff --git a/src/engine/core/events/EventBus.cpp b/src/engine/core/events/EventBus.cpp index 0305a22..eda80cb 100644 --- a/src/engine/core/events/EventBus.cpp +++ b/src/engine/core/events/EventBus.cpp @@ -2,4 +2,11 @@ // Created by sebastian on 13.02.26. // -#include "EventBus.h" \ No newline at end of file +#include "EventBus.h" + +EventBus & EventBus::getInstance() { + static EventBus instance; + return instance; +} + + diff --git a/src/engine/core/events/EventBus.h b/src/engine/core/events/EventBus.h index 87b9a9a..370d0db 100644 --- a/src/engine/core/events/EventBus.h +++ b/src/engine/core/events/EventBus.h @@ -10,6 +10,8 @@ class EventBus { public: + ~EventBus() = default; + static EventBus& getInstance(); template using Handler = std::function; @@ -28,6 +30,7 @@ public: } private: + EventBus() = default; template std::vector>& getHandlers() { static std::vector> handlers; diff --git a/src/engine/layer/Layer.h b/src/engine/layer/Layer.h index abe8df6..6a9a9a4 100644 --- a/src/engine/layer/Layer.h +++ b/src/engine/layer/Layer.h @@ -15,7 +15,7 @@ class GameMode; class Layer { public: - Layer() : events(Application::getInstance().getEventBus()) {} + Layer() = default; virtual ~Layer() = default; virtual void onAttach() {} virtual void onDetach() {} @@ -27,7 +27,6 @@ public: protected: std::shared_ptr gameMode; - EventBus& events; }; diff --git a/src/game/GameLayer.cpp b/src/game/GameLayer.cpp index eabc716..5e1eb1b 100644 --- a/src/game/GameLayer.cpp +++ b/src/game/GameLayer.cpp @@ -70,14 +70,14 @@ void GameLayer::onAttach() AssetManager::loadTexture("upgrade", "assets/worldIcons/up-arrow.png", loader); - events.subscribe([this](const TurnChangedEvent& event) { + EventBus::getInstance().subscribe([this](const TurnChangedEvent& event) { ProducingSystem::onTurnEnded(*entityManager); }); gameInputUser = std::make_unique(*entityManager, *mousePicker, *camera, *gameMode); Application::getInstance().stateManager->registerMouseUser(gameInputUser.get(), {StateRegistry::get().game}); - events.subscribe([this](const BuildingTypeSelectEvent& event) { + EventBus::getInstance().subscribe([this](const BuildingTypeSelectEvent& event) { gameMode->setActiveBuilding(event.selectedBuildingType); }); } diff --git a/src/game/GameWorldSystems.cpp b/src/game/GameWorldSystems.cpp index fea2c15..a1a49d2 100644 --- a/src/game/GameWorldSystems.cpp +++ b/src/game/GameWorldSystems.cpp @@ -29,5 +29,5 @@ void GameWorldSystems::update(EntityManager &em, GameMode &gm, MousePicker &mp, void GameWorldSystems::onTurnEnd(EntityManager& em, GameMode& gm, TurnState& turnState) { ProducingSystem::onTurnEnded(em); - TurnSystem::nextTurn(turnState, Application::getInstance().getEventBus()); + TurnSystem::nextTurn(turnState, EventBus::getInstance()); } diff --git a/src/game/UILayer.cpp b/src/game/UILayer.cpp index e0b6076..c30d836 100644 --- a/src/game/UILayer.cpp +++ b/src/game/UILayer.cpp @@ -84,7 +84,7 @@ void UILayer::onAttach() { auto turnLabel = std::make_unique(*mediumFont, "Runde: 1", turnStyle, glm::vec3(1,1,1)); turnTextID = rootContainer->addChild(std::move(turnLabel)); - events.subscribe([this](const TurnChangedEvent& event) { + EventBus::getInstance().subscribe([this](const TurnChangedEvent& event) { auto turnTextLabel = dynamic_cast(rootContainer->getChildAtIndex(turnTextID)); if (turnTextLabel) { turnTextLabel->setText("Runde: " + std::to_string(event.newTurn)); @@ -118,7 +118,7 @@ void UILayer::onAttach() { auto buildingMenuContainer = std::make_unique(*smallFont); buildingMenuContainer->addBuildingMenuBtnCallback([](UiBuildingMenuButtonClickEvent e) { if (e.originalEventData.isClick(MouseButton::LEFT)) { - Application::getInstance().getEventBus().emit(BuildingTypeSelectEvent{e.buildingType}); + EventBus::getInstance().emit(BuildingTypeSelectEvent{e.buildingType}); } });