| [10473] | 1 |   /* | 
|---|
| [10406] | 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
 | 3 |  | 
|---|
 | 4 |    Copyright (C) 2004 orx | 
|---|
 | 5 |  | 
|---|
 | 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
 | 7 |    it under the terms of the GNU General Public License as published by | 
|---|
 | 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
 | 9 |    any later version. | 
|---|
 | 10 |  | 
|---|
 | 11 | ### File Specific: | 
|---|
 | 12 |    main-programmer: Filip Gospodinov | 
|---|
 | 13 |    co-programmer: Silvan Nellen | 
|---|
 | 14 | */ | 
|---|
 | 15 |  | 
|---|
 | 16 | #include "shell_command.h" | 
|---|
 | 17 | #include "cameraman.h" | 
|---|
 | 18 | #include "game_world_data.h" | 
|---|
 | 19 | #include "state.h" | 
|---|
 | 20 | #include "sound_engine.h" | 
|---|
 | 21 | #include <string> | 
|---|
 | 22 | #include "script_class.h" | 
|---|
 | 23 | #include "loading/load_param_xml.h" | 
|---|
 | 24 | #include "blackscreen.h" | 
|---|
 | 25 | #include "p_node.h" | 
|---|
 | 26 | #include "camera.h" | 
|---|
 | 27 |  | 
|---|
 | 28 | ObjectListDefinition(CameraMan); | 
|---|
 | 29 |  | 
|---|
 | 30 | SHELL_COMMAND(camerainfo, CameraMan, cameraInfo); | 
|---|
 | 31 |  | 
|---|
 | 32 |  | 
|---|
 | 33 | CREATE_SCRIPTABLE_CLASS(CameraMan, | 
|---|
 | 34 |                         addMethod("changeCurrTarget", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::changeCurrTarget)) | 
|---|
 | 35 |                         ->addMethod("atachCurrCameraToWorldEntity", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::atachCurrCameraToWorldEntity)) | 
|---|
 | 36 |                         ->addMethod("changeTarget", Executor3<CameraMan, lua_State*, const std::string&, const std::string&,const std::string&>(&CameraMan::changeTarget)) | 
|---|
| [10416] | 37 |                         ->addMethod("atachCameraToWorldEntity", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::atachCameraToWorldEntity)) | 
|---|
| [10413] | 38 |                         ->addMethod("detachCurrCamera", Executor0<CameraMan, lua_State*>(&CameraMan::detachCurrCamera)) | 
|---|
| [10406] | 39 |                         ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam)) | 
|---|
| [10428] | 40 |                         ->addMethod("toggleFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade)) | 
|---|
| [10424] | 41 |                         ->addMethod("initFadeBlack", Executor0<CameraMan, lua_State*>(&CameraMan::initFadeBlack)) | 
|---|
| [10406] | 42 |                         ->addMethod("getCurrCameraCoorX", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorX)) | 
|---|
 | 43 |                         ->addMethod("getCurrCameraCoorY", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorY)) | 
|---|
 | 44 |                         ->addMethod("getCurrCameraCoorZ", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorZ)) | 
|---|
| [10441] | 45 |                         ->addMethod("jumpCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::jumpCurrCam)) | 
|---|
| [10483] | 46 |                         ->addMethod("jumpCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::jumpCam)) | 
|---|
| [10406] | 47 |                        ); | 
|---|
 | 48 |  | 
|---|
 | 49 |  | 
|---|
 | 50 | CameraMan::CameraMan(const TiXmlElement* root) | 
|---|
 | 51 | { | 
|---|
 | 52 |   this->registerObject(this, CameraMan::_objectList); | 
|---|
 | 53 |  | 
|---|
 | 54 |   this->nearClip = 1.0; | 
|---|
 | 55 |   this->farClip = 1000.0; | 
|---|
 | 56 |  | 
|---|
 | 57 |   this->fadeToBlack=new BlackScreen(); | 
|---|
 | 58 |  | 
|---|
 | 59 |   this->setCam( State::getCamera()); | 
|---|
 | 60 |  | 
|---|
 | 61 |   if (root != NULL) | 
|---|
 | 62 |     this->loadParams(root); | 
|---|
 | 63 | } | 
|---|
 | 64 |  | 
|---|
 | 65 |  | 
|---|
 | 66 | void CameraMan::loadParams(const TiXmlElement* root) | 
|---|
 | 67 | { | 
|---|
 | 68 |   BaseObject::loadParams(root); | 
|---|
| [10414] | 69 |   LoadParamXML(root, "Cameras", this, CameraMan, createCameras); | 
|---|
 | 70 | } | 
|---|
 | 71 |  | 
|---|
 | 72 |  | 
|---|
 | 73 | void CameraMan::createCameras(const TiXmlElement* camerasTag) | 
|---|
 | 74 | { | 
|---|
 | 75 |  | 
|---|
| [10411] | 76 |     LOAD_PARAM_START_CYCLE(camerasTag, object); | 
|---|
 | 77 |     { | 
|---|
 | 78 |       this->createCam(object); | 
|---|
 | 79 |     } | 
|---|
 | 80 |     LOAD_PARAM_END_CYCLE(object); | 
|---|
| [10406] | 81 |  | 
|---|
 | 82 | } | 
|---|
 | 83 |  | 
|---|
 | 84 |  | 
|---|
 | 85 | void CameraMan::createCam(const TiXmlElement* root) | 
|---|
 | 86 | { | 
|---|
 | 87 |   this->cameras.push_back(new Camera(root)); | 
|---|
 | 88 |   cameras[cameras.size()-1]->setClipRegion(nearClip, farClip); | 
|---|
 | 89 | } | 
|---|
 | 90 |  | 
|---|
| [10480] | 91 |  | 
|---|
 | 92 |  | 
|---|
 | 93 |  | 
|---|
 | 94 | void CameraMan::setCam(unsigned int cameraNo) | 
|---|
| [10406] | 95 | { | 
|---|
 | 96 |   if (cameraNo<cameras.size()) | 
|---|
 | 97 |   { | 
|---|
 | 98 |     this->setCam( cameras[cameraNo]); | 
|---|
 | 99 |   } | 
|---|
 | 100 | } | 
|---|
 | 101 |  | 
|---|
 | 102 | void CameraMan::setCam(const std::string& camName) | 
|---|
 | 103 | { | 
|---|
 | 104 |   BaseObject* object = ObjectListBase::getBaseObject("Camera", camName); | 
|---|
 | 105 |  | 
|---|
 | 106 |   if(object != NULL) | 
|---|
 | 107 |   { | 
|---|
| [10480] | 108 |     this->setCam(dynamic_cast<Camera*>(object)); | 
|---|
| [10406] | 109 |     return; | 
|---|
 | 110 |   } | 
|---|
 | 111 |   printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", camName.c_str()); | 
|---|
 | 112 | } | 
|---|
 | 113 |  | 
|---|
 | 114 |  | 
|---|
 | 115 | void CameraMan::setCam(Camera* camera) | 
|---|
 | 116 | { | 
|---|
 | 117 |   if( camera == NULL) | 
|---|
 | 118 |   { | 
|---|
 | 119 |     PRINTF(0)("trying to add a zero camera! uiuiui!\n"); | 
|---|
| [10480] | 120 |     return; | 
|---|
| [10406] | 121 |   } | 
|---|
| [10480] | 122 |    | 
|---|
| [10406] | 123 |   this->currentCam = camera; | 
|---|
 | 124 |  | 
|---|
 | 125 |   State::setCamera(currentCam, currentCam->getTarget()); | 
|---|
 | 126 |   OrxSound::SoundEngine::getInstance()->setListener(currentCam); | 
|---|
 | 127 |  | 
|---|
 | 128 |   // check if it is already added | 
|---|
 | 129 |   if( ! this->cameraIsInVector(currentCam) ) | 
|---|
 | 130 |     this->cameras.push_back(currentCam); | 
|---|
 | 131 |  | 
|---|
 | 132 |   this->fadeToBlack->setRelCoor(0., 0., 0.); | 
|---|
 | 133 |   this->fadeToBlack->setParent(this->currentCam); | 
|---|
 | 134 | } | 
|---|
 | 135 |  | 
|---|
 | 136 |  | 
|---|
 | 137 | void CameraMan::moveCurrCam(int x, int y, int z) | 
|---|
 | 138 | { | 
|---|
 | 139 |   currentCam->target->trans(x,y,z); | 
|---|
 | 140 | } | 
|---|
 | 141 |  | 
|---|
 | 142 |  | 
|---|
 | 143 | void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName) | 
|---|
 | 144 | { | 
|---|
 | 145 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
 | 146 |   if( object != NULL && object->isA(PNode::staticClassID())) | 
|---|
| [10463] | 147 |   { | 
|---|
| [10406] | 148 |     currentCam->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
| [10463] | 149 |     State::setCamera(this->currentCam,  dynamic_cast<CameraTarget*>(object)); | 
|---|
 | 150 |   } | 
|---|
| [10406] | 151 | } | 
|---|
 | 152 |  | 
|---|
| [10463] | 153 |  | 
|---|
| [10406] | 154 | void CameraMan::atachCurrCameraToWorldEntity(const std::string& className, const std::string& targetEntity) | 
|---|
 | 155 | { | 
|---|
 | 156 |   BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); | 
|---|
| [10408] | 157 |  | 
|---|
| [10480] | 158 |   if(object != NULL && object->isA(PNode::staticClassID())) | 
|---|
| [10408] | 159 |   { | 
|---|
| [10482] | 160 |    this->atachTarget(this->currentCam, dynamic_cast<PNode*>(object)); | 
|---|
| [10408] | 161 |    return; | 
|---|
 | 162 |   } | 
|---|
 | 163 |  | 
|---|
| [10416] | 164 | printf("ERROR CAMERAMANAGER: Couldn't set camera to: %s %s \n", className.c_str(),targetEntity.c_str() ); | 
|---|
| [10406] | 165 | } | 
|---|
 | 166 |  | 
|---|
| [10416] | 167 |  | 
|---|
| [10480] | 168 |  | 
|---|
 | 169 | void CameraMan::detachCurrCamera() | 
|---|
| [10416] | 170 | { | 
|---|
| [10480] | 171 |   currentCam->target->detach(); | 
|---|
 | 172 | } | 
|---|
| [10416] | 173 |  | 
|---|
 | 174 |  | 
|---|
| [10480] | 175 |  | 
|---|
 | 176 | void CameraMan::jumpCurrCam(float x, float y, float z) | 
|---|
 | 177 | { | 
|---|
 | 178 |   currentCam->target->jump(x, y, z); | 
|---|
| [10416] | 179 | } | 
|---|
 | 180 |  | 
|---|
| [10480] | 181 | void CameraMan::togglFade() | 
|---|
 | 182 | { | 
|---|
 | 183 |   if( this->fadeToBlack) | 
|---|
 | 184 |     fadeToBlack->toggleFade(); | 
|---|
 | 185 | } | 
|---|
| [10416] | 186 |  | 
|---|
| [10480] | 187 | void CameraMan::initFadeBlack() | 
|---|
| [10413] | 188 | { | 
|---|
| [10480] | 189 |   if( this->fadeToBlack) | 
|---|
 | 190 |     fadeToBlack->initFadeBlack(); | 
|---|
| [10413] | 191 | } | 
|---|
| [10406] | 192 |  | 
|---|
| [10480] | 193 |  | 
|---|
 | 194 | void CameraMan::moveCam(int x, int y, int z, int camNo) | 
|---|
| [10406] | 195 | { | 
|---|
| [10480] | 196 |   cameras[camNo]->target->trans(x,y,z); | 
|---|
| [10406] | 197 | } | 
|---|
 | 198 |  | 
|---|
 | 199 |  | 
|---|
| [10480] | 200 | void CameraMan::changeTarget(int camNo,const std::string& className, const std::string& objectName) | 
|---|
 | 201 | { | 
|---|
 | 202 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
 | 203 |   if( object != NULL && object->isA(PNode::staticClassID())) | 
|---|
 | 204 |   { | 
|---|
 | 205 |     cameras[camNo]->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
 | 206 |   } | 
|---|
 | 207 | } | 
|---|
| [10406] | 208 |  | 
|---|
| [10480] | 209 |  | 
|---|
 | 210 | void CameraMan::changeTarget(const std::string& camName,const std::string& className, const std::string& objectName) | 
|---|
| [10406] | 211 | { | 
|---|
| [10480] | 212 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
 | 213 |   BaseObject* newCam = ObjectListBase::getBaseObject("Camera", camName); | 
|---|
 | 214 |   if( object != NULL && newCam != NULL && object->isA(PNode::staticClassID())) | 
|---|
 | 215 |   { | 
|---|
 | 216 |     dynamic_cast<Camera*>(newCam)->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
 | 217 |     State::setCamera( dynamic_cast<Camera*>(newCam),  dynamic_cast<CameraTarget*>(object)); | 
|---|
 | 218 |   } | 
|---|
| [10406] | 219 | } | 
|---|
 | 220 |  | 
|---|
 | 221 |  | 
|---|
| [10480] | 222 | void CameraMan::atachCameraToWorldEntity(const std::string& cameraName, const std::string& className, const std::string& targetEntity) | 
|---|
| [10406] | 223 | { | 
|---|
| [10480] | 224 |   BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); | 
|---|
 | 225 |   BaseObject* targetCam = ObjectListBase::getBaseObject("Camera", cameraName); | 
|---|
 | 226 |  | 
|---|
 | 227 |   if( object != NULL && targetCam != NULL && object->isA(PNode::staticClassID()) ) | 
|---|
 | 228 |   { | 
|---|
| [10482] | 229 |     this->atachTarget(dynamic_cast<Camera*>(targetCam), dynamic_cast<PNode*>( object )); | 
|---|
| [10480] | 230 |     return; | 
|---|
 | 231 |   } | 
|---|
 | 232 |  | 
|---|
 | 233 |   printf("ERROR CAMERAMANAGER: Couldn't set camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() ); | 
|---|
| [10406] | 234 | } | 
|---|
 | 235 |  | 
|---|
 | 236 |  | 
|---|
 | 237 |  | 
|---|
| [10480] | 238 | void CameraMan::jumpCam(int x, int y, int z, int camNo) | 
|---|
| [10406] | 239 | { | 
|---|
| [10480] | 240 |   cameras[camNo]->target->jump(x, y, z); | 
|---|
| [10406] | 241 | } | 
|---|
 | 242 |  | 
|---|
| [10480] | 243 |  | 
|---|
| [10483] | 244 | void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z) | 
|---|
 | 245 | { | 
|---|
 | 246 |   BaseObject* targetCam = ObjectListBase::getBaseObject("Camera", cameraName); | 
|---|
 | 247 |   if( targetCam != NULL ) | 
|---|
 | 248 |   { | 
|---|
 | 249 |     dynamic_cast<Camera*>(targetCam)->target->jump( x, y, z ); | 
|---|
 | 250 |   } | 
|---|
| [10480] | 251 |  | 
|---|
| [10483] | 252 | } | 
|---|
 | 253 |  | 
|---|
 | 254 |  | 
|---|
| [10480] | 255 | void CameraMan::setClipRegion(float nearCli, float farCli) | 
|---|
| [10424] | 256 | { | 
|---|
| [10480] | 257 |   this->nearClip=nearCli; | 
|---|
 | 258 |   this->farClip=farCli; | 
|---|
 | 259 |  | 
|---|
 | 260 |   for(unsigned int i = 0; i < this->cameras.size(); i++) | 
|---|
 | 261 |     cameras[i]->setClipRegion(nearCli, farCli); | 
|---|
| [10424] | 262 | } | 
|---|
| [10406] | 263 |  | 
|---|
 | 264 |  | 
|---|
| [10480] | 265 |  | 
|---|
| [10406] | 266 | bool CameraMan::cameraIsInVector(Camera* camera) | 
|---|
 | 267 | { | 
|---|
 | 268 |  | 
|---|
 | 269 |   for(std::vector<Camera*>::const_iterator it = cameras.begin(); it != cameras.end(); it++ ) | 
|---|
 | 270 |   { | 
|---|
 | 271 |     if( (*it) == camera) | 
|---|
 | 272 |     { | 
|---|
 | 273 |       return true; | 
|---|
 | 274 |     } | 
|---|
 | 275 |   } | 
|---|
 | 276 |   return false; | 
|---|
 | 277 |  | 
|---|
 | 278 |  | 
|---|
 | 279 | } | 
|---|
 | 280 |  | 
|---|
 | 281 |  | 
|---|
 | 282 | void CameraMan::cameraInfo() | 
|---|
 | 283 | { | 
|---|
 | 284 |   bool sameCam = (this->currentCam == State::getCamera()); | 
|---|
 | 285 |  | 
|---|
 | 286 |  | 
|---|
 | 287 |   PRINT(0)("==== CameraMan::cameraInfo ===\n"); | 
|---|
 | 288 |   PRINT(0)("=  Camera Name: %s\n", this->currentCam->getName().c_str()); | 
|---|
 | 289 |   PRINT(0)("=  Tests:\n"); | 
|---|
 | 290 |   PRINT(0)("==  State::Cam == this::Cam  %i\n", sameCam); | 
|---|
 | 291 |   PRINT(0)("==  Parenting Informations:\n"); | 
|---|
 | 292 |   this->currentCam->debugNode(10); | 
|---|
 | 293 |   PRINT(0)("==============================\n"); | 
|---|
 | 294 | } | 
|---|
 | 295 |  | 
|---|
 | 296 |  | 
|---|
 | 297 | float CameraMan::getCurrCameraCoorX() | 
|---|
 | 298 | { return this->currentCam->getAbsCoorX(); } | 
|---|
 | 299 |  | 
|---|
 | 300 | float CameraMan::getCurrCameraCoorY() | 
|---|
 | 301 | { return this->currentCam->getAbsCoorY(); } | 
|---|
 | 302 |  | 
|---|
 | 303 | float CameraMan::getCurrCameraCoorZ() | 
|---|
 | 304 | { return this->currentCam->getAbsCoorZ(); } | 
|---|
 | 305 |  | 
|---|
 | 306 |  | 
|---|
 | 307 |  | 
|---|
| [10482] | 308 | void CameraMan::atachTarget(Camera* cam ,PNode* target) | 
|---|
 | 309 | { | 
|---|
 | 310 |   cam->target->atach(target); | 
|---|
 | 311 |   cam->setViewMode(Camera::ViewNormal); | 
|---|
 | 312 |   State::setCamera( cam,  dynamic_cast<CameraTarget*>(target)); | 
|---|
 | 313 |  | 
|---|
 | 314 | } | 
|---|
 | 315 |  | 
|---|
| [10406] | 316 | //how to get a class fkt pointer | 
|---|
 | 317 |  | 
|---|
 | 318 | //BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
 | 319 |  | 
|---|
 | 320 |  | 
|---|
 | 321 |  | 
|---|
 | 322 |  | 
|---|
 | 323 |  | 
|---|
 | 324 |  | 
|---|
 | 325 |  | 
|---|
 | 326 |  | 
|---|
 | 327 |  | 
|---|
 | 328 |  | 
|---|
 | 329 |  | 
|---|
 | 330 |  | 
|---|
 | 331 |  | 
|---|
 | 332 |  | 
|---|