Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

KX_TouchSensor.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "KX_TouchSensor.h"
00008 #include "SCA_EventManager.h"
00009 #include "SCA_LogicManager.h"
00010 #include "KX_GameObject.h"
00011 #include "KX_TouchEventManager.h"
00012 #include "SM_Object.h"
00013 
00014 /* ------------------------------------------------------------------------- */
00015 /* Native functions                                                          */
00016 /* ------------------------------------------------------------------------- */
00017 
00018 void KX_TouchSensor::SynchronizeTransform()

00019 {
00020 
00021         if (m_sumoObj)
00022         {
00023                 m_sumoObj->setPosition(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
00024                 m_sumoObj->calcXform();
00025         }
00026         
00027 }
00028 
00029 
00030 void KX_TouchSensor::EndFrame() {
00031         m_colliders->ReleaseAndRemoveAll();
00032         m_bTriggered = false;
00033 }
00034 
00035 bool KX_TouchSensor::Evaluate(CValue* event)

00036 {
00037         bool result = false;
00038 
00039         if (m_bTriggered != m_bLastTriggered)
00040         {
00041                 m_bLastTriggered = m_bTriggered;
00042                 if (!m_bTriggered)
00043                         m_hitObject = NULL;
00044                 result = true;
00045         }
00046         
00047         return result;
00048 }
00049 
00050 KX_TouchSensor::KX_TouchSensor(SCA_EventManager* eventmgr,KX_GameObject* gameobj,SM_Object* sumoObj,bool bFindMaterial,const CCString& touchedpropname,PyTypeObject* T)
00051 :SCA_ISensor(gameobj,eventmgr,T),
00052 m_touchedpropname(touchedpropname),
00053 m_bFindMaterial(bFindMaterial),
00054 m_sumoObj(sumoObj),
00055 m_bCollision(false),
00056 m_bTriggered(false),
00057 m_bLastTriggered(false)

00058 {
00059         m_hitObject =  NULL;
00060         m_colliders = new CListValue();
00061 }
00062 
00063 
00064 KX_TouchSensor::~KX_TouchSensor()

00065 {
00066         m_colliders->Release();
00067 }
00068 
00069 void    KX_TouchSensor::ReParent(SCA_IObject* parent)

00070 {
00071 
00072         m_sumoObj = ((KX_GameObject*)parent)->GetSumoObject();
00073         SCA_ISensor::ReParent(parent);
00074 }
00075 
00076 
00077 void KX_TouchSensor::RegisterSumo(SCA_EventManager* eventmgr,DT_RespTableHandle resptable)

00078 {
00079         m_eventmgr=eventmgr;
00080                 //KX_GameObject* gameobj = (KX_GameObject*)GetParent();
00081                 //SM_Object* smobj = gameobj->GetSumoObject();
00082                 
00083                 if (m_sumoObj)
00084                 {
00085                         DT_SetObjectResponse(resptable,m_sumoObj->getObjectHandle(),collisionResponse,DT_SIMPLE_RESPONSE,this);
00086                 //SM_DEPTH_RESPONSE,this);
00087                 }
00088 
00089 }
00090 
00091 void    KX_TouchSensor::HandleCollision(void* obj1,void* obj2,const DT_CollData * coll_data)

00092 {
00093         KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
00094         KX_GameObject* parent = (KX_GameObject*)GetParent();
00095 
00096         // need the mapping from SM_Objects to gameobjects now
00097         
00098         SM_ClientObjectInfo* client_info =(SM_ClientObjectInfo*) (obj1 == m_sumoObj? 
00099                                         ((SM_Object*)obj2)->getClientObject() : 
00100                                         ((SM_Object*)obj1)->getClientObject());
00101 
00102 
00103         KX_GameObject* gameobj = ( client_info ? 
00104                         (KX_GameObject*)client_info->m_clientobject : 
00105                         NULL);
00106 
00107         if (gameobj && (gameobj != parent))
00108         {
00109                 if (!m_colliders->SearchValue(gameobj))
00110                         m_colliders->Add(gameobj->AddRef());
00111                 
00112                 bool found = (m_touchedpropname.Length() == 0);
00113                 if (!found)
00114                 {
00115                         if (m_bFindMaterial)
00116                         {
00117                                 if (client_info->m_auxilary_info)
00118                                         found = (m_touchedpropname == ((char*)client_info->m_auxilary_info));
00119 
00120                                 if (found)
00121                                 {
00122                                         int i=0;
00123                                 }
00124 
00125                         } else
00126                         {
00127                                 found = (gameobj->GetProperty(m_touchedpropname) != NULL);
00128                         }
00129                 }
00130                 if (found)
00131                 {
00132                         m_bTriggered = true;
00133                         m_hitObject = gameobj;
00134                 }
00135                 
00136         } else
00137         {
00138                 
00139         }
00140 }
00141 
00142 /* ------------------------------------------------------------------------- */
00143 /* Python functions                                                          */
00144 /* ------------------------------------------------------------------------- */
00145 /* Integration hooks ------------------------------------------------------- */
00146 PyTypeObject KX_TouchSensor::Type = {
00147         PyObject_HEAD_INIT(&PyType_Type)
00148         0,
00149         "KX_TouchSensor",
00150         sizeof(KX_TouchSensor),
00151         0,
00152         PyDestructor,
00153         0,
00154         __getattr,
00155         __setattr,
00156         0, //&MyPyCompare,
00157         __repr,
00158         0, //&cvalue_as_number,
00159         0,
00160         0,
00161         0,
00162         0
00163 };
00164 
00165 PyParentObject KX_TouchSensor::Parents[] = {
00166         &KX_TouchSensor::Type,
00167         &SCA_ISensor::Type,
00168         &SCA_ILogicBrick::Type,
00169         &CValue::Type,
00170         NULL
00171 };
00172 
00173 PyMethodDef KX_TouchSensor::Methods[] = {
00174         {"setProperty", (PyCFunction) KX_TouchSensor::sPySetProperty, METH_VARARGS},
00175         {"getProperty", (PyCFunction) KX_TouchSensor::sPyGetProperty, METH_VARARGS},
00176         {"getHitObject", (PyCFunction) KX_TouchSensor::sPyGetHitObject, METH_VARARGS},
00177         {"getHitObjectList", (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_VARARGS},
00178         {NULL,NULL} //Sentinel
00179 };
00180 
00181 PyObject* KX_TouchSensor::_getattr(char* attr) {
00182         _getattr_up(SCA_ISensor);
00183 }
00184 
00185 /* Python API */
00186 
00187 /* 1. setProperty */
00188 char KX_TouchSensor::SetProperty_doc[] = 
00189 "setProperty(name)\n"
00190 "\t- name: string\n"
00191 "\tSet the property or material to collide with. Use\n"
00192 "\tsetTouchMaterial() to switch between properties and\n"
00193 "\tmaterials.";
00194 PyObject* KX_TouchSensor::PySetProperty(PyObject* self, 
00195                                                                                 PyObject* args, 
00196                                                                                 PyObject* kwds) {
00197         char *nameArg;
00198         if (!PyArg_ParseTuple(args, "s", &nameArg)) {
00199                 return NULL;
00200         }
00201 
00202         CValue* prop = GetParent()->FindIdentifier(nameArg);
00203 
00204         if (prop) {
00205                 m_touchedpropname = nameArg;
00206         } else {
00207                 ; /* not found ... */
00208         }
00209         
00210         Py_Return;
00211 }
00212 /* 2. getProperty */
00213 char KX_TouchSensor::GetProperty_doc[] = 
00214 "getProperty(name)\n"
00215 "\tReturns the property or material to collide with. Use\n"
00216 "\tgetTouchMaterial() to find out whether this sensor\n"
00217 "\tlooks for properties or materials.";
00218 PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self, 
00219                                                                                  PyObject* args, 
00220                                                                                  PyObject* kwds) {
00221         return PyString_FromString(m_touchedpropname);
00222 }
00223 
00224 PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self, 
00225                                                                                  PyObject* args, 
00226                                                                                  PyObject* kwds)

00227 {
00228         /* to do: do Py_IncRef if the object is already known in Python */
00229         /* otherwise, this leaks memory */
00230         if (m_hitObject)
00231         {
00232                 return m_hitObject->AddRef();
00233         }
00234         Py_Return;
00235 }
00236 
00237 PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self, 
00238                                                                                  PyObject* args, 
00239                                                                                  PyObject* kwds)

00240 {
00241         /* to do: do Py_IncRef if the object is already known in Python */
00242         /* otherwise, this leaks memory */
00243         return m_colliders->AddRef();
00244 }
00245 
00246 /* 5. getTouchMaterial */
00247 char KX_TouchSensor::GetTouchMaterial_doc[] = 
00248 "getTouchMaterial()\n"
00249 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
00250 "\tKX_FALSE if it looks for a specific property.\n" ;
00251 PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self, 
00252                                                                                          PyObject* args, 
00253                                                                                          PyObject* kwds)

00254 {
00255         int retval = 0;
00256         
00257         if (m_bFindMaterial) {
00258                 retval = KX_TRUE;
00259         } else {
00260                 retval = KX_FALSE;
00261         }
00262 
00263         return PyInt_FromLong(retval);
00264 }
00265 
00266 /* 6. setTouchMaterial */
00267 char KX_TouchSensor::SetTouchMaterial_doc[] = 
00268 "setTouchMaterial(flag)\n"
00269 "\t- flag: KX_TRUE or KX_FALSE.\n"
00270 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
00271 "\tKX_FALSE to switch off positive pulse mode.\n" ;
00272 PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyObject* kwds)

00273 {
00274         int pulseArg = 0;
00275 
00276         if(!PyArg_ParseTuple(args, "i", &pulseArg)) {
00277                 return NULL;
00278         }
00279         
00280         if (pulseArg == KX_TRUE) {
00281                 m_bFindMaterial = true;
00282         } else if (pulseArg == KX_FALSE){
00283                 m_bFindMaterial = false;
00284         } else {
00285                 ; /* internal error */
00286         }
00287 
00288         Py_Return;
00289 }
00290 
00291 /* eof */

Generated at Thu Feb 1 13:03:07 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000