MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
softdebris.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 _softdebris_cpp
41 #define _softdebris_cpp
42 
43 /// @file
44 /// @brief This file contains the implementation for the debris class that will compress and deform.
45 
46 #include "softdebris.h"
47 
48 #include "Graphics/scenemanager.h"
49 #include "Graphics/entityproxy.h"
50 
51 #include "Physics/physicsmanager.h"
52 #include "Physics/softproxy.h"
53 
54 #include "entresol.h"
55 #include "exception.h"
56 #include "stringtool.h"
57 #include "serialization.h"
58 
59 namespace Mezzanine
60 {
61  ///////////////////////////////////////////////////////////////////////////////
62  // RigidDebris Methods
63 
65  Debris(TheWorld),
66  EntProx(NULL),
67  SofProx(NULL)
68  { }
69 
70  SoftDebris::SoftDebris(const String& Name, const Real Mass, World* TheWorld) :
71  Debris(Name,TheWorld),
72  EntProx(NULL),
73  SofProx(NULL)
74  { this->CreateSoftDebris(Mass); }
75 
76  SoftDebris::SoftDebris(const XML::Node& SelfRoot, World* TheWorld) :
77  Debris(TheWorld),
78  EntProx(NULL),
79  SofProx(NULL)
80  { this->ProtoDeSerialize(SelfRoot); }
81 
83  { this->DestroySoftDebris(); }
84 
86  {
88  if( SceneMan ) {
89  this->EntProx = SceneMan->CreateEntityProxy();
90  this->EntProx->_Bind( this );
91  }
92 
94  if( PhysMan ) {
95  this->SofProx = PhysMan->CreateSoftProxy(Mass);
96  this->SofProx->_Bind( this );
97  }
98  }
99 
101  {
102  this->RemoveFromWorld();
103  if( this->EntProx ) {
105  if( SceneMan ) {
106  SceneMan->DestroyProxy( this->EntProx );
107  this->EntProx = NULL;
108  }
109  }
110 
111  if( this->SofProx ) {
113  if( PhysMan ) {
114  PhysMan->DestroyProxy( this->SofProx );
115  this->SofProx = NULL;
116  }
117  }
118  }
119 
120  ///////////////////////////////////////////////////////////////////////////////
121  // Utility and Configuration
122 
123  WorldObjectType SoftDebris::GetType() const
124  { return Mezzanine::WO_DebrisSoft; }
125 
127  { return this->EntProx; }
128 
130  { return this->SofProx; }
131 
132  Boolean SoftDebris::IsInWorld() const
133  { return this->SofProx->IsInWorld(); }
134 
135  Boolean SoftDebris::IsStatic() const
136  { return this->SofProx->IsStatic(); }
137 
138  Boolean SoftDebris::IsKinematic() const
139  { return this->SofProx->IsKinematic(); }
140 
142  {
143  Proxies.push_back( this->EntProx );
144  Proxies.push_back( this->SofProx );
145  }
146 
147  void SoftDebris::GetProxies(const UInt32 Types, ProxyContainer& Proxies)
148  {
149  if( Types & Mezzanine::PT_Graphics_EntityProxy ) {
150  Proxies.push_back( this->EntProx );
151  }
152  if( Types & Mezzanine::PT_Physics_SoftProxy ) {
153  Proxies.push_back( this->SofProx );
154  }
155  }
156 
157  ///////////////////////////////////////////////////////////////////////////////
158  // Working with the World
159 
161  {
162  if( this->EntProx )
163  this->EntProx->AddToWorld();
164 
165  if( this->SofProx )
166  this->SofProx->AddToWorld();
167  }
168 
170  {
171  if( this->EntProx )
172  this->EntProx->RemoveFromWorld();
173 
174  if( this->SofProx )
175  this->SofProx->RemoveFromWorld();
176  }
177 
178  ///////////////////////////////////////////////////////////////////////////////
179  // Transform Methods
180 
182  {
183  this->SofProx->SetLocation(Loc);
184  this->EntProx->SetLocation(Loc);
185  }
186 
187  void SoftDebris::SetLocation(const Real X, const Real Y, const Real Z)
188  {
189  this->SofProx->SetLocation(X,Y,Z);
190  this->EntProx->SetLocation(X,Y,Z);
191  }
192 
194  {
195  return this->SofProx->GetLocation();
196  }
197 
199  {
200  this->SofProx->SetOrientation(Ori);
201  this->EntProx->SetOrientation(Ori);
202  }
203 
204  void SoftDebris::SetOrientation(const Real X, const Real Y, const Real Z, const Real W)
205  {
206  this->SofProx->SetOrientation(X,Y,Z,W);
207  this->EntProx->SetOrientation(X,Y,Z,W);
208  }
209 
211  {
212  return this->SofProx->GetOrientation();
213  }
214 
216  {
217  this->SofProx->SetScale(Sc);
218  this->EntProx->SetScale(Sc);
219  }
220 
221  void SoftDebris::SetScale(const Real X, const Real Y, const Real Z)
222  {
223  this->SofProx->SetScale(X,Y,Z);
224  this->EntProx->SetScale(X,Y,Z);
225  }
226 
228  {
229  return this->SofProx->GetScale();
230  }
231 
232  void SoftDebris::Translate(const Vector3& Trans)
233  {
234  this->SofProx->Translate(Trans);
235  this->EntProx->Translate(Trans);
236  }
237 
238  void SoftDebris::Translate(const Real X, const Real Y, const Real Z)
239  {
240  this->SofProx->Translate(X,Y,Z);
241  this->EntProx->Translate(X,Y,Z);
242  }
243 
244  void SoftDebris::Yaw(const Real Angle)
245  {
246  this->SofProx->Yaw(Angle);
247  this->EntProx->Yaw(Angle);
248  }
249 
250  void SoftDebris::Pitch(const Real Angle)
251  {
252  this->SofProx->Pitch(Angle);
253  this->EntProx->Pitch(Angle);
254  }
255 
256  void SoftDebris::Roll(const Real Angle)
257  {
258  this->SofProx->Roll(Angle);
259  this->EntProx->Roll(Angle);
260  }
261 
262  void SoftDebris::Rotate(const Vector3& Axis, const Real Angle)
263  {
264  this->SofProx->Rotate(Axis,Angle);
265  this->EntProx->Rotate(Axis,Angle);
266  }
267 
268  void SoftDebris::Rotate(const Quaternion& Rotation)
269  {
270  this->SofProx->Rotate(Rotation);
271  this->EntProx->Rotate(Rotation);
272  }
273 
274  void SoftDebris::Scale(const Vector3& Scale)
275  {
276  this->SofProx->Scale(Scale);
277  this->EntProx->Scale(Scale);
278  }
279 
280  void SoftDebris::Scale(const Real X, const Real Y, const Real Z)
281  {
282  this->SofProx->Scale(X,Y,Z);
283  this->EntProx->Scale(X,Y,Z);
284  }
285 
286 
287  ///////////////////////////////////////////////////////////////////////////////
288  // Serialization
289 
291  {
292  this->Debris::ProtoSerializeProperties(SelfRoot);
293  }
294 
296  {
297  // No base implementations to call
298  XML::Node ProxiesNode = SelfRoot.AppendChild( SoftDebris::GetSerializableName() + "Proxies" );
299 
300  if( ProxiesNode.AppendAttribute("Version").SetValue("1") )
301  {
302  XML::Node EntProxNode = ProxiesNode.AppendChild("EntProx");
303  this->EntProx->ProtoSerialize( EntProxNode );
304  XML::Node SofProxNode = ProxiesNode.AppendChild("SofProx");
305  this->SofProx->ProtoSerialize( SofProxNode );
306 
307  return;
308  }else{
309  SerializeError("Create XML Attribute Values",SoftDebris::GetSerializableName() + "Proxies",true);
310  }
311  }
312 
314  {
315  this->Debris::ProtoDeSerializeProperties(SelfRoot);
316  }
317 
319  {
320  this->DestroySoftDebris();
321  // No base implementations to call
322  //XML::Attribute CurrAttrib;
323  XML::Node ProxiesNode = SelfRoot.GetChild( SoftDebris::GetSerializableName() + "Proxies" );
324 
325  if( !ProxiesNode.Empty() ) {
326  if(ProxiesNode.GetAttribute("Version").AsInt() == 1) {
327  /// @todo I don't think an exception is appropriate for the failure of the worldmanager validity checks,
328  /// however a warning should be written to the log if that happens. This should be updated to do that once
329  /// logging refactors are done.
330 
331  XML::Node EntProxNode = ProxiesNode.GetChild("EntProx").GetFirstChild();
332  if( !EntProxNode.Empty() ) {
334  if( SceneMan ) {
335  this->EntProx = SceneMan->CreateEntityProxy( EntProxNode );
336  this->EntProx->_Bind( this );
337  }
338  }
339 
340  XML::Node SofProxNode = ProxiesNode.GetChild("SofProx").GetFirstChild();
341  if( !SofProxNode.Empty() ) {
343  if( PhysMan ) {
344  this->SofProx = PhysMan->CreateSoftProxy(SofProxNode);
345  this->SofProx->_Bind( this );
346  }
347  }
348  }else{
349  MEZZ_EXCEPTION(Exception::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (SoftDebris::GetSerializableName() + "Proxies" ) + ": Not Version 1.");
350  }
351  }else{
352  MEZZ_EXCEPTION(Exception::II_IDENTITY_NOT_FOUND_EXCEPTION,SoftDebris::GetSerializableName() + "Proxies" + " was not found in the provided XML node, which was expected.");
353  }
354  }
355 
357  { return SoftDebris::GetSerializableName(); }
358 
360  { return "SoftDebris"; }
361 
362  ///////////////////////////////////////////////////////////////////////////////
363  // Internal Methods
364 
366  {
367  // Do nothing
368  }
369 
371  {
372  if( ToBeDestroyed == NULL )
373  return;
374 
375  if( this->EntProx == ToBeDestroyed ) {
376  this->EntProx = NULL;
377  }
378 
379  if( this->SofProx == ToBeDestroyed ) {
380  this->SofProx = NULL;
381  }
382  }
383 
384  ///////////////////////////////////////////////////////////////////////////////
385  // RigidDebrisFactory Methods
386 
388  { }
389 
391  { }
392 
394  { return SoftDebris::GetSerializableName(); }
395 
396  SoftDebris* SoftDebrisFactory::CreateSoftDebris(const String& Name, const Real Mass, World* TheWorld)
397  { return new SoftDebris(Name,Mass,TheWorld); }
398 
400  { return static_cast<SoftDebris*>( this->CreateDebris(XMLNode,TheWorld) ); }
401 
402  Debris* SoftDebrisFactory::CreateDebris(const String& Name, World* TheWorld, const NameValuePairMap& Params)
403  {
404  Real Mass = 0;
405  NameValuePairMap::const_iterator ParamIt = Params.find( "Mass" );
406  if( ParamIt != Params.end() )
407  Mass = StringTools::ConvertToReal( (*ParamIt).second );
408 
409  return new SoftDebris(Name,Mass,TheWorld);
410  }
411 
413  { return new SoftDebris(XMLNode,TheWorld); }
414 
416  { delete ToBeDestroyed; }
417 }//Mezzanine
418 
419 #endif