RavEngine
Loading...
Searching...
No Matches
Entity.hpp
1#pragma once
2#include "CTTI.hpp"
3#include "Layer.hpp"
4#include "Types.hpp"
5#include "World.hpp"
6
7namespace RavEngine{
8struct World;
9struct Transform;
10
11struct Entity : public AutoCTTI{
12 World* world = nullptr;
13 entity_t id = {INVALID_ENTITY};
14
15 Entity(entity_t id, World* owner) : id(id), world(owner){}
16 Entity(const Entity&) = default;
17 Entity(){}
18 bool operator==(const Entity& other) const{
19 return world == other.world && id == other.GetID();
20 }
21
22 template<typename T, typename ... A>
23 T& EmplaceComponent(A&& ... args) const{
24 return world->EmplaceComponent<T>(id, args...);
25 }
26
27 template<typename T>
28 void DestroyComponent() const{
29 world->DestroyComponent<T>(id);
30 }
31
32 template<typename T>
33 bool HasComponent() const{
34 return world->HasComponent<T>(id);
35 }
36
37 template<typename T>
38 bool HasComponentOfBase() const{
39 return world->HasComponentOfBase<T>(id);
40 }
41
42 template<typename T>
43 auto GetAllComponentsPolymorphic() const{
44 return world->GetAllComponentsPolymorphic<T>(id);
45 }
46
47 template<typename T>
48 T& GetComponent() const{
49 return world->GetComponent<T>(id);
50 }
51
52 entity_t GetID() const{
53 return id;
54 }
55
56 void Destroy(){
57 world->DestroyEntity(id);
58 id = {INVALID_ENTITY};
59 }
60
61 bool IsValid() const{
62 return EntityIsValid(id) && world != nullptr;
63 }
64
65 World* GetWorld() const {
66 return world;
67 }
68
69 void SetEntityRenderlayer(renderlayer_t layers) const{
70 world->SetEntityRenderlayer(id, layers);
71 }
72
73 void SetEntityAttributes(perobject_t attributes) {
74 world->SetEntityAttributes(id, attributes);
75 }
76
77 perobject_t GetEntityAttributes() const {
78 return world->GetEntityAttributes(id);
79 }
80
81 Transform& GetTransform();
82
83 // default create impl
84 // define your own to hide this one
85 inline void Create(){}
86};
87}
88
89namespace std{
90 template<>
91 struct hash<RavEngine::Entity>{
92 inline size_t operator()(const RavEngine::Entity& entity) const {
93 return (size_t(entity.GetID().id) << sizeof(entity_t)) | (uint32_t(uintptr_t(entity.GetWorld())));
94 }
95 };
96}
Definition World.hpp:181
void SetEntityAttributes(entity_t globalid, perobject_t attributes)
Definition World.cpp:547
void SetEntityRenderlayer(entity_t globalid, renderlayer_t layers)
Definition World.cpp:543
perobject_t GetEntityAttributes(entity_t globalid)
Definition World.cpp:552
Definition Animation.hpp:6
Definition Types.hpp:10
Definition CTTI.hpp:14
Definition Entity.hpp:11
Definition Transform.hpp:20