TrueReality  v0.1.1912
ActorBase.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/ActorBase.h>
23 
25 #include <trManager/Invokable.h>
26 #include <trManager/EntityType.h>
27 #include <trUtil/Functor.h>
28 #include <trUtil/Logging/Log.h>
29 
30 namespace trManager
31 {
32  const trUtil::RefStr ActorBase::CLASS_TYPE("trManager::ActorBase");
33 
35  ActorBase::ActorBase(const std::string& name) : BaseClass(name)
36  {
39  }
40 
43  {
44  }
45 
48  {
49  //Access i by reference
50  for (auto&& i : mActorModules)
51  {
52  i->OnTick(msg);
53  }
54  }
55 
57  void ActorBase::RegisterForMessage(const std::string& messageType, const std::string& invokableName)
58  {
59  if (mSysMan.valid())
60  {
61  mSysMan->RegisterForMessage(messageType, *this, invokableName);
62  }
63  else
64  {
65  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
66  if (!IsRegistered())
67  {
68  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
69  }
70  }
71  }
72 
74  void ActorBase::UnRegisterFromMessage(const std::string& messageType)
75  {
76  if (mSysMan.valid())
77  {
78  mSysMan->UnregisterFromMessage(messageType, *this);
79  }
80  else
81  {
82  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
83  if (!IsRegistered())
84  {
85  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
86  }
87  }
88  }
89 
91  void ActorBase::RegisterForMessagesAboutEntity(const trBase::UniqueId& aboutEntityId, const std::string& invokableName)
92  {
93  if (mSysMan.valid())
94  {
95  mSysMan->RegisterForMessagesAboutEntity(*this, aboutEntityId, invokableName);
96  }
97  else
98  {
99  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
100  if (!IsRegistered())
101  {
102  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
103  }
104  }
105  }
106 
109  {
110  if (mSysMan.valid())
111  {
112  mSysMan->UnregisterFromMessagesAboutEntity(*this, aboutEntityId);
113  }
114  else
115  {
116  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
117  if (!IsRegistered())
118  {
119  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
120  }
121  }
122  }
123 
126  {
129  }
130 
133  {}
134 
137  {
138  if (mSysMan.valid())
139  {
140  return mSysMan->SendMessage(message);
141  }
142  else
143  {
144  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
145  if (!IsRegistered())
146  {
147  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
148  }
149 
150  return false;
151  }
152  }
153 
156  {
157  if (mSysMan.valid())
158  {
159  return mSysMan->SendNetworkMessage(message);
160  }
161  else
162  {
163  LOG_E("The entity " + GetName() + " is missing the internal System Manager reference. ")
164  if (!IsRegistered())
165  {
166  LOG_W("The entity " + GetName() + " is not registered with System Manager. ")
167  }
168 
169  return false;
170  }
171  }
172 
175  {
176  //Make sure we are dealing with an Actor Module, and not another entity
178  {
179  trBase::SmrtPtr<trManager::EntityBase> newActor = &actorModule;
180  mActorModules.push_back(newActor);
181 
182  //Sets the parent of the Actor Module
183  actorModule.SetParent(*this);
184 
185  return true;
186  }
187  else
188  {
189  LOG_E("Attempted to add " + actorModule.GetEntityType().GetName() + " instead of an Actor Module")
190  return false;
191  }
192  }
193 
196  {
197  //Make sure we are dealing with an Actor Module, and not another entity
199  {
200  //Find an Actor Module with the same reference
201  ActorModules::iterator found;
202  for (found = mActorModules.begin(); found != mActorModules.end(); ++found)
203  {
204  if (found->Get() == &actorModule)
205  {
206  //Remove the parent reference from the actor module
207  found->Get()->ForgetParent();
208 
209  //We found the actor module we need, remove it
210  mActorModules.erase(found);
211 
212  return true;
213  }
214  }
215 
216  //If you are here then you wanted to remove something that does not exist
217  LOG_E("Attempted to remove a module which does not exist")
218  return false;
219  }
220  else
221  {
222  LOG_E("Attempted to remove " + actorModule.GetEntityType().GetName() + " instead of an Actor Module")
223  return false;
224  }
225 
226  return false;
227  }
228 
231  {
232  trManager::EntityBase* foundModule = FindActorModule(id);
233  if (foundModule != nullptr)
234  {
235  return RemoveActorModule(*foundModule);
236  }
237  else
238  {
239  return false;
240  }
241  }
242 
245  {
246  for (int index = mActorModules.size() - 1; index >= 0; --index)
247  {
248  if (!RemoveActorModule(*mActorModules[index]))
249  {
250  //If one of the removals fail, stop and return a failure.
251  LOG_E("Error in removing " + mActorModules[index]->GetEntityType().GetName())
252  return false;
253  }
254  }
255 
256  return true;
257  }
258 
261  {
262  //Find an Actor Module with the same reference
263  ActorModules::iterator found;
264  for (found = mActorModules.begin(); found != mActorModules.end(); ++found)
265  {
266  if (found->Get()->GetUUID() == id)
267  {
268  //We found the actor module we need
269  return found->Get();
270  }
271  }
272  return nullptr;
273  }
274 
277  {
279 
280  //ReAttach all modules, if we have any saved in the reattach list.
281  for (unsigned int index = 0; index < mActModReAttachStore.size(); ++index)
282  {
284  }
285 
286  //Clear the reattach list
287  mActModReAttachStore.clear();
288  }
289 
292  {
294 
295  //Make the vector a needed size to speed up assignment.
296  mActModReAttachStore.reserve(mActorModules.size());
297 
298  //Store all the registered modules so we can re-attach them if we re-register with System Manager
299  for (unsigned int index = 0; index < mActorModules.size(); ++index)
300  {
301  mActModReAttachStore.push_back(mActorModules[index]);
302  }
303 
304  //Remove all Modules to make sure we don't have a memory leak if this instance is deleted.
306  }
307 }
virtual void UnregisterFromMessagesAboutEntity(const trBase::UniqueId &aboutEntityId)
Unregisters from messages about a specific actor.
Definition: ActorBase.cpp:108
virtual void OnTick(const trManager::MessageBase &msg)=0
Convenience function that will receive Tick Messages from the System Manager This does not happen aut...
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
virtual bool SendNetworkMessage(const trManager::MessageBase &message)
Send a Network message to an Actor, Actor Module, or a Director.
Definition: ActorBase.cpp:155
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 bool RemoveActorModule(trManager::EntityBase &actorModule)
Removes the given Actor Module from the current Actor.
Definition: ActorBase.cpp:195
const std::string & GetName() const
Inlined because it&#39;s called frequently.
static const EntityType ACTOR_MODULE
Definition: EntityType.h:44
virtual void RegisterForMessagesAboutEntity(const trBase::UniqueId &aboutEntityId, const std::string &invokableName)
Registers for messages about a specific actor.
Definition: ActorBase.cpp:91
virtual void OnRemovedFromSysMan()
Called by the System Manager after removing and Unregistering the EntityBase.
Definition: EntityBase.h:107
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
trUtil::EnumerationPointer< const trManager::EntityType > mEntityType
Definition: EntityBase.h:352
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
Functor< typename trUtil::FunTraits< CallType >::ResultType, typename trUtil::FunTraits< CallType >::TypeListType > MakeFunctor(CallType fun)
Constructor.
Definition: Functor.h:288
virtual void AddInvokable(trManager::Invokable &newInvokable)
Adds an invokable that can receive a message.
Definition: EntityBase.cpp:66
virtual void RegisterForMessage(const std::string &messageType, const std::string &invokableName)
Registers an actor for messages.
Definition: ActorBase.cpp:57
trBase::ObsrvrPtr< trManager::SystemManager > mSysMan
Definition: EntityBase.h:351
ActorModules mActModReAttachStore
Definition: ActorBase.h:254
virtual trManager::EntityBase * FindActorModule(const trBase::UniqueId &id)
Finds and returns the Actor Module cast down to Entity.
Definition: ActorBase.cpp:260
virtual void OnAddedToSysMan() override
Called by the System Manager when EntityBase Registration is complete.
Definition: ActorBase.cpp:276
virtual void OnRemovedFromSysMan() override
Called by the System Manager after removing and Unregistering the EntityBase.
Definition: ActorBase.cpp:291
#define LOG_E(msg)
Log an ERROR message.
Definition: Log.h:165
virtual const bool & IsRegistered()
Returns True if the Instance is registered with a System Manager.
Definition: EntityBase.h:107
virtual void UnRegisterFromMessage(const std::string &messageType)
Unregister an actor that is listening for a given message.
Definition: ActorBase.cpp:74
virtual bool AddActorModule(trManager::EntityBase &actorModule)
Adds an Actor Module to the current Actor.
Definition: ActorBase.cpp:174
virtual bool RemoveAllActorModules()
Definition: ActorBase.cpp:244
virtual void OnTickRemote(const trManager::MessageBase &msg)
Convenience function that will receive a Network Tick Message from the System Manager This does not h...
Definition: ActorBase.cpp:132
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
Definition: ActorBase.h:47
const EntityType & GetEntityType()
Returns the Entity Type, which is usually a Director, Actor, or an Actor module.
Definition: EntityBase.cpp:42
virtual void BuildInvokables()
Builds the default invokables for this class.
Definition: ActorBase.cpp:125
This serves as the base class for the Entity class and removes a circular dependency between Entity a...
Definition: EntityBase.h:46
virtual void ActorModuleTick(const trManager::MessageBase &tickMsg)
Sends the passed in message Tick Message to all attached Actor Modules.
Definition: ActorBase.cpp:47
ActorBase(const std::string &name=CLASS_TYPE)
Holds the class type name for efficient comparisons.
Definition: ActorBase.cpp:35
This is the base class for all the messages in TR.
Definition: MessageBase.h:40
virtual bool SendMessage(const trManager::MessageBase &message)
Send a message to an Actor, Actor Module, or a Director.
Definition: ActorBase.cpp:136
ActorModules mActorModules
Definition: ActorBase.h:251
virtual void SetParent(trManager::EntityBase &parent)
Sets the hierarchal parent of this Entity.
Definition: EntityBase.cpp:236
virtual void OnAddedToSysMan()
Called by the System Manager when EntityBase Registration is complete.
Definition: EntityBase.h:100