void add_component(Handle<Component>);
void remove_component(Handle<Component>);
+ template<typename T>
+ Handle<T> get_component();
+
+ Handle<Transform> get_transform() const { return transform; }
+
void add_child(Handle<Entity>);
void remove_child(Handle<Entity>);
Handle<Entity> get_root();
const std::vector<Handle<Entity>> &get_children() const { return children; }
Stage &get_stage();
- Handle<Transform> get_transform() const { return transform; }
};
+template<typename T>
+inline Handle<T> Entity::get_component()
+{
+ for(Handle<Component> c: components)
+ if(Handle<T> tc = dynamic_handle_cast<T>(c))
+ return tc;
+ return nullptr;
+}
+
inline Handle<Entity> Entity::get_root()
{
Handle<Entity> e = Handle<Entity>::from_object(this);
bool operator==(const Handle &other) const = default;
};
+template<typename T, typename U>
+ requires std::is_base_of_v<U, T>
+Handle<T> dynamic_handle_cast(Handle<U> h)
+{
+ return Handle<T>::from_object(dynamic_cast<T *>(h.get()));
+}
+
} // namespace Msp::Game
#endif
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