MxEngine
MxObject.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Core/Resources/Mesh.h"
32 #include "Core/Components/Transform.h"
33 #include "Core/Components/InstanceFactory.h"
34 #include "Core/Components/Rendering/MeshRenderer.h"
35 
36 GENERATE_METHOD_CHECK(Init, Init())
37 
38 namespace MxEngine
39 {
40  class MxObject
41  {
42  mutable AABB boundingBox;
43 
44  using EngineHandle = size_t;
45  constexpr static EngineHandle InvalidHandle = std::numeric_limits<EngineHandle>::max();
46  EngineHandle handle = InvalidHandle;
47 
48  public:
49  MxString Name = UUIDGenerator::Get();
50  float TranslateSpeed = 1.0f;
51  float RotateSpeed = 1.0f;
52  float ScaleSpeed = 1.0f;
53  Transform::Handle Transform;
54  private:
55  ComponentManager components;
56  public:
59 
60  static Handle Create();
61  static void Destroy(Handle& object);
62  static void Destroy(MxObject& object);
63 
64  static ComponentView<MxObject> GetObjects();
65  static Handle GetByName(const MxString& name);
66 
67  template<typename T>
68  static MxObject& GetByComponent(T& component)
69  {
70  auto handle = reinterpret_cast<EngineHandle>(component.UserData);
71  MX_ASSERT(handle != InvalidHandle);
72  auto& managedObject = Factory::Get<MxObject>()[handle];
73  return managedObject.value;
74  }
75 
76  template<typename T>
77  static Handle GetHandleByComponent(T& component)
78  {
79  auto handle = reinterpret_cast<EngineHandle>(component.UserData);
80  MX_ASSERT(handle != InvalidHandle);
81  auto& managedObject = Factory::Get<MxObject>()[handle];
82  MX_ASSERT(managedObject.refCount > 0 && managedObject.uuid != UUIDGenerator::GetNull());
83  return MxObjectHandle(managedObject.uuid, handle);
84  }
85 
86  MxObject();
87  MxObject(const MxObject&) = delete;
88  MxObject& operator=(const MxObject&) = delete;
89  MxObject(MxObject&&) = default;
90  MxObject& operator=(MxObject&&) = default;
91  ~MxObject();
92 
93  template<typename T, typename... Args>
94  auto AddComponent(Args&&... args)
95  {
96  auto component = this->components.AddComponent<T>(std::forward<Args>(args)...);
97  component->UserData = reinterpret_cast<void*>(this->handle);
98  if constexpr (has_method_Init<T>::value) component->Init();
99  return component;
100  }
101 
102  template<typename T>
103  auto GetComponent() const
104  {
105  return this->components.GetComponent<T>();
106  }
107 
108  template<typename T>
109  auto GetOrAddComponent()
110  {
111  if (!this->HasComponent<T>())
112  return this->AddComponent<T>();
113  else
114  return this->GetComponent<T>();
115  }
116 
117  template<typename T>
118  void RemoveComponent()
119  {
120  static_assert(!std::is_same_v<T, MxEngine::Transform>, "Transform component cannot be deleted");
121  this->components.RemoveComponent<T>();
122  }
123 
124  template<typename T>
125  bool HasComponent() const
126  {
127  return this->components.HasComponent<T>();
128  }
129 
130  template<typename T>
131  static typename T::Handle GetComponentHandle(const T& component)
132  {
133  return MxObject::GetByComponent(component).GetComponent<T>();
134  }
135 
136  template<typename T>
137  static UUID GetComponentUUID(const T& component)
138  {
139  return MxObject::GetComponentHandle(component).GetUUID();
140  }
141  };
142 }
Definition: AbstractFactory.h:61
Definition: MxObject.h:40
Definition: Transform.h:36
Definition: Component.h:57
Definition: ComponentView.h:42
Definition: AbstractFactory.h:443
Definition: UUID.h:45
Definition: Application.cpp:49
Definition: AABB.h:35