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();
|
||||
[[nodiscard]] Window& getWindow() const {return *window;}
|
||||
EventBus& getEventBus() {return eventBus;}
|
||||
std::unique_ptr<Keyboard> keyboard;
|
||||
std::unique_ptr<Mouse> mouse;
|
||||
std::unique_ptr<StateManager> stateManager;
|
||||
@ -41,7 +40,6 @@ private:
|
||||
static Application* instance;
|
||||
|
||||
std::vector<Layer*> layers;
|
||||
EventBus eventBus;
|
||||
|
||||
void updateTime();
|
||||
|
||||
|
||||
@ -2,4 +2,11 @@
|
||||
// Created by sebastian on 13.02.26.
|
||||
//
|
||||
|
||||
#include "EventBus.h"
|
||||
#include "EventBus.h"
|
||||
|
||||
EventBus & EventBus::getInstance() {
|
||||
static EventBus instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@
|
||||
|
||||
class EventBus {
|
||||
public:
|
||||
~EventBus() = default;
|
||||
static EventBus& getInstance();
|
||||
template<typename Event>
|
||||
using Handler = std::function<void(const Event&)>;
|
||||
|
||||
@ -28,6 +30,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
EventBus() = default;
|
||||
template<typename Event>
|
||||
std::vector<Handler<Event>>& getHandlers() {
|
||||
static std::vector<Handler<Event>> handlers;
|
||||
|
||||
@ -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> gameMode;
|
||||
EventBus& events;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -70,14 +70,14 @@ void GameLayer::onAttach()
|
||||
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);
|
||||
});
|
||||
|
||||
gameInputUser = std::make_unique<GameInputUser>(*entityManager, *mousePicker, *camera, *gameMode);
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ void UILayer::onAttach() {
|
||||
auto turnLabel = std::make_unique<UiText>(*mediumFont, "Runde: 1", turnStyle, glm::vec3(1,1,1));
|
||||
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));
|
||||
if (turnTextLabel) {
|
||||
turnTextLabel->setText("Runde: " + std::to_string(event.newTurn));
|
||||
@ -118,7 +118,7 @@ void UILayer::onAttach() {
|
||||
auto buildingMenuContainer = std::make_unique<UiBuildingMenuContainer>(*smallFont);
|
||||
buildingMenuContainer->addBuildingMenuBtnCallback([](UiBuildingMenuButtonClickEvent e) {
|
||||
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