]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/stage.h
Schedule systems based on their declared dependencies
[libs/game.git] / source / game / stage.h
index d7793e22f60ec21fd52acab1b8af9aafbe2f4d94..0c2219bdc18b1d893851ed91467677c5a35fc4b1 100644 (file)
@@ -2,14 +2,18 @@
 #define MSP_GAME_STAGE_H_
 
 #include <memory>
+#include <msp/datafile/collection.h>
 #include <msp/time/timedelta.h>
 #include "eventbus.h"
 #include "events.h"
 #include "eventsource.h"
 #include "handle.h"
+#include "reflection.h"
+#include "systemscheduler.h"
 
 namespace Msp::Game {
 
+class Camera;
 class Root;
 class System;
 
@@ -17,21 +21,29 @@ class Stage
 {
 public:
        using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
-               Events::ComponentCreated, Events::ComponentDestroyed>;
+               Events::ComponentCreated, Events::ComponentDestroyed, Events::CameraChanged>;
 
 private:
+       Reflection::Reflector &reflector;
+       DataFile::Collection &resources;
        PoolPool pools;
        EventBus event_bus;
        EventSource event_source;
+       EventObserver event_observer;
        /* Use unique_ptr because there's only one root per stage so it's pointless
        to put it in a pool. */
        std::unique_ptr<Root> root;
        std::vector<std::unique_ptr<System>> systems;
+       SystemScheduler scheduler;
+       Handle<Camera> active_camera;
+       bool pending_reschedule = false;
 
 public:
-       Stage();
+       Stage(Reflection::Reflector &, DataFile::Collection &);
        ~Stage();
 
+       Reflection::Reflector &get_reflector() const { return reflector; }
+       DataFile::Collection &get_resources() const { return resources; }
        PoolPool &get_pools() { return pools; }
        EventBus &get_event_bus() { return event_bus; }
        EventSource &get_event_source() { return event_source; }
@@ -43,11 +55,20 @@ public:
        template<typename T, typename... Args>
        T &add_system(Args &&...);
 
+       void remove_system(System &);
        const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
 
        template<typename T>
        T *get_system() const;
 
+       void set_active_camera(Handle<Camera>);
+       Handle<Camera> get_active_camera() const { return active_camera; }
+
+       void synthesize_initial_events(EventObserver &);
+private:
+       void synthesize_initial_events(Handle<Entity>, EventObserver &);
+
+public:
        void tick(Time::TimeDelta);
 };
 
@@ -60,8 +81,13 @@ inline void Stage::iterate_objects(const F &func)
 template<typename T, typename... Args>
 inline T &Stage::add_system(Args &&... args)
 {
-       systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
-       return static_cast<T &>(*systems.back());
+       // Ensure that a reflected class exists for scheduling
+       reflector.get_or_create_class<T>();
+
+       auto &sys = systems.emplace_back(std::make_unique<T>(*this, std::forward<Args>(args)...));
+       scheduler.add_system(*sys);
+       pending_reschedule = true;
+       return static_cast<T &>(*sys);
 }
 
 template<typename T>