#include "entity.h"
#include "component.h"
-#include "stage.h"
+#include "root.h"
using namespace std;
erase(children, child);
}
+Stage &Entity::get_stage()
+{
+ Handle<Entity> root = get_root();
+ return dynamic_cast<Root &>(*root).get_stage();
+}
+
} // namespace Msp::Game
namespace Msp::Game {
class Component;
+class Stage;
class hierarchy_error: public std::logic_error
{
Handle<Entity> get_parent() const { return parent; }
Handle<Entity> get_root();
+ Stage &get_stage();
};
#include <stdexcept>
#include "handle.h"
+#include "stage.h"
namespace Msp::Game {
class Component;
class Entity;
-class Root;
-class Stage;
template<typename T>
class Owned: public Handle<T>
template<typename O>
Stage &Owned<T>::get_stage(O &obj)
{
- using DependentRoot = std::conditional_t<sizeof(T), Root, Root>;
if constexpr(std::is_base_of_v<Component, O>)
return get_stage(*obj.get_entity());
- else if constexpr(std::is_base_of_v<Entity, O>)
- return dynamic_cast<DependentRoot &>(*obj.get_root()).get_stage();
else
- return obj;
+ return obj.get_stage();
}
template<typename T>
#include "stage.h"
+#include "root.h"
#include "system.h"
namespace Msp::Game {
Stage::Stage():
- root(*this)
+ root(std::make_unique<Root>(*this))
{ }
-// Hide ~unique_ptr<System> from the header
+// Hide unique_ptr destructors from the header
Stage::~Stage()
{ }
#include <memory>
#include <msp/time/timedelta.h>
#include "handle.h"
-#include "root.h"
namespace Msp::Game {
+class Root;
class System;
class Stage
{
private:
PoolPool pools;
- Root root;
+ /* 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();
PoolPool &get_pools() { return pools; }
- Handle<Root> get_root() { return Handle<Root>::from_object(&root); }
+ Handle<Root> get_root() { return Handle<Root>::from_object(root.get()); }
template<typename T, typename F>
void iterate_objects(const F &);