MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
debrismanager.cpp
Go to the documentation of this file.
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 _debrismanager_cpp
41 #define _debrismanager_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the manager that manages debris objects in a world.
45 
46 #include "debrismanager.h"
47 #include "rigiddebris.h"
48 #include "softdebris.h"
49 
50 #include "Physics/physicsmanager.h"
51 #include "entresol.h"
52 
53 namespace Mezzanine
54 {
55  ///////////////////////////////////////////////////////////////////////////////
56  // DebrisUpdateWorkUnit Methods
57 
59  { }
60 
62  { return *this; }
63 
65  TargetManager(Target) { }
66 
68  { }
69 
70  ///////////////////////////////////////////////////////////////////////////////
71  // Utility
72 
74  {
75  for( DebrisManager::DebrisIterator DebIt = this->TargetManager->Debriss.begin() ; DebIt != this->TargetManager->Debriss.end() ; ++DebIt )
76  {
77  (*DebIt)->_Update();
78  }
79  }
80 
81  ///////////////////////////////////////////////////////////////////////////////
82  // DebrisManager Methods
83 
85  {
86  this->AddDebrisFactory( new RigidDebrisFactory() );
87  this->AddDebrisFactory( new SoftDebrisFactory() );
88 
89  this->DebrisUpdateWork = new DebrisUpdateWorkUnit(this);
90  }
91 
93  {
94  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
95 
96  this->AddDebrisFactory( new RigidDebrisFactory() );
97  this->AddDebrisFactory( new SoftDebrisFactory() );
98 
99  this->DebrisUpdateWork = new DebrisUpdateWorkUnit(this);
100  }
101 
103  {
104  this->Deinitialize();
105  this->DestroyAllDebris();
107 
108  delete this->DebrisUpdateWork;
109  }
110 
111  ///////////////////////////////////////////////////////////////////////////////
112  // Prefab Debris Type Creation
113 
115  {
117  if( DebFactIt != this->DebrisFactories.end() ) {
118  RigidDebris* Ret = static_cast<RigidDebrisFactory*>( (*DebFactIt).second )->CreateRigidDebris( Name, Mass, this->ParentWorld );
119  this->Debriss.push_back( Ret );
120  return Ret;
121  }else{
122  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a RigidDebris without it's factory registered.");
123  }
124  }
125 
127  {
129  if( DebFactIt != this->DebrisFactories.end() ) {
130  RigidDebris* Ret = static_cast<RigidDebrisFactory*>( (*DebFactIt).second )->CreateRigidDebris( SelfRoot, this->ParentWorld );
131  this->Debriss.push_back( Ret );
132  return Ret;
133  }else{
134  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a RigidDebris without it's factory registered.");
135  }
136  }
137 
139  {
141  if( DebFactIt != this->DebrisFactories.end() ) {
142  SoftDebris* Ret = static_cast<SoftDebrisFactory*>( (*DebFactIt).second )->CreateSoftDebris( Name, Mass, this->ParentWorld );
143  this->Debriss.push_back( Ret );
144  return Ret;
145  }else{
146  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a SoftDebris without it's factory registered.");
147  }
148  }
149 
151  {
153  if( DebFactIt != this->DebrisFactories.end() ) {
154  SoftDebris* Ret = static_cast<SoftDebrisFactory*>( (*DebFactIt).second )->CreateSoftDebris( SelfRoot, this->ParentWorld );
155  this->Debriss.push_back( Ret );
156  return Ret;
157  }else{
158  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a SoftDebris without it's factory registered.");
159  }
160  }
161 
162  ///////////////////////////////////////////////////////////////////////////////
163  // Debris Management
164 
165  Debris* DebrisManager::CreateDebris(const String& TypeName, const String& InstanceName, const NameValuePairMap& Params)
166  {
167  FactoryIterator DebFactIt = this->DebrisFactories.find( TypeName );
168  if( DebFactIt != this->DebrisFactories.end() ) {
169  Debris* Ret = (*DebFactIt).second->CreateDebris( InstanceName, this->ParentWorld, Params );
170  this->Debriss.push_back( Ret );
171  return Ret;
172  }else{
173  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create an Debris of unknown type.");
174  }
175  }
176 
178  {
179  FactoryIterator DebFactIt = this->DebrisFactories.find( SelfRoot.Name() );
180  if( DebFactIt != this->DebrisFactories.end() ) {
181  Debris* Ret = (*DebFactIt).second->CreateDebris( SelfRoot, this->ParentWorld );
182  this->Debriss.push_back( Ret );
183  return Ret;
184  }else{
185  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a Debris of unknown type.");
186  }
187  }
188 
190  {
191  return this->Debriss.at(Index);
192  }
193 
195  {
196  for( ConstDebrisIterator DebIt = this->Debriss.begin() ; DebIt != this->Debriss.end() ; ++DebIt )
197  {
198  if( (*DebIt)->GetName() == Name )
199  return (*DebIt);
200  }
201  return NULL;
202  }
203 
205  {
206  return this->Debriss.size();
207  }
208 
210  {
211  DebrisIterator DebIt = ( Index < this->GetNumDebris() ? this->Debriss.begin() + Index : this->Debriss.end() );
212  if( DebIt != this->Debriss.end() )
213  {
214  FactoryIterator DebFactIt = this->DebrisFactories.find( (*DebIt)->GetDerivedSerializableName() );
215  if( DebFactIt != this->DebrisFactories.end() ) {
216  (*DebFactIt).second->DestroyDebris( (*DebIt) );
217  }else{
218  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Debris of unknown type.");
219  }
220 
221  this->Debriss.erase(DebIt);
222  }
223  }
224 
226  {
227  DebrisIterator DebIt = std::find( this->Debriss.begin(), this->Debriss.end(), ToBeDestroyed );
228  if( DebIt != this->Debriss.end() )
229  {
230  FactoryIterator DebFactIt = this->DebrisFactories.find( (*DebIt)->GetDerivedSerializableName() );
231  if( DebFactIt != this->DebrisFactories.end() ) {
232  (*DebFactIt).second->DestroyDebris( (*DebIt) );
233  }else{
234  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Debris of unknown type.");
235  }
236 
237  this->Debriss.erase(DebIt);
238  }
239  }
240 
242  {
243  for( DebrisIterator DebIt = this->Debriss.begin() ; DebIt != this->Debriss.end() ; ++DebIt )
244  {
245  FactoryIterator DebFactIt = this->DebrisFactories.find( (*DebIt)->GetDerivedSerializableName() );
246  if( DebFactIt != this->DebrisFactories.end() ) {
247  (*DebFactIt).second->DestroyDebris( (*DebIt) );
248  }else{
249  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy a Debris of unknown type.");
250  }
251  }
252  this->Debriss.clear();
253  }
254 
255  ///////////////////////////////////////////////////////////////////////////////
256  // DebrisFactory Management
257 
259  {
260  this->DebrisFactories.insert(std::pair<String,DebrisFactory*>(ToBeAdded->GetTypeName(),ToBeAdded));
261  }
262 
264  {
265  this->RemoveDebrisFactory(ToBeRemoved->GetTypeName());
266  }
267 
269  {
270  FactoryIterator DebFactIt = this->DebrisFactories.find(ImplName);
271  if( DebFactIt != this->DebrisFactories.end() )
272  { this->DebrisFactories.erase(DebFactIt); }
273  }
274 
276  {
277  this->DestroyDebrisFactory(ToBeDestroyed->GetTypeName());
278  }
279 
281  {
282  FactoryIterator DebFactIt = this->DebrisFactories.find(ImplName);
283  if( DebFactIt != this->DebrisFactories.end() ) {
284  delete DebFactIt->second;
285  this->DebrisFactories.erase(DebFactIt);
286  }
287  }
288 
290  {
291  for( FactoryIterator DebFactIt = this->DebrisFactories.begin() ; DebFactIt != this->DebrisFactories.end() ; ++DebFactIt )
292  { delete (*DebFactIt).second; }
293  this->DebrisFactories.clear();
294  }
295 
296  ///////////////////////////////////////////////////////////////////////////////
297  // Utility
298 
300  {
301  // Do nothing currently
302  }
303 
305  {
306  if( !this->Initialized )
307  {
308  //WorldManager::Initialize();
309 
310  this->TheEntresol->GetScheduler().AddWorkUnitMain( this->DebrisUpdateWork, "DebrisUpdateWork" );
312  if( PhysicsMan ) {
313  this->DebrisUpdateWork->AddDependency( PhysicsMan->GetSimulationWork() );
314  }
315 
316  this->Initialized = true;
317  }
318  }
319 
321  {
322  if( this->Initialized )
323  {
326 
327  this->Initialized = false;
328  }
329  }
330 
332  { return this->DebrisUpdateWork; }
333 
334  ///////////////////////////////////////////////////////////////////////////////
335  // Type Identifier Methods
336 
338  { return ManagerBase::MT_DebrisManager; }
339 
341  { return "DefaultDebrisManager"; }
342 
343  ///////////////////////////////////////////////////////////////////////////////
344  // DefaultDebrisManagerFactory Methods
345 
347  { }
348 
350  { }
351 
353  { return "DefaultDebrisManager"; }
354 
356  { return new DebrisManager(); }
357 
359  { return new DebrisManager(XMLNode); }
360 
362  { delete ToBeDestroyed; }
363 }//Mezzanine
364 
365 #endif