MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
actormanager.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef actormanager_cpp
41 #define actormanager_cpp
42 
43 #include "actormanager.h"
44 #include "actor.h"
45 #include "Physics/physicsmanager.h"
46 #include "entresol.h"
47 
48 #include <sstream>
49 #include <algorithm>
50 
51 namespace Mezzanine
52 {
53  ///////////////////////////////////////////////////////////////////////////////
54  // ActorUpdateWorkUnit Methods
55 
57  { }
58 
60  { return *this; }
61 
63  TargetManager(Target) { }
64 
66  { }
67 
68  ///////////////////////////////////////////////////////////////////////////////
69  // Utility
70 
72  {
73  for( ActorManager::ActorIterator ActIt = this->TargetManager->Actors.begin() ; ActIt != this->TargetManager->Actors.end() ; ++ActIt )
74  {
75  (*ActIt)->_Update();
76  }
77  }
78 
79  ///////////////////////////////////////////////////////////////////////////////
80  // ActorManager Methods
81 
83  ActorUpdateWork(NULL),
84  ThreadResources(NULL)
85  {
86  this->ActorUpdateWork = new ActorUpdateWorkUnit(this);
87  }
88 
90  ActorUpdateWork(NULL),
91  ThreadResources(NULL)
92  {
93  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
94 
95  this->ActorUpdateWork = new ActorUpdateWorkUnit(this);
96  }
97 
99  {
100  this->Deinitialize();
101  this->DestroyAllActors();
102 
103  delete this->ActorUpdateWork;
104  }
105 
106  ///////////////////////////////////////////////////////////////////////////////
107  // Prefab Actor Type Creation
108 
109  ///////////////////////////////////////////////////////////////////////////////
110  // Actor Management
111 
112  Actor* ActorManager::CreateActor(const String& TypeName, const String& InstanceName, const NameValuePairMap& Params)
113  {
114  FactoryIterator ActFactIt = this->ActorFactories.find( TypeName );
115  if( ActFactIt != this->ActorFactories.end() ) {
116  Actor* Ret = (*ActFactIt).second->CreateActor( InstanceName, this->ParentWorld, Params );
117  this->Actors.push_back( Ret );
118  return Ret;
119  }else{
120  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create an Actor of unknown type.");
121  }
122  }
123 
125  {
126  FactoryIterator ActFactIt = this->ActorFactories.find( SelfRoot.Name() );
127  if( ActFactIt != this->ActorFactories.end() ) {
128  Actor* Ret = (*ActFactIt).second->CreateActor( SelfRoot, this->ParentWorld );
129  this->Actors.push_back( Ret );
130  return Ret;
131  }else{
132  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a Actor of unknown type.");
133  }
134  }
135 
136  Actor* ActorManager::GetActor(const Whole Index) const
137  {
138  return this->Actors.at(Index);
139  }
140 
141  Actor* ActorManager::GetActor(const String& Name) const
142  {
143  for( ConstActorIterator ActIt = this->Actors.begin() ; ActIt != this->Actors.end() ; ++ActIt )
144  {
145  if( (*ActIt)->GetName() == Name )
146  return (*ActIt);
147  }
148  return NULL;
149  }
150 
152  {
153  return this->Actors.size();
154  }
155 
157  {
158  ActorIterator ActIt = ( Index < this->GetNumActors() ? this->Actors.begin() + Index : this->Actors.end() );
159  if( ActIt != this->Actors.end() )
160  {
161  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
162  if( ActFactIt != this->ActorFactories.end() ) {
163  (*ActFactIt).second->DestroyActor( (*ActIt) );
164  }else{
165  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
166  }
167 
168  this->Actors.erase(ActIt);
169  }
170  }
171 
172  void ActorManager::DestroyActor(Actor* ToBeDestroyed)
173  {
174  ActorIterator ActIt = std::find( this->Actors.begin(), this->Actors.end(), ToBeDestroyed );
175  if( ActIt != this->Actors.end() )
176  {
177  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
178  if( ActFactIt != this->ActorFactories.end() ) {
179  (*ActFactIt).second->DestroyActor( (*ActIt) );
180  }else{
181  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
182  }
183 
184  this->Actors.erase(ActIt);
185  }
186  }
187 
189  {
190  for( ActorIterator ActIt = this->Actors.begin() ; ActIt != this->Actors.end() ; ++ActIt )
191  {
192  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
193  if( ActFactIt != this->ActorFactories.end() ) {
194  (*ActFactIt).second->DestroyActor( (*ActIt) );
195  }else{
196  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
197  }
198  }
199  this->Actors.clear();
200  }
201 
202  ///////////////////////////////////////////////////////////////////////////////
203  // ActorFactory Management
204 
206  {
207  this->ActorFactories.insert(std::pair<String,ActorFactory*>(ToBeAdded->GetTypeName(),ToBeAdded));
208  }
209 
211  {
212  this->RemoveActorFactory(ToBeRemoved->GetTypeName());
213  }
214 
216  {
217  FactoryIterator ActFactIt = this->ActorFactories.find(ImplName);
218  if( ActFactIt != this->ActorFactories.end() )
219  { this->ActorFactories.erase(ActFactIt); }
220  }
221 
223  {
224  this->DestroyActorFactory(ToBeDestroyed->GetTypeName());
225  }
226 
228  {
229  FactoryIterator ActFactIt = this->ActorFactories.find(ImplName);
230  if( ActFactIt != this->ActorFactories.end() ) {
231  delete ActFactIt->second;
232  this->ActorFactories.erase(ActFactIt);
233  }
234  }
235 
237  {
238  for( FactoryIterator ActFactIt = this->ActorFactories.begin() ; ActFactIt != this->ActorFactories.end() ; ++ActFactIt )
239  { delete (*ActFactIt).second; }
240  this->ActorFactories.clear();
241  }
242 
243  ///////////////////////////////////////////////////////////////////////////////
244  // Utility
245 
247  {
248  // Do nothing currently
249  }
250 
252  {
253  if( !this->Initialized )
254  {
255  //WorldManager::Initialize();
256 
257  this->TheEntresol->GetScheduler().AddWorkUnitMain( this->ActorUpdateWork, "ActorUpdateWork" );
259  if( PhysicsMan ) {
260  this->ActorUpdateWork->AddDependency( PhysicsMan->GetSimulationWork() );
261  }
262 
263  this->Initialized = true;
264  }
265  }
266 
268  {
269  if( this->Initialized )
270  {
273 
274  this->Initialized = false;
275  }
276  }
277 
279  { return this->ActorUpdateWork; }
280 
281  ///////////////////////////////////////////////////////////////////////////////
282  // Type Identifier Methods
283 
285  { return ManagerBase::MT_ActorManager; }
286 
288  { return "DefaultActorManager"; }
289 
290  ///////////////////////////////////////////////////////////////////////////////
291  // DefaultActorManagerFactory Methods
292 
294  { }
295 
297  { }
298 
300  { return "DefaultActorManager"; }
301 
303  { return new ActorManager(); }
304 
306  { return new ActorManager(XMLNode); }
307 
309  { delete ToBeDestroyed; }
310 }//Mezzanine
311 
312 #endif