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

KX_ConvertSensors.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "KX_ConvertSensors.h"
00008 
00009 #include "blender.h"
00010 #include "game.h"
00011 
00012 // Sensors
00013 #include "KX_GameObject.h"
00014 #include "RAS_MeshObject.h"
00015 #include "SCA_KeyboardSensor.h"
00016 #include "SCA_MouseSensor.h"
00017 #include "SCA_AlwaysSensor.h"
00018 #include "KX_TouchSensor.h"
00019 #include "KX_NearSensor.h"
00020 #include "KX_RadarSensor.h"
00021 
00022 #include "SCA_PropertySensor.h"
00023 #include "SCA_RandomSensor.h"
00024 #include "KX_RaySensor.h"
00025 #include "SCA_EventManager.h"
00026 #include "SCA_LogicManager.h"
00027 #include "KX_BlenderInputDevice.h"
00028 #include "KX_Scene.h"
00029 #include "IntValue.h"
00030 #include "KX_BlenderKetsjiConversionMaps.h"
00031 #include "KX_BlenderKeyboardDevice.h"
00032 #include "KX_BlenderGL.h"
00033 
00034 void BL_ConvertSensors(struct Object* blenderobject,
00035                                                 class KX_GameObject* gameobj,
00036                                                 SCA_LogicManager* logicmgr,
00037                                                 KX_Scene* kxscene,
00038                                                 KX_BlenderKeyboardDevice* keydev,
00039                                                 int & executePriority
00040                                                 )

00041 {
00042 
00043         
00044 
00045         int uniqueint = 0;
00046         bSensor* sens = (bSensor*)blenderobject->sensors.first;
00047         bool pos_pulsemode = false;
00048         bool neg_pulsemode = false;
00049         int      pos_frequency = 0;
00050         int      neg_frequency = 0;
00051         bool invert    = false;
00052         
00053         while(sens)
00054         {
00055                 SCA_ISensor* gamesensor=NULL;
00056                 /* All sensors have a pulse toggle, frequency, and invert field.     */
00057                 /* These are extracted here, and set when the sensor is added to the */
00058                 /* list.                                                             */
00059                 pos_pulsemode = (sens->pulse & SENS_PULSE_REPEAT)!=0;
00060                 neg_pulsemode = (sens->pulse & SENS_NEG_PULSE_MODE)!=0;
00061 
00062                 pos_frequency = sens->freq;
00063                 /* note: neg_frequency is the same in Blender. we might change that some day...(ton) */
00064                 neg_frequency = sens->freq;
00065                 invert    = !(sens->invert == 0);
00066 
00067                 switch (sens->type)
00068                 {
00069                 case  SENS_ALWAYS:
00070                 {
00071                                 
00072                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
00073                         if (eventmgr)
00074                         {
00075                                 gamesensor = new SCA_AlwaysSensor(eventmgr, gameobj);
00076                         }
00077 
00078                         break;
00079                 }
00080 
00081                 case SENS_COLLISION:
00082                         {
00083                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
00084                                 if (eventmgr)
00085                                 {
00086                                         // collision sensor can sense both materials and properties. 
00087                                         
00088                                         bool bFindMaterial = false;
00089 
00090                                         bCollisionSensor* blendertouchsensor = (bCollisionSensor*)sens->data;
00091                                                 
00092                                         CCString touchPropOrMatName = ( blendertouchsensor->name ? blendertouchsensor->name : "" );
00093 
00094                                         bFindMaterial = (blendertouchsensor->mode 
00095                                                                          & SENS_COLLISION_MATERIAL);
00096                                         
00097                                         
00098                                         gamesensor = new KX_TouchSensor(eventmgr,
00099                                                                                                         gameobj,
00100                                                                                                         gameobj->GetSumoObject(),
00101                                                                                                         bFindMaterial,
00102                                                                                                         touchPropOrMatName);
00103                                 
00104                                 }
00105 
00106                                 break;
00107                         }
00108                 case SENS_TOUCH:
00109                 {
00110                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
00111                         if (eventmgr)
00112                         {
00113                                 CCString touchpropertyname;
00114                                 bTouchSensor* blendertouchsensor = (bTouchSensor*)sens->data;
00115                                 
00116                                 if (blendertouchsensor->ma)
00117                                 {
00118                                         touchpropertyname = (char*) (blendertouchsensor->ma->id.name);
00119                                 }
00120                                 bool bFindMaterial = true;
00121 
00122                                 gamesensor = new KX_TouchSensor(eventmgr,
00123                                                 gameobj,
00124                                                 gameobj->GetSumoObject(),
00125                                                 bFindMaterial,
00126                                                 touchpropertyname);
00127                         }
00128                         break;
00129                 }
00130                 case SENS_NEAR:
00131                 {
00132                         
00133                         SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
00134                         if (eventmgr)
00135                         {
00136                                 CCString nearpropertyname;      
00137                                 bNearSensor* blendernearsensor = (bNearSensor*)sens->data;
00138                                 if (blendernearsensor->name)
00139                                 {
00140                                         // only objects that own this property will be taken into account
00141                                         nearpropertyname = (char*) blendernearsensor->name;
00142                                 }
00143 
00144                                 DT_ShapeHandle shape    =       DT_Sphere(0.0);
00145                                 
00146                                 // this sumoObject is not deleted by a gameobj, so delete it ourself
00147                                 // later (memleaks)!
00148                                 SM_Object* sumoObj              =       new SM_Object(shape,NULL,NULL,NULL);
00149                                 sumoObj->setMargin(blendernearsensor->dist);
00150                                 sumoObj->setPosition(gameobj->NodeGetWorldPosition());
00151                                 bool bFindMaterial = false;
00152                                 gamesensor = new KX_NearSensor(eventmgr,gameobj,blendernearsensor->dist,blendernearsensor->resetdist,sumoObj,bFindMaterial,nearpropertyname);
00153                         }
00154                         break;
00155                 }
00156 
00157 
00158                 case SENS_KEYBOARD:
00159                 {
00160                         /* temporary input device, for converting the code for the keyboard sensor */
00161                         
00162                         bKeyboardSensor* blenderkeybdsensor = (bKeyboardSensor*)sens->data;
00163                         SCA_KeyboardManager* eventmgr = (SCA_KeyboardManager*) logicmgr->FindEventManager(SCA_EventManager::KEYBOARD_EVENTMGR);
00164                         if (eventmgr)
00165                         {
00166                                 gamesensor = new SCA_KeyboardSensor(eventmgr,
00167                                                                                                    keydev->ToNative(blenderkeybdsensor->key),
00168                                                                                                    blenderkeybdsensor->qual,
00169                                                                                                    blenderkeybdsensor->qual2,
00170                                                                                                    (blenderkeybdsensor->type == SENS_ALL_KEYS),
00171 
00172                                                                                                    gameobj); //                 blenderkeybdsensor->pad);
00173 
00174                         } 
00175                         
00176                         break;
00177                 }
00178                 case SENS_MOUSE:
00179                 {
00180                         int type = -1;
00181                         bMouseSensor *bmouse = (bMouseSensor *)sens->data;
00182                         SCA_MouseManager *eventmgr 
00183                                 = (SCA_MouseManager*) logicmgr->FindEventManager(SCA_EventManager::MOUSE_EVENTMGR);
00184                         if (eventmgr) {
00185                                 switch (bmouse->type) {
00186                                 case BL_SENS_MOUSE_LEFT_BUTTON:
00187                                         type = SCA_MouseSensor::KX_MOUSESENSORMODE_LEFTBUTTON;
00188                                         break;
00189                                 case BL_SENS_MOUSE_MIDDLE_BUTTON:
00190                                         type = SCA_MouseSensor::KX_MOUSESENSORMODE_MIDDLEBUTTON;
00191                                         break;
00192                                 case BL_SENS_MOUSE_RIGHT_BUTTON:
00193                                         type = SCA_MouseSensor::KX_MOUSESENSORMODE_RIGHTBUTTON;
00194                                         break;
00195                                 case BL_SENS_MOUSE_MOVEMENT:
00196                                         type = SCA_MouseSensor::KX_MOUSESENSORMODE_MOVEMENT;
00197                                         break;
00198                                 default:
00199                                         ; /* error */
00200                                 };      
00201                                 
00205 
00206                                 
00207                                  int startx = BL_GetWindowWidth()/2.0;
00208                                  int starty = BL_GetWindowHeight()/2.0;
00209                                 gamesensor = new SCA_MouseSensor(eventmgr,
00210                                                                                                 startx,starty,
00211                                                                                                 type,
00212                                                                                                 gameobj);
00213                         } else {
00214 //                              cout << "\n Could't find mouse event manager...";
00215                         }
00216                         break;
00217                 }
00218                 case SENS_PROPERTY:
00219                 {
00220                         bPropertySensor* blenderpropsensor = (bPropertySensor*) sens->data;
00221                         SCA_EventManager* eventmgr 
00222                                 = logicmgr->FindEventManager(SCA_EventManager::PROPERTY_EVENTMGR);
00223                         if (eventmgr)
00224                         {
00225                                 CCString propname=blenderpropsensor->name;
00226                                 CCString propval=blenderpropsensor->value;
00227                                 CCString propmaxval=blenderpropsensor->maxvalue;
00228 
00229                                 SCA_PropertySensor::KX_PROPSENSOR_TYPE 
00230                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NODEF;
00231 
00232                                 /* Better do an explicit conversion here! (was implicit      */
00233                                 /* before...)                                                */
00234                                 switch(blenderpropsensor->type) {
00235                                 case SENS_PROP_EQUAL:
00236                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EQUAL;
00237                                         break;
00238                                 case SENS_PROP_NEQUAL:
00239                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_NOTEQUAL;
00240                                         break;
00241                                 case SENS_PROP_INTERVAL:
00242                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_INTERVAL;
00243                                         break;
00244                                 case SENS_PROP_CHANGED:
00245                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_CHANGED;
00246                                         break;
00247                                 case SENS_PROP_EXPRESSION:
00248                                         propchecktype = SCA_PropertySensor::KX_PROPSENSOR_EXPRESSION;
00249                                         /* error */
00250                                         break;
00251                                 default:
00252                                         ; /* error */
00253                                 }
00254                                 gamesensor = new SCA_PropertySensor(eventmgr,gameobj,propname,propval,propmaxval,propchecktype);
00255                         }
00256                                 
00257                         break;
00258                 }
00259 
00260                 case SENS_RADAR:
00261                         {
00262                                 
00263                                 
00264                                 
00265                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);
00266                                 if (eventmgr)
00267                                 {
00268                                         CCString radarpropertyname;
00269                                         CCString touchpropertyname;
00270                                         bRadarSensor* blenderradarsensor = (bRadarSensor*) sens->data;
00271                                         
00272                                         int             radaraxis = blenderradarsensor->axis;
00273 
00274                                         if (blenderradarsensor->name)
00275                                         {
00276                                                 // only objects that own this property will be taken into account
00277                                                 radarpropertyname = (char*) blenderradarsensor->name;
00278                                         }
00279 
00280                                         
00281                                         MT_Scalar coneheight = blenderradarsensor->range;
00282                                         MT_Scalar factor = tan(MT_radians(blenderradarsensor->angle));
00283                                         MT_Scalar coneradius = coneheight * factor;
00284                                         
00285                                         DT_ShapeHandle shape    =       DT_Cone(coneradius,coneheight);
00286                                         
00287                                         // this sumoObject is not deleted by a gameobj, so delete it ourself
00288                                         // later (memleaks)!
00289                                         SM_Object* sumoObj              =       new SM_Object(shape,NULL,NULL,NULL);
00290                                         sumoObj->setMargin(0.0);
00291                                         
00292                                         sumoObj->setPosition(gameobj->NodeGetWorldPosition());
00293                                         MT_Scalar smallmargin = 0.0;
00294                                         MT_Scalar largemargin = 0.1;
00295                                         
00296                                         bool bFindMaterial=false;
00297                                         gamesensor = new KX_RadarSensor(
00298                                                                                 eventmgr,
00299                                                                                 gameobj,
00300                                                                                 coneradius,
00301                                                                                 coneheight,
00302                                                                                 radaraxis,
00303                                                                                 smallmargin,
00304                                                                                 largemargin,
00305                                                                                 sumoObj,
00306                                                                                 bFindMaterial,
00307                                                                                 radarpropertyname);
00308 
00309 
00310                                 }
00311                                 
00312                                 break;
00313                         }
00314                 case SENS_RAY:
00315                         {
00316                                 
00317                                 bRaySensor* blenderraysensor = (bRaySensor*) sens->data;
00318                                 
00319                                 //blenderradarsensor->angle;
00320                                 SCA_EventManager* eventmgr 
00321                                         = logicmgr->FindEventManager(SCA_EventManager::RAY_EVENTMGR);
00322                                 if (eventmgr)
00323                                 {
00324                                         bool bFindMaterial = (blenderraysensor->mode 
00325                                                                                  & SENS_COLLISION_MATERIAL);
00326 
00327                                         CCString checkname = (bFindMaterial? blenderraysensor->matname : blenderraysensor->propname);
00328                                         // don't want to get rays of length 0.0 or so
00329                                         double distance = ( blenderraysensor->range < 0.01 ?    0.01 :  blenderraysensor->range );
00330                                         gamesensor = new KX_RaySensor(eventmgr,gameobj,checkname,bFindMaterial,distance,kxscene->GetSumoScene());
00331                                 }
00332                         
00333                                 break;
00334 
00335                         }
00336 
00337                 case SENS_RANDOM:
00338                 {
00339                         bRandomSensor* blenderrndsensor = (bRandomSensor*) sens->data;
00340                         // some files didn't write randomsensor, avoid crash now for NULL ptr's
00341                         if (blenderrndsensor)
00342                         {
00343                                 SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::RANDOM_EVENTMGR);
00344                                 if (eventmgr)
00345                                 {
00346                                         int randomSeed = blenderrndsensor->seed;
00347                                         gamesensor = new SCA_RandomSensor(eventmgr, gameobj, randomSeed);
00348                                 }
00349                         }
00350                         break;
00351                 }
00352                 default:
00353                 {
00354                         int i=0;
00355                 }
00356 
00357 
00358                 }
00359                 if (gamesensor)
00360                 {
00361                         gamesensor->SetExecutePriority(executePriority++);
00362                         CCString uniquename = sens->name;
00363                         uniquename += "#SENS#";
00364                         uniqueint++;
00365                         CIntValue* uniqueval = new CIntValue(uniqueint);
00366                         uniquename += uniqueval->GetText();
00367                         uniqueval->Release();
00368 
00369                         /* Conversion succeeded, so we can set the generic props here.   */
00370                         gamesensor->SetPulseMode(pos_pulsemode, neg_pulsemode, 
00371                                                                          pos_frequency, neg_frequency);
00372                         gamesensor->SetInvert(invert);
00373 
00374                         gamesensor->SetName(CCString(sens->name));                      
00375 
00376                         gameobj->AddSensor(gamesensor);
00377                         
00378                         // only register to manager if it's in an active layer
00379 
00380                         if (gameobj->IsInLayer())
00381                                 gamesensor->RegisterToManager();
00382 
00383                         
00384                         for (int i=0;i<sens->totlinks;i++)
00385                         {
00386                                 bController* linkedcont = (bController*) sens->links[i];
00387                                 SCA_IController** gamecontptr = map_blender_to_gamecontroller[CHashedPtr(linkedcont)];
00388                                 if (gamecontptr)
00389                                 {
00390                                         SCA_IController* gamecont = *gamecontptr;
00391                                         
00392                                                 logicmgr->RegisterToSensor(gamecont,gamesensor);
00393                                         
00394                                 }
00395                         }
00396                 
00397                 }
00398                 sens=sens->next;
00399         }
00400 
00401         
00402         
00403         
00404 }

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