]> git.tdb.fi Git - libs/game.git/commitdiff
Add stage activation and events
authorMikko Rasa <tdb@tdb.fi>
Tue, 25 Oct 2022 21:16:04 +0000 (00:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 25 Oct 2022 21:16:04 +0000 (00:16 +0300)
source/game/director.cpp
source/game/director.h
source/game/events.h

index 741965bc290988a7f73210bf453d5cb081533f98..28d86e7d142e23142839e186b8b4613c6299d8f6 100644 (file)
@@ -7,12 +7,25 @@ using namespace std;
 
 namespace Msp::Game {
 
+Director::Director():
+       event_source(event_bus)
+{ }
+
 Stage &Director::create_stage()
 {
        stages.emplace_back(std::make_unique<Stage>());
+       event_source.emit<Events::StageCreated>(std::ref(*stages.back()));
        return *stages.back();
 }
 
+void Director::activate_stage(Stage &s)
+{
+       if(active_stage)
+               event_source.emit<Events::StageDeactivated>(std::ref(s));
+       active_stage = &s;
+       event_source.emit<Events::StageActivated>(std::ref(s));
+}
+
 void Director::tick()
 {
        Time::TimeStamp now = Time::now();
index 24ef6341caaf7e93e784ccc7bde1176621ef7e55..9d221b0678345a8def4c06b5936c3bee95ebe865 100644 (file)
@@ -5,6 +5,9 @@
 #include <vector>
 #include <msp/time/timedelta.h>
 #include <msp/time/timestamp.h>
+#include "eventbus.h"
+#include "events.h"
+#include "eventsource.h"
 
 namespace Msp::Game {
 
@@ -12,8 +15,14 @@ class Stage;
 
 class Director
 {
+public:
+       using EventSource = Game::EventSource<Events::StageCreated, Events::StageActivated, Events::StageDeactivated>;
+
 private:
+       EventBus event_bus;
+       EventSource event_source;
        std::vector<std::unique_ptr<Stage>> stages;
+       Stage *active_stage = nullptr;
        Time::TimeStamp last_tick;
        Time::TimeDelta stepsize = Time::sec/60;
        Time::TimeDelta backlog;
@@ -21,7 +30,9 @@ private:
        unsigned max_backlog_steps = 600;
 
 public:
+       Director();
        Stage &create_stage();
+       void activate_stage(Stage &);
 
        void tick();
 };
index 15988e555baff678cccf4201c80805498cde9b1b..98a6ff0febc3674be666ffbcd3d01965d84cd39e 100644 (file)
@@ -7,6 +7,7 @@ namespace Msp::Game {
 
 class Component;
 class Entity;
+class Stage;
 
 namespace Events {
 
@@ -30,6 +31,21 @@ struct ComponentDestroyed
        Handle<Component> component;
 };
 
+struct StageCreated
+{
+       Stage &stage;
+};
+
+struct StageActivated
+{
+       Stage &stage;
+};
+
+struct StageDeactivated
+{
+       Stage &stage;
+};
+
 } // namespace Events
 } // namespace Msp::Game