RavEngine
Loading...
Searching...
No Matches
ComponentHandle.hpp
1#pragma once
2#include "Entity.hpp"
3
4namespace RavEngine{
6 protected:
7 Entity owner;
8 public:
9 ComponentHandleBase(decltype(owner) owner ) : owner(owner){}
10
11 inline operator bool () const{
12 return IsValid();
13 }
14
15 inline void reset(){
16 owner = Entity({INVALID_ENTITY}, nullptr);
17 }
18
19 inline bool IsValid() const{
20 return owner.IsValid();
21 }
22
23 inline decltype(owner) GetOwner() const{
24 return owner;
25 }
26
27 inline auto get_id() const{
28 return GetOwner().id.id;
29 }
30 };
31
32 template<typename T>
34 ComponentHandle(decltype(owner) owner) : ComponentHandleBase(owner){
35 //assert(owner.HasComponent<T>());
36 }
37 ComponentHandle() : ComponentHandleBase({{INVALID_ENTITY}, nullptr}){}
39
40 inline T* operator->(){
41 return get();
42 }
43
44 inline T* get(){
45 //assert(EntityIsValid(owner.id));
46 //assert(owner.HasComponent<T>());
47 return &owner.GetComponent<T>();
48 }
49
53 template<typename U>
54 inline U* get_as(){
55 return static_cast<U*>(get());
56 }
57
58 inline bool operator==(const ComponentHandle<T>& other) const{
59 return owner.id == other.owner.id;
60 }
61 };
62
63 template<typename Base>
65 ctti_t full_type_id;
66
67 template<typename Full>
69 assert(owner.HasComponentOfBase<Base>());
70 }
71
72 PolymorphicComponentHandle(decltype(owner) owner, decltype(full_type_id) id) : full_type_id(id),ComponentHandleBase(owner){}
73
74 inline bool operator==(const PolymorphicComponentHandle<Base>& other){
75 return owner.id == other.owner.id && full_type_id == other.full_type_id;
76 }
77
78 inline Base* get(){
79 assert(EntityIsValid(owner.id));
80 auto matching = owner.GetAllComponentsPolymorphic<Base>();
81 for(auto& comp : matching){
82 if (comp.full_id == full_type_id){
83 return comp.template Get<Base>(GetOwner().GetID());
84 }
85 }
86 return nullptr;
87 }
88
89 inline Base* operator->(){
90 return get();
91 }
92
93 template<typename T>
94 inline T* get_as(){
95 return static_cast<T*>(get());
96 }
97 };
98}
99
100namespace std{
101 template<typename T>
102 struct hash<RavEngine::ComponentHandle<T>>{
103 inline size_t operator()(const RavEngine::ComponentHandle<T>& ch) const{
104 return ch.GetOwner().id.id;
105 }
106 };
107
108template<typename T>
109struct hash<RavEngine::PolymorphicComponentHandle<T>>{
110 inline size_t operator()(const RavEngine::PolymorphicComponentHandle<T>& ch) const{
111 return ch.GetOwner().id.id ^ ch.full_type_id;
112 }
113};
114}
Definition ComponentHandle.hpp:5
Definition concurrentqueue.h:747
Definition Animation.hpp:6
Definition ComponentHandle.hpp:33
U * get_as()
Definition ComponentHandle.hpp:54
Definition Entity.hpp:11
Definition ComponentHandle.hpp:64