ezEngine  Milestone 7
World_inl.h
1 
2 EZ_FORCE_INLINE const char* ezWorld::GetName() const
3 {
4  return m_Data.m_sName.GetData();
5 }
6 
8 {
9  ezGameObject* pNewObject;
10  return CreateObject(desc, pNewObject);
11 }
12 
13 EZ_FORCE_INLINE bool ezWorld::IsValidObject(const ezGameObjectHandle& object) const
14 {
15  CheckForReadAccess();
16  EZ_ASSERT_DEV(object.IsInvalidated() || object.m_InternalId.m_WorldIndex == m_uiIndex,
17  "Object does not belong to this world. Expected world id %d got id %d", m_uiIndex, object.m_InternalId.m_WorldIndex);
18 
19  return m_Data.m_Objects.Contains(object);
20 }
21 
22 EZ_FORCE_INLINE bool ezWorld::TryGetObject(const ezGameObjectHandle& object, ezGameObject*& out_pObject) const
23 {
24  CheckForReadAccess();
25  EZ_ASSERT_DEV(object.IsInvalidated() || object.m_InternalId.m_WorldIndex == m_uiIndex,
26  "Object does not belong to this world. Expected world id %d got id %d", m_uiIndex, object.m_InternalId.m_WorldIndex);
27 
28  ObjectStorageEntry storageEntry = { nullptr };
29  bool bResult = m_Data.m_Objects.TryGetValue(object, storageEntry);
30  out_pObject = storageEntry.m_Ptr;
31  return bResult;
32 }
33 
34 EZ_FORCE_INLINE ezUInt32 ezWorld::GetObjectCount() const
35 {
36  CheckForReadAccess();
37  return m_Data.m_ObjectStorage.GetCount();
38 }
39 
40 EZ_FORCE_INLINE ezInternal::WorldData::ObjectStorage::Iterator ezWorld::GetObjects()
41 {
42  CheckForWriteAccess();
43  return m_Data.m_ObjectStorage.GetIterator(0);
44 }
45 
46 EZ_FORCE_INLINE ezInternal::WorldData::ObjectStorage::ConstIterator ezWorld::GetObjects() const
47 {
48  CheckForReadAccess();
49  return m_Data.m_ObjectStorage.GetIterator(0);
50 }
51 
52 EZ_FORCE_INLINE void ezWorld::Traverse(VisitorFunc visitorFunc, TraversalMethod method /*= DepthFirst*/)
53 {
54  CheckForWriteAccess();
55 
56  if (method == DepthFirst)
57  {
58  m_Data.TraverseDepthFirst(visitorFunc);
59  }
60  else // method == BreadthFirst
61  {
62  m_Data.TraverseBreadthFirst(visitorFunc);
63  }
64 }
65 
66 template <typename ManagerType>
68 {
69  CheckForWriteAccess();
70  EZ_CHECK_AT_COMPILETIME_MSG(EZ_IS_DERIVED_FROM_STATIC(ezComponentManagerBase, ManagerType),
71  "Not a valid component manager type");
72 
73  const ezUInt16 uiTypeId = ManagerType::TypeId();
74  if (uiTypeId >= m_Data.m_ComponentManagers.GetCount())
75  {
76  m_Data.m_ComponentManagers.SetCount(uiTypeId + 1);
77  }
78 
79  ManagerType* pManager = static_cast<ManagerType*>(m_Data.m_ComponentManagers[uiTypeId]);
80  if (pManager == nullptr)
81  {
82  pManager = EZ_NEW(&m_Data.m_Allocator, ManagerType, this);
83  pManager->Initialize();
84 
85  m_Data.m_ComponentManagers[uiTypeId] = pManager;
86  }
87 
88  return pManager;
89 }
90 
91 template <typename ManagerType>
93 {
94  CheckForWriteAccess();
95 
96  const ezUInt16 uiTypeId = ManagerType::TypeId();
97  if (uiTypeId < m_Data.m_ComponentManagers.GetCount())
98  {
99  if (ManagerType* pManager = static_cast<ManagerType*>(m_Data.m_ComponentManagers[uiTypeId]))
100  {
101  m_Data.m_ComponentManagers[uiTypeId] = nullptr;
102 
103  pManager->Deinitialize();
104  DeregisterUpdateFunctions(pManager);
105  EZ_DELETE(&m_Data.m_Allocator, pManager);
106  }
107  }
108 }
109 
110 template <typename ManagerType>
111 EZ_FORCE_INLINE ManagerType* ezWorld::GetComponentManager() const
112 {
113  CheckForReadAccess();
114  EZ_CHECK_AT_COMPILETIME_MSG(EZ_IS_DERIVED_FROM_STATIC(ezComponentManagerBase, ManagerType),
115  "Not a valid component manager type");
116 
117  const ezUInt16 uiTypeId = ManagerType::TypeId();
118  ManagerType* pManager = nullptr;
119  if (uiTypeId < m_Data.m_ComponentManagers.GetCount())
120  {
121  pManager = static_cast<ManagerType*>(m_Data.m_ComponentManagers[uiTypeId]);
122  }
123 
124  EZ_ASSERT_DEV(pManager != nullptr, "Component Manager '%s' (id: %u) does not exists. Call 'CreateComponentManager' first.",
125  ezGetStaticRTTI<typename ManagerType::ComponentType>()->GetTypeName(), uiTypeId);
126  return pManager;
127 }
128 
129 inline bool ezWorld::IsValidComponent(const ezComponentHandle& component) const
130 {
131  CheckForReadAccess();
132  const ezUInt16 uiTypeId = component.m_InternalId.m_TypeId;
133 
134  if (uiTypeId < m_Data.m_ComponentManagers.GetCount())
135  {
136  if (ezComponentManagerBase* pManager = m_Data.m_ComponentManagers[uiTypeId])
137  {
138  return pManager->IsValidComponent(component);
139  }
140  }
141 
142  return false;
143 }
144 
145 template <typename ComponentType>
146 inline bool ezWorld::TryGetComponent(const ezComponentHandle& component, ComponentType*& out_pComponent) const
147 {
148  CheckForReadAccess();
149  EZ_CHECK_AT_COMPILETIME_MSG(EZ_IS_DERIVED_FROM_STATIC(ezComponent, ComponentType),
150  "Not a valid component type");
151 
152  const ezUInt16 uiTypeId = component.m_InternalId.m_TypeId;
153 
154  if (uiTypeId < m_Data.m_ComponentManagers.GetCount())
155  {
156  if (ezComponentManagerBase* pManager = m_Data.m_ComponentManagers[uiTypeId])
157  {
158  ezComponent* pComponent = nullptr;
159  bool bResult = pManager->TryGetComponent(component, pComponent);
160  out_pComponent = static_cast<ComponentType*>(pComponent);
161  return bResult;
162  }
163  }
164 
165  return false;
166 }
167 
168 EZ_FORCE_INLINE void ezWorld::SendMessage(const ezGameObjectHandle& receiverObject, ezMessage& msg,
169  ezObjectMsgRouting::Enum routing /*= ezObjectMsgRouting::Default*/)
170 {
171  CheckForWriteAccess();
172 
173  ezGameObject* pReceiverObject = NULL;
174  if (TryGetObject(receiverObject, pReceiverObject))
175  {
176  pReceiverObject->SendMessage(msg, routing);
177  }
178 }
179 
180 EZ_FORCE_INLINE ezTask* ezWorld::GetUpdateTask()
181 {
182  return &m_UpdateTask;
183 }
184 
185 EZ_FORCE_INLINE const ezInternal::SpatialData& ezWorld::GetSpatialData() const
186 {
187  CheckForReadAccess();
188 
189  return m_SpatialData;
190 }
191 
192 EZ_FORCE_INLINE void ezWorld::GetCoordinateSystem(const ezVec3& vGlobalPosition, ezCoordinateSystem& out_CoordinateSystem) const
193 {
194  m_Data.m_pCoordinateSystemProvider->GetCoordinateSystem(vGlobalPosition, out_CoordinateSystem);
195 }
196 
198 {
199  m_Data.m_pCoordinateSystemProvider = std::move(pProvider);
200  m_Data.m_pCoordinateSystemProvider->m_pOwnerWorld = this;
201 }
202 
204 {
205  return m_Data.m_pCoordinateSystemProvider.Borrow();
206 }
207 
209 {
210  return &m_Data.m_Allocator;
211 }
212 
214 {
215  return &m_Data.m_BlockAllocator;
216 }
217 
219 {
220  return m_Data.m_ReadMarker;
221 }
222 
224 {
225  return m_Data.m_WriteMarker;
226 }
227 
228 EZ_FORCE_INLINE void ezWorld::SetUserData(void* pUserData)
229 {
230  CheckForWriteAccess();
231 
232  m_Data.m_pUserData = pUserData;
233 }
234 
235 EZ_FORCE_INLINE void* ezWorld::GetUserData() const
236 {
237  CheckForReadAccess();
238 
239  return m_Data.m_pUserData;
240 }
241 
242 //static
243 EZ_FORCE_INLINE ezUInt32 ezWorld::GetWorldCount()
244 {
245  return s_Worlds.GetCount();
246 }
247 
248 //static
249 EZ_FORCE_INLINE ezWorld* ezWorld::GetWorld(ezUInt32 uiIndex)
250 {
251  return s_Worlds[uiIndex];
252 }
253 
254 EZ_FORCE_INLINE void ezWorld::CheckForReadAccess() const
255 {
256  EZ_ASSERT_DEV(m_Data.m_iReadCounter > 0, "Trying to read from World '%s', but it is not marked for reading.", GetName());
257 }
258 
259 EZ_FORCE_INLINE void ezWorld::CheckForWriteAccess() const
260 {
261  EZ_ASSERT_DEV(m_Data.m_WriteThreadID == ezThreadUtils::GetCurrentThreadID(), "Trying to write to World '%s', but it is not marked for writing.", GetName());
262 }
263 
264 EZ_FORCE_INLINE ezGameObject* ezWorld::GetObjectUnchecked(ezUInt32 uiIndex) const
265 {
266  return m_Data.m_Objects.GetValueUnchecked(uiIndex).m_Ptr;
267 }