#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"
namespace Msp::Game {
class Stage
{
+public:
+ using EventSource = Game::EventSource<Events::EntityCreated, Events::EntityDestroyed,
+ Events::ComponentCreated, Events::ComponentDestroyed>;
+
private:
+ DataFile::Collection &resources;
PoolPool pools;
EventBus event_bus;
+ EventSource event_source;
/* 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;
public:
- Stage();
+ Stage(DataFile::Collection &);
~Stage();
+ 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; }
Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
template<typename T, typename F>
const std::vector<std::unique_ptr<System>> &get_systems() const { return systems; }
+ template<typename T>
+ T *get_system() const;
+
void tick(Time::TimeDelta);
};
template<typename T, typename F>
-void Stage::iterate_objects(const F &func)
+inline void Stage::iterate_objects(const F &func)
{
pools.get_pool<T>().iterate_objects(func);
}
template<typename T, typename... Args>
-T &Stage::add_system(Args &&... 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());
}
+template<typename T>
+inline T *Stage::get_system() const
+{
+ for(const auto &s: systems)
+ if(T *ts = dynamic_cast<T *>(s.get()))
+ return ts;
+ return nullptr;
+}
+
} // namespace Msp::Game
#endif