diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-05-17 15:55:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-17 15:55:37 -0400 |
| commit | 90aadf3844386824140a20d7fbb847bc16009a94 (patch) | |
| tree | 6f83fce90e71abb22b1a8f3e09c79963b2a34d5d /src/system_manager.h | |
| parent | bc63fa57ced1c7329f7fdcfd168eaf7e290158bc (diff) | |
| parent | 86f0106d0523825549f1e74b835688c78a10cf6c (diff) | |
| download | therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.gz therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.bz2 therapy-90aadf3844386824140a20d7fbb847bc16009a94.zip | |
Merge pull request #7 from hatkirby/es-rewrite
The ECS rewrite exceeds the original branch in functionality, so it is time to merge it in.
Diffstat (limited to 'src/system_manager.h')
| -rw-r--r-- | src/system_manager.h | 68 |
1 files changed, 68 insertions, 0 deletions
| diff --git a/src/system_manager.h b/src/system_manager.h new file mode 100644 index 0000000..b03c3f2 --- /dev/null +++ b/src/system_manager.h | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #ifndef SYSTEM_MANAGER_H_544E6056 | ||
| 2 | #define SYSTEM_MANAGER_H_544E6056 | ||
| 3 | |||
| 4 | #include <list> | ||
| 5 | #include <memory> | ||
| 6 | #include <map> | ||
| 7 | #include <typeindex> | ||
| 8 | #include <stdexcept> | ||
| 9 | #include "system.h" | ||
| 10 | |||
| 11 | class SystemManager { | ||
| 12 | private: | ||
| 13 | |||
| 14 | std::list<std::unique_ptr<System>> loop; | ||
| 15 | std::map<std::type_index, System*> systems; | ||
| 16 | |||
| 17 | public: | ||
| 18 | |||
| 19 | template <class T, class... Args> | ||
| 20 | void emplaceSystem(Game& game, Args&&... args) | ||
| 21 | { | ||
| 22 | std::unique_ptr<T> ptr(new T(game, std::forward<Args>(args)...)); | ||
| 23 | std::type_index systemType = typeid(T); | ||
| 24 | |||
| 25 | systems[systemType] = ptr.get(); | ||
| 26 | loop.push_back(std::move(ptr)); | ||
| 27 | } | ||
| 28 | |||
| 29 | template <class T> | ||
| 30 | T& getSystem() | ||
| 31 | { | ||
| 32 | std::type_index systemType = typeid(T); | ||
| 33 | |||
| 34 | if (!systems.count(systemType)) | ||
| 35 | { | ||
| 36 | throw std::invalid_argument("Cannot get non-existent system"); | ||
| 37 | } | ||
| 38 | |||
| 39 | return *dynamic_cast<T*>(systems[systemType]); | ||
| 40 | } | ||
| 41 | |||
| 42 | void tick(double dt) | ||
| 43 | { | ||
| 44 | for (std::unique_ptr<System>& sys : loop) | ||
| 45 | { | ||
| 46 | sys->tick(dt); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | virtual void render(Texture& texture) | ||
| 51 | { | ||
| 52 | for (std::unique_ptr<System>& sys : loop) | ||
| 53 | { | ||
| 54 | sys->render(texture); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | virtual void input(int key, int action) | ||
| 59 | { | ||
| 60 | for (std::unique_ptr<System>& sys : loop) | ||
| 61 | { | ||
| 62 | sys->input(key, action); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | }; | ||
| 67 | |||
| 68 | #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ | ||
