Spinning Topp Logo BlackTopp Studios
inc
gravityfield.cpp
1 // © Copyright 2010 - 2016 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 _gravityfield_cpp
41 #define _gravityfield_cpp
42 
43 #include "gravityfield.h"
44 
45 #include "Physics/rigidproxy.h"
46 #include "Physics/ghostproxy.h"
47 #include "Physics/physicsmanager.h"
48 
49 #include "entresol.h"
50 #include "world.h"
51 #include "serialization.h"
52 #include "exception.h"
53 
54 namespace Mezzanine
55 {
57  AreaEffect(TheWorld)
58  { }
59 
60  GravityField::GravityField(const String& Name, World* TheWorld) :
61  AreaEffect(Name,TheWorld)
62  { }
63 
65  AreaEffect(Name,Shape,TheWorld)
66  { }
67 
68  GravityField::GravityField(const XML::Node& SelfRoot, World* TheWorld) :
69  AreaEffect(TheWorld)
70  { this->ProtoDeSerialize(SelfRoot); }
71 
73  { }
74 
75  ///////////////////////////////////////////////////////////////////////////////
76  // Utility
77 
79  { return Mezzanine::WO_AreaEffectGravityField; }
80 
82  {
83  for( ObjectIterator AddedIt = this->AddedObjects.begin() ; AddedIt != this->AddedObjects.end() ; ++AddedIt )
84  {
85  ProxyContainer RigidProxies;
86  (*AddedIt)->GetProxies(Mezzanine::PT_Physics_RigidProxy,RigidProxies);
87  for( ProxyIterator ProxIt = RigidProxies.begin() ; ProxIt != RigidProxies.end() ; ++ProxIt )
88  {
89  Physics::RigidProxy* RigProx = static_cast<Physics::RigidProxy*>( *ProxIt );
90  RigProx->SetGravity(this->Grav);
91  }
92  }
93 
94  if( !RemovedObjects.empty() ) {
95  const Vector3 WorldGravity = static_cast<Physics::PhysicsManager*>( this->ParentWorld->GetManager(ManagerBase::MT_PhysicsManager) )->GetWorldGravity();
96 
97  for( ObjectIterator RemovedIt = this->RemovedObjects.begin() ; RemovedIt != this->RemovedObjects.end() ; ++RemovedIt )
98  {
99  ProxyContainer RigidProxies;
100  (*RemovedIt)->GetProxies(Mezzanine::PT_Physics_RigidProxy,RigidProxies);
101  for( ProxyIterator ProxIt = RigidProxies.begin() ; ProxIt != RigidProxies.end() ; ++ProxIt )
102  {
103  Physics::RigidProxy* RigProx = static_cast<Physics::RigidProxy*>( *ProxIt );
104  RigProx->SetGravity(WorldGravity);
105  }
106  }
107  }
108  }
109 
110  ///////////////////////////////////////////////////////////////////////////////
111  // GravityField Properties
112 
114  { this->Grav = Gravity; }
115 
117  { return this->Grav; }
118 
119  ///////////////////////////////////////////////////////////////////////////////
120  // Serialization
121 
123  {
124  this->AreaEffect::ProtoSerializeProperties(SelfRoot);
125 
126  XML::Node PropertiesNode = SelfRoot.AppendChild( WorldObject::GetSerializableName() + "Properties" );
127 
128  if( PropertiesNode.AppendAttribute("Version").SetValue("1") )
129  {
130  XML::Node GravityNode = PropertiesNode.AppendChild("Gravity");
131  this->GetFieldGravity().ProtoSerialize( GravityNode );
132 
133  return;
134  }else{
135  SerializeError("Create XML Attribute Values",WorldObject::GetSerializableName() + "Properties",true);
136  }
137  }
138 
140  {
142 
143  XML::Node PropertiesNode = SelfRoot.GetChild( WorldObject::GetSerializableName() + "Properties" );
144 
145  if( !PropertiesNode.Empty() ) {
146  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
147  XML::Node GravityNode = PropertiesNode.GetChild("Gravity").GetFirstChild();
148  if( !GravityNode.Empty() ) {
149  Vector3 Gravity(GravityNode);
150  this->SetFieldGravity(Gravity);
151  }
152  }else{
153  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + (WorldObject::GetSerializableName() + "Properties" ) + ": Not Version 1.");
154  }
155  }else{
156  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,WorldObject::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
157  }
158  }
159 
162 
164  { return "GravityField"; }
165 
166  ///////////////////////////////////////////////////////////////////////////////
167  // GravityFieldFactory Methods
168 
170  { }
171 
173  { }
174 
177 
179  { return new GravityField(Name,TheWorld); }
180 
182  { return new GravityField(Name,AEShape,TheWorld); }
183 
185  { return static_cast<GravityField*>( this->CreateAreaEffect(XMLNode,TheWorld) ); }
186 
188  { return new GravityField(Name,TheWorld); }
189 
191  { return new GravityField(XMLNode,TheWorld); }
192 
194  { delete ToBeDestroyed; }
195 }//Mezzanine
196 
197 #endif
This is the base class for all collision shapes.
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
WorldManager * GetManager(const Whole ManagerToGet)
This is will find the manager of a given type.
Definition: world.cpp:324
GravityField(World *TheWorld)
Blank constructor.
ObjectContainer AddedObjects
Container of actors that have been added since last frame.
Definition: areaeffect.h:80
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
virtual ~GravityFieldFactory()
Class destructor.
virtual ~GravityField()
Class destructor.
ObjectContainer::iterator ObjectIterator
Iterator type for Object instances stored by this class.
Definition: areaeffect.h:71
Thrown when the requested identity could not be found.
Definition: exception.h:94
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual void SetFieldGravity(const Vector3 &Gravity)
Sets the gravity force for this field.
virtual Mezzanine::WorldObjectType GetType() const
Gets the type of the object instance.
virtual GravityField * CreateGravityField(const String &Name, World *TheWorld)
Creates a GravityField object.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual void ApplyEffect()
World * ParentWorld
This is the world this object belongs to and will be inserted in/removed from.
Definition: worldobject.h:84
virtual AreaEffect * CreateAreaEffect(const String &Name, World *TheWorld, const NameValuePairMap &Params)
Vector3 Grav
The stored value for this fields gravity.
Definition: gravityfield.h:57
bool Empty() const
Is this storing anything at all?
This implements the exception hiearchy for Mezzanine.
This is an implementation of the AreaEffect class that alters gravity in a region.
Definition: gravityfield.h:52
The interface for serialization.
std::vector< WorldProxy * > ProxyContainer
Basic container type for WorldProxy storage by this class.
Definition: worldobject.h:64
virtual String GetTypeName() const
bool SetValue(const Char8 *rhs)
Set the value of this.
This class is used to define area's in the world that have unique effects.
Definition: areaeffect.h:65
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: areaeffect.cpp:279
GravityFieldFactory()
Class constructor.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldObject.
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: areaeffect.cpp:300
This is a proxy from which rigid body proxys are handled.
Definition: rigidproxy.h:102
virtual void DestroyAreaEffect(AreaEffect *ToBeDestroyed)
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
This is simply a place for storing all the Physics Related functions.
ProxyContainer::iterator ProxyIterator
Iterator type for WorldProxy instances stored by this class.
Definition: worldobject.h:66
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
WorldObjectType
Used by various classes to help identify what class an object is.
Definition: enumerations.h:147
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
This class represents a world for objects to interact within.
Definition: world.h:74
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual void SetGravity(const Vector3 &Gravity)
Sets the gravity for only this proxy.
Definition: rigidproxy.cpp:204
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
ObjectContainer RemovedObjects
Container of actors that have been removed since last frame.
Definition: areaeffect.h:83
std::map< String, String > NameValuePairMap
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:209
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector3.cpp:588
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
virtual Vector3 GetFieldGravity() const
Gets the gravity of this field.