wren
Vulkan-based game engine
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <flecs.h>
4
5#include "components.hpp"
6#include "scene.hpp"
7
8namespace wren::scene {
9
10class Scene;
11
12class Entity {
13 public:
14 Entity(flecs::entity entity, const std::shared_ptr<Scene>& scene)
15 : entity_(entity), scene_(scene) {}
16
17 template <typename T>
18 [[nodiscard]] auto has_component() const -> bool;
19
20 template <typename T>
21 auto get_component() -> T&;
22
23 template <typename T, typename... Args>
24 void add_component(Args&&... args);
25
26 private:
27 flecs::entity entity_;
28
29 std::shared_ptr<Scene> scene_;
30};
31
32template <typename T>
33auto Entity::has_component() const -> bool {
34 return entity_.has<T>();
35}
36
37template <typename T>
39 return *entity_.get_mut<T>();
40}
41
42template <typename T>
43concept HasInit = requires() { T::init(); };
44
45template <typename T, typename... Args>
46void Entity::add_component(Args&&... args) {
47 entity_.add<T>();
48 if (sizeof...(args) > 0) {
49 T t(std::forward<Args...>(args)...);
50 entity_.set<T>(t);
51 }
52
53 if constexpr (HasInit<T>) {
54 T::init(scene_->world());
55 }
56}
57
58} // namespace wren::scene
Definition entity.hpp:12
auto has_component() const -> bool
Definition entity.hpp:33
auto get_component() -> T &
Definition entity.hpp:38
std::shared_ptr< Scene > scene_
Definition entity.hpp:29
Entity(flecs::entity entity, const std::shared_ptr< Scene > &scene)
Definition entity.hpp:14
flecs::entity entity_
Definition entity.hpp:27
void add_component(Args &&... args)
Definition entity.hpp:46
Definition scene.hpp:11
Definition entity.hpp:43
Definition components.cpp:3