00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SoundListenerComponent.h"
00018 #include "../../core/GameManager.h"
00019
00020
00021
00022
00023 SoundListenerComponent* SoundListenerComponent::mMainListener = 0;
00024
00025
00026 SoundListenerComponent::SoundListenerComponent(Entity *parent, bool isMainListener)
00027 : EntityComponent(parent), mPosition(vector3df(0, 0, 0)), mTarget(vector3df(0, 0, 0))
00028 {
00029
00030 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00031
00032
00033 if(isMainListener)
00034 {
00035 if(mMainListener)
00036 mMainListener->setIsMainListener(false);
00037
00038 setIsMainListener(true);
00039 }
00040
00041
00042 mName = "SoundListenerComponent";
00043
00044
00045 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPositionChange",
00046 this, &SoundListenerComponent::onPositionChange);
00047 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00048 this, &SoundListenerComponent::onPause);
00049 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00050 this, &SoundListenerComponent::onUnPause);
00051 }
00052
00053
00054 SoundListenerComponent::~SoundListenerComponent()
00055 {
00056 setIsMainListener(false);
00057 }
00058
00059
00060 SoundListenerComponent* SoundListenerComponent::refFactory(Entity *parent, bool isMainListener)
00061 {
00062 return new SoundListenerComponent(parent, isMainListener);
00063 }
00064
00065
00066 bool SoundListenerComponent::getIsMainListener() const
00067 {
00068 return mIsMainListener;
00069 }
00070
00071
00072 const vector3df& SoundListenerComponent::getTarget() const
00073 {
00074 return mTarget;
00075 }
00076
00077
00078 void SoundListenerComponent::setIsMainListener(bool value)
00079 {
00080 if(value)
00081 {
00082 mMainListener = this;
00083 mIsMainListener = true;
00084
00085 sf::Listener::SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00086 sf::Listener::SetTarget(mTarget.X, mTarget.Y, mTarget.Z);
00087 }
00088
00089 else
00090 {
00091 if(mIsMainListener)
00092 mMainListener = 0;
00093
00094 mIsMainListener = false;
00095 }
00096 }
00097
00098
00099 void SoundListenerComponent::setTarget(const vector3df &target)
00100 {
00101 mTarget = target;
00102 if(mIsMainListener)
00103 sf::Listener::SetTarget(mTarget.X, mTarget.Y, mTarget.Z);
00104 }
00105
00106
00107
00108 void SoundListenerComponent::onPositionChange(void *p)
00109 {
00110 mPosition += static_cast<vector3df*>(p)[0];
00111 if(mIsMainListener)
00112 sf::Listener::SetPosition(mPosition.X, mPosition.Y, mPosition.Z);
00113 }
00114
00115
00116 void SoundListenerComponent::onPause(void *p)
00117 {
00118 mWasMainListener = getIsMainListener();
00119 setIsMainListener(false);
00120 }
00121
00122
00123 void SoundListenerComponent::onUnPause(void *p)
00124 {
00125 if(mWasMainListener)
00126 setIsMainListener(true);
00127 }
00128
00129
00130
00131 void bindSoundListenerComponent(asIScriptEngine *engine)
00132 {
00133
00134 int r;
00135
00136
00137 r = engine->RegisterObjectType("SoundListenerComponent", sizeof(SoundListenerComponent), asOBJ_REF); assert(r >= 0);
00138
00139
00140 bindEntityComponentBase<SoundListenerComponent>(engine, "SoundListenerComponent");
00141
00142
00143 r = engine->RegisterGlobalBehaviour(asBEHAVE_REF_CAST, "SoundListenerComponent@ f(EntityComponent @)",
00144 asFUNCTION((asRefCast<EntityComponent,SoundListenerComponent>)), asCALL_CDECL); assert( r >= 0 );
00145 r = engine->RegisterGlobalBehaviour(asBEHAVE_IMPLICIT_REF_CAST, "EntityComponent@ f(SoundListenerComponent @)",
00146 asFUNCTION((asRefCast<SoundListenerComponent,EntityComponent>)), asCALL_CDECL); assert( r >= 0 );
00147
00148 r = engine->RegisterObjectBehaviour("SoundListenerComponent", asBEHAVE_FACTORY, "SoundListenerComponent@ f(Entity @)",
00149 asFUNCTION(SoundListenerComponent::refFactory), asCALL_CDECL); assert(r >= 0);
00150 r = engine->RegisterObjectBehaviour("SoundListenerComponent", asBEHAVE_FACTORY, "SoundListenerComponent@ f(Entity @, bool)",
00151 asFUNCTION(SoundListenerComponent::refFactory), asCALL_CDECL); assert(r >= 0);
00152
00153
00154 r = engine->RegisterObjectMethod("SoundListenerComponent", "bool getIsMainListener()",
00155 asMETHOD(SoundListenerComponent, getIsMainListener), asCALL_THISCALL); assert(r >= 0);
00156 r = engine->RegisterObjectMethod("SoundListenerComponent", "const vector3df& getTarget()",
00157 asMETHOD(SoundListenerComponent, getTarget), asCALL_THISCALL); assert(r >= 0);
00158
00159 r = engine->RegisterObjectMethod("SoundListenerComponent", "void setIsMainListener()",
00160 asMETHOD(SoundListenerComponent, setIsMainListener), asCALL_THISCALL); assert(r >= 0);
00161 r = engine->RegisterObjectMethod("SoundListenerComponent", "void setIsMainListener(bool)",
00162 asMETHOD(SoundListenerComponent, setIsMainListener), asCALL_THISCALL); assert(r >= 0);
00163 r = engine->RegisterObjectMethod("SoundListenerComponent", "void setTarget(const vector3df &in)",
00164 asMETHOD(SoundListenerComponent, setTarget), asCALL_THISCALL); assert(r >= 0);
00165 }
00166
00167