00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "forceeffector.h"
00024 #include "forceaction.h"
00025 #include <zeitgeist/logserver/logserver.h>
00026 #include <oxygen/gamecontrolserver/actionobject.h>
00027
00028 using namespace boost;
00029 using namespace oxygen;
00030 using namespace salt;
00031
00032 ForceEffector::ForceEffector() : oxygen::Effector()
00033 {
00034 }
00035
00036 ForceEffector::~ForceEffector()
00037 {
00038 }
00039
00040 bool ForceEffector::Realize(boost::shared_ptr<ActionObject> action)
00041 {
00042 if (mBody.get() == 0)
00043 {
00044 return false;
00045 }
00046
00047 shared_ptr<ForceAction> forceAction =
00048 shared_dynamic_cast<ForceAction>(action);
00049
00050 if (forceAction.get() == 0)
00051 {
00052 GetLog()->Error()
00053 << "ERROR: (ForceEffector) cannot realize an unknown ActionObject\n";
00054 return false;
00055 }
00056
00057 const Vector3f& force = forceAction->GetForce();
00058 mBody->AddForce(Vector3f(force[0],force[1],force[2]));
00059
00060 return true;
00061 }
00062
00063 shared_ptr<ActionObject>
00064 ForceEffector::GetActionObject(const Predicate& predicate)
00065 {
00066 if (predicate.name != GetPredicate())
00067 {
00068 GetLog()->Error() << "ERROR: (ForceEffector) invalid predicate"
00069 << predicate.name << "\n";
00070 return shared_ptr<ActionObject>();
00071 }
00072
00073 Vector3f force;
00074 if (! predicate.GetValue(predicate.begin(), force))
00075 {
00076 GetLog()->Error()
00077 << "ERROR: (ForceEffector) Vector3f parameter expected\n";
00078 return shared_ptr<ActionObject>();
00079 };
00080
00081 return shared_ptr<ActionObject>(new ForceAction(GetPredicate(),force));
00082 }
00083
00084 void ForceEffector::OnLink()
00085 {
00086 shared_ptr<BaseNode> parent =
00087 shared_dynamic_cast<BaseNode>(make_shared(GetParent()));
00088
00089 if (parent.get() == 0)
00090 {
00091 GetLog()->Error()
00092 << "ERROR: (ForceEffector) parent node is not derived from BaseNode\n";
00093 return;
00094 }
00095
00096
00097
00098 mBody = shared_dynamic_cast<Body>(parent->GetChildOfClass("Body"));
00099
00100 if (mBody.get() == 0)
00101 {
00102 GetLog()->Error()
00103 << "ERROR: (ForceEffector) parent node has no Body child;"
00104 "cannot apply force\n";
00105 return;
00106 }
00107 }
00108
00109 void ForceEffector::OnUnlink()
00110 {
00111 mBody.reset();
00112 }
00113
00114