| 1 | /* | 
|---|
| 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)) | 
|---|
| 37 |                         ->addMethod("atachCameraToWorldEntity", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::atachCameraToWorldEntity)) | 
|---|
| 38 |                         ->addMethod("detachCurrCamera", Executor0<CameraMan, lua_State*>(&CameraMan::detachCurrCamera)) | 
|---|
| 39 |                         ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam)) | 
|---|
| 40 |                         ->addMethod("togglFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade)) | 
|---|
| 41 |                         ->addMethod("getCurrCameraCoorX", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorX)) | 
|---|
| 42 |                         ->addMethod("getCurrCameraCoorY", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorY)) | 
|---|
| 43 |                         ->addMethod("getCurrCameraCoorZ", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorZ)) | 
|---|
| 44 |                        ); | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | CameraMan::CameraMan(const TiXmlElement* root) | 
|---|
| 48 | { | 
|---|
| 49 |   this->registerObject(this, CameraMan::_objectList); | 
|---|
| 50 |  | 
|---|
| 51 |   this->nearClip = 1.0; | 
|---|
| 52 |   this->farClip = 1000.0; | 
|---|
| 53 |  | 
|---|
| 54 |   this->fadeToBlack=new BlackScreen(); | 
|---|
| 55 |  | 
|---|
| 56 |   this->setCam( State::getCamera()); | 
|---|
| 57 |  | 
|---|
| 58 |   if (root != NULL) | 
|---|
| 59 |     this->loadParams(root); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | void CameraMan::loadParams(const TiXmlElement* root) | 
|---|
| 64 | { | 
|---|
| 65 |   BaseObject::loadParams(root); | 
|---|
| 66 |   LoadParamXML(root, "Cameras", this, CameraMan, createCameras); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | void CameraMan::createCameras(const TiXmlElement* camerasTag) | 
|---|
| 71 | { | 
|---|
| 72 |  | 
|---|
| 73 |     LOAD_PARAM_START_CYCLE(camerasTag, object); | 
|---|
| 74 |     { | 
|---|
| 75 |       this->createCam(object); | 
|---|
| 76 |     } | 
|---|
| 77 |     LOAD_PARAM_END_CYCLE(object); | 
|---|
| 78 |  | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | void CameraMan::createCam(const TiXmlElement* root) | 
|---|
| 84 | { | 
|---|
| 85 |   //Camera* newCamera=new Camera(root); | 
|---|
| 86 |   this->cameras.push_back(new Camera(root)); | 
|---|
| 87 |  // cameras[cameras.size()-1]->target->detach(); | 
|---|
| 88 |   cameras[cameras.size()-1]->setClipRegion(nearClip, farClip); | 
|---|
| 89 |  | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void CameraMan::setCam(int cameraNo) | 
|---|
| 93 | { | 
|---|
| 94 |   if (cameraNo<cameras.size()) | 
|---|
| 95 |   { | 
|---|
| 96 |     this->setCam( cameras[cameraNo]); | 
|---|
| 97 |   } | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void CameraMan::setCam(const std::string& camName) | 
|---|
| 101 | { | 
|---|
| 102 |   BaseObject* object = ObjectListBase::getBaseObject("Camera", camName); | 
|---|
| 103 |  | 
|---|
| 104 |   if(object != NULL) | 
|---|
| 105 |   { | 
|---|
| 106 |     Camera* currentCam = dynamic_cast<Camera*>(object) ; | 
|---|
| 107 |  | 
|---|
| 108 |     this->setCam(currentCam); | 
|---|
| 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"); | 
|---|
| 120 |   } | 
|---|
| 121 |   this->currentCam = camera; | 
|---|
| 122 |  | 
|---|
| 123 |   State::setCamera(currentCam, currentCam->getTarget()); | 
|---|
| 124 |   OrxSound::SoundEngine::getInstance()->setListener(currentCam); | 
|---|
| 125 |  | 
|---|
| 126 |   // check if it is already added | 
|---|
| 127 |   if( ! this->cameraIsInVector(currentCam) ) | 
|---|
| 128 |     this->cameras.push_back(currentCam); | 
|---|
| 129 |  | 
|---|
| 130 |   this->fadeToBlack->setRelCoor(0., 0., 0.); | 
|---|
| 131 |   this->fadeToBlack->setParent(this->currentCam); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 | void CameraMan::moveCurrCam(int x, int y, int z) | 
|---|
| 136 | { | 
|---|
| 137 |   currentCam->target->trans(x,y,z); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | void CameraMan::moveCam(int x, int y, int z, int camNo) | 
|---|
| 142 | { | 
|---|
| 143 |   cameras[camNo]->target->trans(x,y,z); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | void CameraMan::changeTarget(int camNo,const std::string& className, const std::string& objectName) | 
|---|
| 148 | { | 
|---|
| 149 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
| 150 |   if( object != NULL && object->isA(PNode::staticClassID())) | 
|---|
| 151 |     cameras[camNo]->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | void CameraMan::changeTarget(const std::string& camName,const std::string& className, const std::string& objectName) | 
|---|
| 156 | { | 
|---|
| 157 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
| 158 |   BaseObject* newCam = ObjectListBase::getBaseObject("Camera", camName); | 
|---|
| 159 |   if( object != NULL && newCam != NULL && object->isA(PNode::staticClassID())) | 
|---|
| 160 |     dynamic_cast<Camera*>(newCam)->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName) | 
|---|
| 164 | { | 
|---|
| 165 |   BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
| 166 |   if( object != NULL && object->isA(PNode::staticClassID())) | 
|---|
| 167 |     currentCam->lookAt(dynamic_cast<PNode*>(object)); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | void CameraMan::atachCurrTarget(PNode* target) | 
|---|
| 171 | { | 
|---|
| 172 |   currentCam->target->atach(target); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | void CameraMan::atachCurrCameraToWorldEntity(const std::string& className, const std::string& targetEntity) | 
|---|
| 176 | { | 
|---|
| 177 |   BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); | 
|---|
| 178 |  | 
|---|
| 179 |   if(object != NULL) | 
|---|
| 180 |   { | 
|---|
| 181 |    this->atachCurrTarget(dynamic_cast<PNode*>(object)); | 
|---|
| 182 |    return; | 
|---|
| 183 |   } | 
|---|
| 184 |  | 
|---|
| 185 | printf("ERROR CAMERAMANAGER: Couldn't set camera to: %s %s \n", className.c_str(),targetEntity.c_str() ); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 | void CameraMan::atachCameraToWorldEntity(const std::string& cameraName, const std::string& className, const std::string& targetEntity) | 
|---|
| 190 | { | 
|---|
| 191 |   BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); | 
|---|
| 192 |   BaseObject* targetCam = ObjectListBase::getBaseObject("Camera", cameraName); | 
|---|
| 193 |  | 
|---|
| 194 |   if(object != NULL && targetCam != NULL) | 
|---|
| 195 |   { | 
|---|
| 196 |    dynamic_cast<Camera*>(targetCam)->target->atach(dynamic_cast<PNode*>(object)); | 
|---|
| 197 |    return; | 
|---|
| 198 |   } | 
|---|
| 199 |  | 
|---|
| 200 | printf("ERROR CAMERAMANAGER: Couldn't set camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() ); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | void CameraMan::detachCurrCamera() | 
|---|
| 205 | { | 
|---|
| 206 |   currentCam->target->detach(); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | void CameraMan::jumpCam(int x, int y, int z, int camNo) | 
|---|
| 210 | { | 
|---|
| 211 |   cameras[camNo]->target->jump(x, y, z); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 216 | void CameraMan::setClipRegion(float nearCli, float farCli) | 
|---|
| 217 | { | 
|---|
| 218 |   this->nearClip=nearCli; | 
|---|
| 219 |   this->farClip=farCli; | 
|---|
| 220 |  | 
|---|
| 221 |   for(int i = 0; i < this->cameras.size(); i++) | 
|---|
| 222 |     cameras[i]->setClipRegion(nearCli, farCli); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 |  | 
|---|
| 226 | void CameraMan::jumpCurrCam(int x, int y, int z) | 
|---|
| 227 | { | 
|---|
| 228 |   currentCam->target->jump(x, y, z); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 |  | 
|---|
| 232 |  | 
|---|
| 233 |  | 
|---|
| 234 | void CameraMan::togglFade() | 
|---|
| 235 | { | 
|---|
| 236 |   if( this->fadeToBlack) | 
|---|
| 237 |     fadeToBlack->toggleFade(); | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | bool CameraMan::cameraIsInVector(Camera* camera) | 
|---|
| 243 | { | 
|---|
| 244 |  | 
|---|
| 245 |   for(std::vector<Camera*>::const_iterator it = cameras.begin(); it != cameras.end(); it++ ) | 
|---|
| 246 |   { | 
|---|
| 247 |     if( (*it) == camera) | 
|---|
| 248 |     { | 
|---|
| 249 |       return true; | 
|---|
| 250 |     } | 
|---|
| 251 |   } | 
|---|
| 252 |   return false; | 
|---|
| 253 |  | 
|---|
| 254 |  | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 | void CameraMan::cameraInfo() | 
|---|
| 259 | { | 
|---|
| 260 |   bool sameCam = (this->currentCam == State::getCamera()); | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 |   PRINT(0)("==== CameraMan::cameraInfo ===\n"); | 
|---|
| 264 |   PRINT(0)("=  Camera Name: %s\n", this->currentCam->getName().c_str()); | 
|---|
| 265 |   PRINT(0)("=  Tests:\n"); | 
|---|
| 266 |   PRINT(0)("==  State::Cam == this::Cam  %i\n", sameCam); | 
|---|
| 267 |   PRINT(0)("==  Parenting Informations:\n"); | 
|---|
| 268 |   this->currentCam->debugNode(10); | 
|---|
| 269 |   PRINT(0)("==============================\n"); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 |  | 
|---|
| 273 | float CameraMan::getCurrCameraCoorX() | 
|---|
| 274 | { return this->currentCam->getAbsCoorX(); } | 
|---|
| 275 |  | 
|---|
| 276 | float CameraMan::getCurrCameraCoorY() | 
|---|
| 277 | { return this->currentCam->getAbsCoorY(); } | 
|---|
| 278 |  | 
|---|
| 279 | float CameraMan::getCurrCameraCoorZ() | 
|---|
| 280 | { return this->currentCam->getAbsCoorZ(); } | 
|---|
| 281 |  | 
|---|
| 282 |  | 
|---|
| 283 |  | 
|---|
| 284 | //how to get a class fkt pointer | 
|---|
| 285 |  | 
|---|
| 286 | //BaseObject* object = ObjectListBase::getBaseObject(className, objectName); | 
|---|
| 287 |  | 
|---|
| 288 |  | 
|---|
| 289 |  | 
|---|
| 290 |  | 
|---|
| 291 |  | 
|---|
| 292 |  | 
|---|
| 293 |  | 
|---|
| 294 |  | 
|---|
| 295 |  | 
|---|
| 296 |  | 
|---|
| 297 |  | 
|---|
| 298 |  | 
|---|
| 299 |  | 
|---|
| 300 |  | 
|---|