TrueReality  v0.1.1912
EntityBase.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trManager/EntityBase.h>
23 
25 #include <trUtil/Logging/Log.h>
26 
27 namespace trManager
28 {
29  const trUtil::RefStr EntityBase::CLASS_TYPE("trManager::EntityBase");
30 
34 
36  EntityBase::EntityBase(const std::string& name) : BaseClass(name)
37  {
39  }
40 
43  {
44  return mEntityType;
45  }
46 
49  {
50  mSysMan = sysMan;
51  }
52 
54  const bool & EntityBase::IsRegistered()
55  {
56  return mIsRegistered;
57  }
58 
60  void EntityBase::SetRegistration(bool isRegistered)
61  {
62  mIsRegistered = isRegistered;
63  }
64 
67  {
69  if (itor != mInvokables.end())
70  {
71  LOG_W("Could not add new invokable " + newInvokable.GetName() + " for " + GetName() + " because an invokable with that name already exists.")
72  }
73  else
74  {
75  LOG_D("Adding a new invokable " + newInvokable.GetName() + " for " + GetName())
76  mInvokables.insert(std::make_pair(newInvokable.GetName(), trBase::SmrtPtr<Invokable>(&newInvokable)));
77  }
78  }
79 
82  {
83  RemoveInvokable(invokable->GetName());
84  }
85 
87  void EntityBase::RemoveInvokable(const std::string &invokableName)
88  {
90 
91  if (itor != mInvokables.end())
92  {
93  mInvokables.erase(itor);
94  }
95  else
96  {
97  LOG_W("Could not remove invokable " + invokableName + " because an invokable with that name does not exists.")
98  }
99  }
100 
103  {
105 
106  if (itor == mInvokables.end())
107  {
108  return nullptr;
109  }
110  else
111  {
112  return itor->second.Get();
113  }
114  }
115 
117  void EntityBase::GetInvokables(std::vector<trManager::Invokable*> &toFill)
118  {
119  toFill.clear();
120  toFill.reserve(mInvokables.size());
121 
122  for (trUtil::HashMap<std::string, trBase::SmrtPtr<trManager::Invokable>>::iterator i = mInvokables.begin();
123  i != mInvokables.end(); ++i)
124  {
125  toFill.push_back(i->second.Get());
126  }
127  }
128 
130  void EntityBase::GetInvokables(std::vector<const trManager::Invokable*> &toFill) const
131  {
132  toFill.clear();
133  toFill.reserve(mInvokables.size());
134 
135  for (trUtil::HashMap<std::string, trBase::SmrtPtr<trManager::Invokable>>::const_iterator i = mInvokables.begin();
136  i != mInvokables.end(); ++i)
137  {
138  toFill.push_back(i->second.Get());
139  }
140  }
141 
144  {
145  if (child.GetParent() == nullptr)
146  {
147  //Make sure that the only entities that can attach to one another are actors.
149  {
150  mChildren.push_back(*new trBase::SmrtPtr<trManager::EntityBase>(&child));
151  child.SetParent(*this);
152  return true;
153  }
154  else
155  {
156  LOG_E("You can not add " + child.GetEntityType().GetName() + " to " + GetEntityType().GetName() + " as a child.")
157  return false;
158  }
159  }
160  else
161  {
162  LOG_E("You can not add " + child.GetEntityType().GetName() + " to " + GetEntityType().GetName() + " because the child already has a parent " + child.GetParent()->GetName())
163  return false;
164  }
165  }
166 
169  {
170  //Find and remove the child from this entity
171  for (unsigned int i = 0; i < mChildren.size(); ++i)
172  {
173  if (mChildren[i].Get() == &child)
174  {
175  mChildren.erase(mChildren.begin() + i);
176  break;
177  }
178  }
179 
180  //Remove the child's parent
181  if (child.GetParent() != nullptr)
182  {
183  child.Emancipate();
184  }
185  return false;
186  }
187 
190  {
191  for (int i = mChildren.size() - 1; i >= 0; --i)
192  {
193  //Remove the parental link to this entity.
194  mChildren[i]->ForgetParent();
195  }
196  //Clear the children list
197  mChildren.clear();
198  return true;
199  }
200 
202  std::vector<trBase::SmrtPtr<trManager::EntityBase>>* EntityBase::GetChildren()
203  {
204  return &mChildren;
205  }
206 
208  const std::vector<trBase::SmrtPtr<trManager::EntityBase>>* EntityBase::GetChildren() const
209  {
210  return &mChildren;
211  }
212 
215  {
216  //Find and remove the child from this entity
217  for (unsigned int i = 0; i < mChildren.size(); ++i)
218  {
219  if (mChildren[i]->GetUUID() == childId)
220  {
221  return mChildren[i];
222  }
223  }
224 
225  //If the child was not found, return NULL;
226  return nullptr;
227  }
228 
231  {
232  return mChildren.size();
233  }
234 
237  {
238  if (!mParent.Valid())
239  {
240  //Set the new parent
241  mParent = &parent;
242 
243  //Call the On Parent Set callback
244  OnParentSet(parent);
245  }
246  else
247  {
248  LOG_E("Entity " + GetName() + " already has a parent " + mParent->GetName())
249  }
250  }
251 
254  {
255  //Make a copy of the parent before disconnecting from it.
257 
258  mParent = nullptr;
259 
260  OnParentRemoved(*parent);
261  }
262 
265  {
266  return mParent.Get();
267  }
268 
271  {
272  return mParent.Get();
273  }
274 
277  {
278  }
279 
282  {
283  }
284 
287  {
288  //Make a copy of the parent before disconnecting from it.
290 
291 
292  //Remove this Entity from its previous parent.
293  if (!parent.Valid())
294  {
295  ForgetParent();
296  parent->RemoveChild(*this);
297  }
298  else
299  {
300  LOG_W("The Entity " + GetName() + " was not attached to a parent.")
301  }
302  }
303 
306  {
307  if (mParent.Valid())
308  {
309  for (unsigned int i = 0; i < mChildren.size(); ++i)
310  {
311  //Remove the child's parent (this entity)
312  mChildren[i]->ForgetParent();
313 
314  //Add the child to this entities previous parent.
315  if (!mParent->AddChild(*mChildren[i]))
316  {
317  return false;
318  }
319  }
320 
321  //Delete all references to the children;
322  mChildren.clear();
323 
324  //Remove this entities parent.
325  ForgetParent();
326 
327  return true;
328  }
329  else
330  {
331  LOG_E(GetName() + " is not attached to a parent. Can't remove it from Hierarchy.")
332  return false;
333  }
334  }
335 
338  {
339  }
340 }
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
Definition: EntityBase.h:52
void GetInvokables(std::vector< trManager::Invokable *> &toFill)
Gets the list of invokables.
Definition: EntityBase.cpp:117
static const EntityType INVALID
Definition: EntityType.h:45
virtual const trManager::EntityBase * FindChild(const trBase::UniqueId &childId)
Finds a child of this Entity in the children list.
Definition: EntityBase.cpp:214
virtual const std::string & GetName()
Returns this instances name.
Definition: Base.cpp:49
static const trUtil::RefStr ON_TICK_REMOTE_INVOKABLE
Invokable for Tick messages.
Definition: EntityBase.h:56
This class creates a GUID, or a Unique ID that is used through out TR to identify and distinguish one...
Definition: UniqueId.h:42
virtual void SetSystemManager(trManager::SystemManager *sysMan)
This method is used by the System Manager to pass the Entity an instance if itself when it is registe...
Definition: EntityBase.cpp:48
Smart pointer for handling referenced counted objects.
Definition: SmrtPtr.h:36
const std::string & GetName() const
Definition: Invokable.h:118
const std::string & GetName() const
Inlined because it&#39;s called frequently.
virtual std::vector< trBase::SmrtPtr< trManager::EntityBase > > * GetChildren()
Definition: EntityBase.cpp:202
An Invokable is a queriable method interface that can be added to a trManager::ActorBase Invoking the...
Definition: Invokable.h:84
A string wrapper that will make sure that all of the strings with the same value will point to the sa...
Definition: RefStr.h:50
virtual void Emancipate()
Removes the hierarchal parent of this Entity, and removes this Entity from the parent.
Definition: EntityBase.cpp:286
A class that specifies what kind of entity an object is in the Entity System.
Definition: EntityType.h:37
virtual void OnParentRemoved(trManager::EntityBase &parent)
Convenience function that will be called by the system when the Entities parent is removed...
Definition: EntityBase.cpp:276
EntityBase(const std::string &name=CLASS_TYPE)
Invokable for Tick Remote messages.
Definition: EntityBase.cpp:36
trUtil::EnumerationPointer< const trManager::EntityType > mEntityType
Definition: EntityBase.h:352
System Manager class is a singleton that is responsible for all message routing and basic operations ...
Definition: SystemManager.h:46
static const trUtil::RefStr ON_TICK_INVOKABLE
Invokable for general messages.
Definition: EntityBase.h:55
#define LOG_W(msg)
Log a WARNING message.
Definition: Log.h:156
This class is part of the internal garbage collection system.
Definition: SmrtClass.h:38
static const EntityType ACTOR
Definition: EntityType.h:42
virtual void AddInvokable(trManager::Invokable &newInvokable)
Adds an invokable that can receive a message.
Definition: EntityBase.cpp:66
static const trUtil::RefStr ON_MESSAGE_INVOKABLE
Holds the class type name for efficient comparisons.
Definition: EntityBase.h:54
trBase::ObsrvrPtr< trManager::SystemManager > mSysMan
Definition: EntityBase.h:351
virtual void RemoveInvokable(trManager::Invokable *invokable)
Removes the invokable that is passed in.
Definition: EntityBase.cpp:81
virtual void ForgetParent()
Forgets the hierarchal parent of this Entiry.
Definition: EntityBase.cpp:253
#define LOG_E(msg)
Log an ERROR message.
Definition: Log.h:165
trManager::Invokable * GetInvokable(const std::string &name)
Gets a registered invokable.
Definition: EntityBase.cpp:102
bool Valid() const
Returns True if the smart pointer has a valid internal pointer set.
Definition: SmrtPtr.h:103
virtual const trBase::UniqueId & GetUUID(void)
Returns the instances Universally Unique ID.
Definition: Base.cpp:67
virtual const bool & IsRegistered()
Returns True if the Instance is registered with a System Manager.
Definition: EntityBase.h:107
virtual bool AddChild(trManager::EntityBase &child)
Adds a child to this Entity.
Definition: EntityBase.cpp:143
virtual bool RemoveFromHierarchy()
Removes from this entity from the hierarchy tree attaching its children to its parent.
Definition: EntityBase.cpp:305
trBase::SmrtPtr< trManager::EntityBase > mParent
Definition: EntityBase.h:361
virtual bool RemoveAllChildren()
Removes all of the entities children .
Definition: EntityBase.cpp:189
trUtil::HashMap< std::string, trBase::SmrtPtr< trManager::Invokable > > mInvokables
Definition: EntityBase.h:353
T * Get() const
Returns the stored internal pointer.
Definition: SmrtPtr.h:73
std::vector< trBase::SmrtPtr< trManager::EntityBase > > mChildren
Definition: EntityBase.h:360
const EntityType & GetEntityType()
Returns the Entity Type, which is usually a Director, Actor, or an Actor module.
Definition: EntityBase.cpp:42
This serves as the base class for the Entity class and removes a circular dependency between Entity a...
Definition: EntityBase.h:46
virtual void SetRegistration(bool isRegistered)
Is set to True by the System Manager when the class instance is registered with it.
Definition: EntityBase.cpp:60
virtual int GetNumOfChildren()
Gets the children of this Entity has.
Definition: EntityBase.cpp:230
virtual bool RemoveChild(trManager::EntityBase &child)
Removes the child from this Entity.
Definition: EntityBase.cpp:168
trManager::EntityBase * GetParent()
Gets the parent of this hierarchal Entity.
Definition: EntityBase.cpp:264
#define LOG_D(msg)
Log a DEBUG message.
Definition: Log.h:138
virtual void SetParent(trManager::EntityBase &parent)
Sets the hierarchal parent of this Entity.
Definition: EntityBase.cpp:236
virtual void OnParentSet(trManager::EntityBase &parent)
Convenience function that will be called by the system when a new parent is added or set to the Entit...
Definition: EntityBase.cpp:281