MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
areaeffectmanager.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 _areaeffectmanager_cpp
41 #define _areaeffectmanager_cpp
42 
43 #include "areaeffectmanager.h"
44 
45 #include "fieldofforce.h"
46 #include "gravityfield.h"
47 #include "gravitywell.h"
48 
49 #include "Physics/physicsmanager.h"
50 
51 #include "actormanager.h"
52 #include "entresol.h"
53 
54 #include <sstream>
55 #include <algorithm>
56 
57 namespace Mezzanine
58 {
59  ///////////////////////////////////////////////////////////////////////////////
60  // AreaEffectUpdateWorkUnit Methods
61 
63  { }
64 
66  { return *this; }
67 
69  TargetManager(Target) { }
70 
72  { }
73 
74  ///////////////////////////////////////////////////////////////////////////////
75  // Utility
76 
78  {
79  for( AreaEffectManager::AreaEffectIterator AE = this->TargetManager->AreaEffects.begin() ; AE != this->TargetManager->AreaEffects.end() ; ++AE )
80  {
81  (*AE)->_Update();
82  (*AE)->ApplyEffect();
83  }
84  }
85 
86  ///////////////////////////////////////////////////////////////////////////////
87  // AreaEffectManager Methods
88 
90  AreaEffectUpdateWork(NULL),
91  ThreadResources(NULL)
92  {
96 
98  }
99 
101  AreaEffectUpdateWork(NULL),
102  ThreadResources(NULL)
103  {
104  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
105 
109 
111  }
112 
114  {
115  this->Deinitialize();
116  this->DestroyAllAreaEffects();
118 
119  delete this->AreaEffectUpdateWork;
120  }
121 
122  ///////////////////////////////////////////////////////////////////////////////
123  // Prefab AreaEffect Type Creation
124 
126  {
128  if( AEFactIt != this->AreaEffectFactories.end() ) {
129  FieldOfForce* Ret = static_cast<FieldOfForceFactory*>( (*AEFactIt).second )->CreateFieldOfForce( Name, this->ParentWorld );
130  this->AreaEffects.push_back( Ret );
131  return Ret;
132  }else{
133  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a FieldOfForce AreaEffect without it's factory registered.");
134  }
135  }
136 
138  {
140  if( AEFactIt != this->AreaEffectFactories.end() ) {
141  FieldOfForce* Ret = static_cast<FieldOfForceFactory*>( (*AEFactIt).second )->CreateFieldOfForce( SelfRoot, this->ParentWorld );
142  this->AreaEffects.push_back( Ret );
143  return Ret;
144  }else{
145  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a FieldOfForce AreaEffect without it's factory registered.");
146  }
147  }
148 
150  {
152  if( AEFactIt != this->AreaEffectFactories.end() ) {
153  GravityField* Ret = static_cast<GravityFieldFactory*>( (*AEFactIt).second )->CreateGravityField( Name, this->ParentWorld );
154  this->AreaEffects.push_back( Ret );
155  return Ret;
156  }else{
157  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a GravityField AreaEffect without it's factory registered.");
158  }
159  }
160 
162  {
164  if( AEFactIt != this->AreaEffectFactories.end() ) {
165  GravityField* Ret = static_cast<GravityFieldFactory*>( (*AEFactIt).second )->CreateGravityField( SelfRoot, this->ParentWorld );
166  this->AreaEffects.push_back( Ret );
167  return Ret;
168  }else{
169  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a GravityField AreaEffect without it's factory registered.");
170  }
171  }
172 
174  {
176  if( AEFactIt != this->AreaEffectFactories.end() ) {
177  GravityWell* Ret = static_cast<GravityWellFactory*>( (*AEFactIt).second )->CreateGravityWell( Name, this->ParentWorld );
178  this->AreaEffects.push_back( Ret );
179  return Ret;
180  }else{
181  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a GravityWell AreaEffect without it's factory registered.");
182  }
183  }
184 
186  {
188  if( AEFactIt != this->AreaEffectFactories.end() ) {
189  GravityWell* Ret = static_cast<GravityWellFactory*>( (*AEFactIt).second )->CreateGravityWell( SelfRoot, this->ParentWorld );
190  this->AreaEffects.push_back( Ret );
191  return Ret;
192  }else{
193  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create a GravityWell AreaEffect without it's factory registered.");
194  }
195  }
196 
197  ///////////////////////////////////////////////////////////////////////////////
198  // AreaEffect Management
199 
200  AreaEffect* AreaEffectManager::CreateAreaEffect(const String& TypeName, const String& InstanceName, const NameValuePairMap& Params)
201  {
202  FactoryIterator AEFactIt = this->AreaEffectFactories.find( TypeName );
203  if( AEFactIt != this->AreaEffectFactories.end() ) {
204  AreaEffect* Ret = (*AEFactIt).second->CreateAreaEffect( InstanceName, this->ParentWorld, Params );
205  this->AreaEffects.push_back( Ret );
206  return Ret;
207  }else{
208  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create an AreaEffect of unknown type.");
209  }
210  }
211 
213  {
214  FactoryIterator AEFactIt = this->AreaEffectFactories.find( SelfRoot.Name() );
215  if( AEFactIt != this->AreaEffectFactories.end() ) {
216  AreaEffect* Ret = (*AEFactIt).second->CreateAreaEffect( SelfRoot, this->ParentWorld );
217  this->AreaEffects.push_back( Ret );
218  return Ret;
219  }else{
220  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to create an AreaEffect of unknown type.");
221  }
222  }
223 
225  {
226  return this->AreaEffects.at(Index);
227  }
228 
230  {
231  for( ConstAreaEffectIterator it = this->AreaEffects.begin() ; it != this->AreaEffects.end() ; ++it )
232  {
233  if(Name == (*it)->GetName())
234  return (*it);
235  }
236  return NULL;
237  }
238 
240  {
241  return this->AreaEffects.size();
242  }
243 
245  {
246  AreaEffectIterator AEIt = ( Index < this->GetNumAreaEffects() ? this->AreaEffects.begin() + Index : this->AreaEffects.end() );
247  if( AEIt != this->AreaEffects.end() )
248  {
249  FactoryIterator AEFactIt = this->AreaEffectFactories.find( (*AEIt)->GetDerivedSerializableName() );
250  if( AEFactIt != this->AreaEffectFactories.end() ) {
251  (*AEFactIt).second->DestroyAreaEffect( (*AEIt) );
252  }else{
253  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy an AreaEffect of unknown type.");
254  }
255 
256  this->AreaEffects.erase(AEIt);
257  }
258  }
259 
261  {
262  AreaEffectIterator AEIt = std::find( this->AreaEffects.begin(), this->AreaEffects.end(), ToBeDestroyed );
263  if( AEIt != this->AreaEffects.end() )
264  {
265  FactoryIterator AEFactIt = this->AreaEffectFactories.find( (*AEIt)->GetDerivedSerializableName() );
266  if( AEFactIt != this->AreaEffectFactories.end() ) {
267  (*AEFactIt).second->DestroyAreaEffect( (*AEIt) );
268  }else{
269  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy an AreaEffect of unknown type.");
270  }
271 
272  this->AreaEffects.erase(AEIt);
273  }
274  }
275 
277  {
278  for( AreaEffectIterator AEIt = this->AreaEffects.begin() ; AEIt != this->AreaEffects.end() ; ++AEIt )
279  {
280  FactoryIterator AEFactIt = this->AreaEffectFactories.find( (*AEIt)->GetDerivedSerializableName() );
281  if( AEFactIt != this->AreaEffectFactories.end() ) {
282  (*AEFactIt).second->DestroyAreaEffect( (*AEIt) );
283  }else{
284  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Attempting to destroy an AreaEffect of unknown type.");
285  }
286  }
287  this->AreaEffects.clear();
288  }
289 
290  ///////////////////////////////////////////////////////////////////////////////
291  // AreaEffectFactory Management
292 
294  {
295  this->AreaEffectFactories.insert(std::pair<String,AreaEffectFactory*>(ToBeAdded->GetTypeName(),ToBeAdded));
296  }
297 
299  {
300  this->RemoveAreaEffectFactory(ToBeRemoved->GetTypeName());
301  }
302 
304  {
305  FactoryIterator AEFactIt = this->AreaEffectFactories.find(ImplName);
306  if( AEFactIt != this->AreaEffectFactories.end() )
307  { this->AreaEffectFactories.erase(AEFactIt); }
308  }
309 
311  {
312  this->DestroyAreaEffectFactory(ToBeDestroyed->GetTypeName());
313  }
314 
316  {
317  FactoryIterator AEFactIt = this->AreaEffectFactories.find(ImplName);
318  if( AEFactIt != this->AreaEffectFactories.end() ) {
319  delete AEFactIt->second;
320  this->AreaEffectFactories.erase(AEFactIt);
321  }
322  }
323 
325  {
326  for( FactoryIterator AEFactIt = this->AreaEffectFactories.begin() ; AEFactIt != this->AreaEffectFactories.end() ; ++AEFactIt )
327  { delete (*AEFactIt).second; }
328  this->AreaEffectFactories.clear();
329  }
330 
331  ///////////////////////////////////////////////////////////////////////////////
332  // Utility
333 
335  {
336  // Do nothing currently
337  }
338 
340  {
341  for( AreaEffectManager::AreaEffectIterator AE = this->AreaEffects.begin() ; AE != this->AreaEffects.end() ; ++AE )
342  {
343  (*AE)->_Update();
344  (*AE)->ApplyEffect();
345  }
346  }
347 
349  {
350  if( !this->Initialized )
351  {
352  //WorldManager::Initialize();
353 
354  this->TheEntresol->GetScheduler().AddWorkUnitMain( this->AreaEffectUpdateWork, "AreaEffectUpdateWork" );
355 
357  if( PhysicsMan ) {
358  this->AreaEffectUpdateWork->AddDependency( PhysicsMan->GetSimulationWork() );
360  }
361 
363  if( ActorMan ) {
365  }
366 
367  this->Initialized = true;
368  }
369  }
370 
372  {
373  if( this->Initialized )
374  {
377 
378  this->Initialized = false;
379  }
380  }
381 
383  { return this->AreaEffectUpdateWork; }
384 
385  ///////////////////////////////////////////////////////////////////////////////
386  // Type Identifier Methods
387 
389  { return ManagerBase::MT_AreaEffectManager; }
390 
392  { return "DefaultAreaEffectManager"; }
393 
394  ///////////////////////////////////////////////////////////////////////////////
395  // DefaultAreaEffectManagerFactory Methods
396 
398  { }
399 
401  { }
402 
404  { return "DefaultAreaEffectManager"; }
405 
407  { return new AreaEffectManager(); }
408 
410  { return new AreaEffectManager(XMLNode); }
411 
413  { delete ToBeDestroyed; }
414 }//Mezzanine
415 
416 #endif