[10593] | 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 "cameraman.h" |
---|
[10593] | 17 | #include "blackscreen.h" |
---|
| 18 | #include "loading/load_param_xml.h" |
---|
[10406] | 19 | #include "sound_engine.h" |
---|
| 20 | #include "script_class.h" |
---|
| 21 | |
---|
| 22 | ObjectListDefinition(CameraMan); |
---|
| 23 | |
---|
| 24 | CREATE_SCRIPTABLE_CLASS(CameraMan, |
---|
[10593] | 25 | /// Camera management |
---|
| 26 | addMethod("changeTarget", Executor3<CameraMan, lua_State*, const std::string&, const std::string&,const std::string&>(&CameraMan::changeTarget)) |
---|
| 27 | ->addMethod("attachCamera", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::attachCamera)) |
---|
| 28 | ->addMethod("jumpCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::jumpCam)) |
---|
| 29 | ->addMethod("moveCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::moveCam)) |
---|
| 30 | ->addMethod("setRelCoor", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::setRelCameraCoor)) |
---|
| 31 | ->addMethod("setRelCoorSoft", Executor5<CameraMan, lua_State*,const std::string&,float,float,float,float>(&CameraMan::setRelCameraCoorSoft)) |
---|
| 32 | ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam)) |
---|
| 33 | /// Current Camera |
---|
| 34 | ->addMethod("changeCurrTarget", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::changeCurrTarget)) |
---|
| 35 | ->addMethod("attachCurrCamera", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::attachCurrCamera)) |
---|
| 36 | ->addMethod("jumpCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::jumpCurrCam)) |
---|
| 37 | ->addMethod("moveCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::moveCurrCam)) |
---|
| 38 | /// Fading |
---|
[10428] | 39 | ->addMethod("toggleFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade)) |
---|
[10424] | 40 | ->addMethod("initFadeBlack", Executor0<CameraMan, lua_State*>(&CameraMan::initFadeBlack)) |
---|
[10593] | 41 | /// Polling |
---|
[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)) |
---|
| 45 | ); |
---|
| 46 | |
---|
| 47 | |
---|
[10593] | 48 | |
---|
| 49 | /** |
---|
| 50 | * constructor of the CameraManager |
---|
| 51 | */ |
---|
[10406] | 52 | CameraMan::CameraMan(const TiXmlElement* root) |
---|
| 53 | { |
---|
| 54 | this->registerObject(this, CameraMan::_objectList); |
---|
| 55 | |
---|
| 56 | this->nearClip = 1.0; |
---|
| 57 | this->farClip = 1000.0; |
---|
| 58 | |
---|
| 59 | this->fadeToBlack=new BlackScreen(); |
---|
| 60 | |
---|
[10593] | 61 | this->setCam( State::getCamera() ); |
---|
[10406] | 62 | |
---|
| 63 | if (root != NULL) |
---|
| 64 | this->loadParams(root); |
---|
| 65 | } |
---|
| 66 | |
---|
[10593] | 67 | /** |
---|
| 68 | * Loads the parameters of the CameraManager from the xml file |
---|
| 69 | * @param root reference to the xml root element |
---|
| 70 | */ |
---|
[10406] | 71 | void CameraMan::loadParams(const TiXmlElement* root) |
---|
| 72 | { |
---|
| 73 | BaseObject::loadParams(root); |
---|
[10414] | 74 | LoadParamXML(root, "Cameras", this, CameraMan, createCameras); |
---|
| 75 | } |
---|
| 76 | |
---|
[10593] | 77 | /** |
---|
| 78 | * Creates all the Cameras |
---|
| 79 | * @param root reference to the xml root element |
---|
| 80 | */ |
---|
[10414] | 81 | void CameraMan::createCameras(const TiXmlElement* camerasTag) |
---|
| 82 | { |
---|
[10411] | 83 | LOAD_PARAM_START_CYCLE(camerasTag, object); |
---|
| 84 | { |
---|
| 85 | this->createCam(object); |
---|
| 86 | } |
---|
| 87 | LOAD_PARAM_END_CYCLE(object); |
---|
[10406] | 88 | } |
---|
| 89 | |
---|
[10593] | 90 | /** |
---|
| 91 | * Creates a Camera from a xml node |
---|
| 92 | * @param root reference to the xml node |
---|
| 93 | */ |
---|
[10406] | 94 | void CameraMan::createCam(const TiXmlElement* root) |
---|
| 95 | { |
---|
[10593] | 96 | Camera* newCam = new Camera(root); |
---|
| 97 | newCam->setClipRegion(nearClip, farClip); |
---|
[10406] | 98 | } |
---|
| 99 | |
---|
[10593] | 100 | /** |
---|
| 101 | * Changes the target of a camera. (where the camera looks at) |
---|
| 102 | * |
---|
| 103 | * @param cameraName the name of the camera whose target is changed |
---|
| 104 | * @param className the class of the new target |
---|
| 105 | * @param objectName the name of the new target |
---|
| 106 | */ |
---|
| 107 | void CameraMan::changeTarget(const std::string& cameraName,const std::string& className, const std::string& objectName) |
---|
[10495] | 108 | { |
---|
[10593] | 109 | BaseObject* object = ObjectListBase::getBaseObject(className, objectName); |
---|
| 110 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
[10480] | 111 | |
---|
[10593] | 112 | if( object != NULL && camera != NULL && object->isA(PNode::staticClassID())) |
---|
[10406] | 113 | { |
---|
[10593] | 114 | camera->lookAt(dynamic_cast<PNode*>(object)); |
---|
| 115 | State::setCamera( camera, dynamic_cast<CameraTarget*>(object)); |
---|
| 116 | return; |
---|
[10406] | 117 | } |
---|
| 118 | |
---|
[10593] | 119 | printf("ERROR CAMERAMANAGER: Couldn't change target for camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),objectName.c_str() ); |
---|
[10406] | 120 | |
---|
| 121 | } |
---|
| 122 | |
---|
[10593] | 123 | /** |
---|
| 124 | * Attaches a camera to a new pnode. (the camera follows the pnode) |
---|
| 125 | * |
---|
| 126 | * @param cameraName the name of the camera |
---|
| 127 | * @param className the class of the new parent node |
---|
| 128 | * @param objectName the name of the new parent node |
---|
| 129 | */ |
---|
| 130 | void CameraMan::attachCamera(const std::string& cameraName, const std::string& className, const std::string& targetEntity) |
---|
| 131 | { |
---|
| 132 | BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity); |
---|
| 133 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
[10406] | 134 | |
---|
[10593] | 135 | if( object != NULL && camera != NULL && object->isA(PNode::staticClassID()) ) |
---|
[10406] | 136 | { |
---|
[10593] | 137 | camera->target->atach(dynamic_cast<PNode*>(object)); |
---|
| 138 | camera->setViewMode(Camera::ViewNormal); |
---|
| 139 | State::setCamera( camera, dynamic_cast<CameraTarget*>(object)); |
---|
[10480] | 140 | return; |
---|
[10406] | 141 | } |
---|
| 142 | |
---|
[10593] | 143 | printf("ERROR CAMERAMANAGER: Couldn't attach camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() ); |
---|
[10406] | 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[10593] | 148 | /** |
---|
| 149 | * Sets a camera to a new position |
---|
| 150 | * |
---|
| 151 | * @param cameraName the name of the camera |
---|
| 152 | * @param x the new x coordinate |
---|
| 153 | * @param y the new y coordinate |
---|
| 154 | * @param z the new z coordinate |
---|
| 155 | */ |
---|
| 156 | void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z) |
---|
[10406] | 157 | { |
---|
[10593] | 158 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
| 159 | if( camera != NULL ) |
---|
[10463] | 160 | { |
---|
[10593] | 161 | camera->target->jump( x, y, z ); |
---|
[10463] | 162 | } |
---|
[10406] | 163 | } |
---|
| 164 | |
---|
[10593] | 165 | /** |
---|
| 166 | * Moves a camera slowly to a new position |
---|
| 167 | * |
---|
| 168 | * @param cameraName the name of the camera |
---|
| 169 | * @param x the new x coordinate |
---|
| 170 | * @param y the new y coordinate |
---|
| 171 | * @param z the new z coordinate |
---|
| 172 | */ |
---|
| 173 | void CameraMan::moveCam(const std::string& cameraName, float x, float y, float z) |
---|
[10406] | 174 | { |
---|
[10593] | 175 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
| 176 | if( camera != NULL ) |
---|
[10408] | 177 | { |
---|
[10593] | 178 | camera->target->trans( x, y, z ); |
---|
[10408] | 179 | } |
---|
[10406] | 180 | } |
---|
| 181 | |
---|
[10593] | 182 | /** |
---|
| 183 | * Sets the coordinate of a camera relative to its target. |
---|
| 184 | * |
---|
| 185 | * @param cameraName the name of the camera |
---|
| 186 | * @param x the new x coordinate |
---|
| 187 | * @param y the new y coordinate |
---|
| 188 | * @param z the new z coordinate |
---|
| 189 | */ |
---|
| 190 | void CameraMan::setRelCameraCoor(const std::string& cameraName, float x, float y, float z) |
---|
[10416] | 191 | { |
---|
[10593] | 192 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
| 193 | camera->setRelCoor(x,y,z); |
---|
| 194 | camera->target->setRelCoor(0,0,0); |
---|
[10480] | 195 | } |
---|
[10416] | 196 | |
---|
[10593] | 197 | /** |
---|
| 198 | * Sets the coordinate of a camera relative to its target. (softly) |
---|
| 199 | * |
---|
| 200 | * @param cameraName the name of the camera |
---|
| 201 | * @param x the new x coordinate |
---|
| 202 | * @param y the new y coordinate |
---|
| 203 | * @param z the new z coordinate |
---|
| 204 | */ |
---|
| 205 | void CameraMan::setRelCameraCoorSoft(const std::string& cameraName, float x, float y, float z, float bias) |
---|
[10480] | 206 | { |
---|
[10593] | 207 | Camera* newCam = Camera::objectList().getObject(cameraName); |
---|
| 208 | newCam->setRelCoor(x,y,z); |
---|
| 209 | newCam->target->setRelCoorSoft(0,0,0); |
---|
[10416] | 210 | } |
---|
| 211 | |
---|
[10593] | 212 | /** |
---|
| 213 | * Sets a new camera as the current one. |
---|
| 214 | * |
---|
| 215 | * @param cameraName the name of the new camera |
---|
| 216 | */ |
---|
| 217 | void CameraMan::setCam(const std::string& cameraName) |
---|
[10480] | 218 | { |
---|
[10593] | 219 | Camera* camera = Camera::objectList().getObject(cameraName); |
---|
[10416] | 220 | |
---|
[10593] | 221 | if(camera != NULL) |
---|
| 222 | { |
---|
| 223 | this->setCam(camera); |
---|
| 224 | return; |
---|
| 225 | } |
---|
| 226 | printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", cameraName.c_str()); |
---|
[10413] | 227 | } |
---|
[10406] | 228 | |
---|
[10593] | 229 | /** |
---|
| 230 | * Sets a new camera as the current one. |
---|
| 231 | * |
---|
| 232 | * @param camera a pointer to the new camera |
---|
| 233 | */ |
---|
| 234 | void CameraMan::setCam(Camera* newCamera) |
---|
[10406] | 235 | { |
---|
[10593] | 236 | if( newCamera == NULL) |
---|
[10480] | 237 | { |
---|
[10593] | 238 | PRINTF(0)("trying to add a zero camera! uiuiui!\n"); |
---|
| 239 | return; |
---|
[10480] | 240 | } |
---|
[10593] | 241 | |
---|
| 242 | State::setCamera(newCamera, newCamera->getTarget()); |
---|
| 243 | OrxSound::SoundEngine::getInstance()->setListener(newCamera); |
---|
[10406] | 244 | |
---|
[10593] | 245 | this->fadeToBlack->setRelCoor(0., 0., 0.); |
---|
| 246 | this->fadeToBlack->setParent(newCamera); |
---|
[10480] | 247 | |
---|
[10406] | 248 | } |
---|
| 249 | |
---|
[10593] | 250 | /** |
---|
| 251 | * Sets the clipregion of all the cameras |
---|
| 252 | * |
---|
| 253 | * @param nearClip the new near clip |
---|
| 254 | * @param nearClip the new far clip |
---|
| 255 | */ |
---|
| 256 | void CameraMan::setClipRegion(float nearClip, float farClip) |
---|
[10406] | 257 | { |
---|
[10593] | 258 | this->nearClip=nearClip; |
---|
| 259 | this->farClip=farClip; |
---|
[10480] | 260 | |
---|
[10593] | 261 | for (ObjectList<Camera>::const_iterator it = Camera::objectList().begin(); |
---|
| 262 | it != Camera::objectList().end(); |
---|
| 263 | ++it) |
---|
[10480] | 264 | { |
---|
[10593] | 265 | (*it)->setClipRegion(this->nearClip, this->farClip); |
---|
[10480] | 266 | } |
---|
[10406] | 267 | } |
---|
| 268 | |
---|
[10593] | 269 | /** |
---|
| 270 | * Changes the target of the current camera. (where the camera looks at) |
---|
| 271 | * |
---|
| 272 | * @param className the class of the new target |
---|
| 273 | * @param objectName the name of the new target |
---|
| 274 | */ |
---|
| 275 | void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName) |
---|
[10406] | 276 | { |
---|
[10593] | 277 | this->changeTarget( (State::getCamera())->getName() , className, objectName); |
---|
[10406] | 278 | } |
---|
| 279 | |
---|
[10593] | 280 | /** |
---|
| 281 | * Attaches the current camera to a new pnode. (the camera follows the pnode) |
---|
| 282 | * |
---|
| 283 | * @param className the class of the new parent node |
---|
| 284 | * @param objectName the name of the new parent node |
---|
| 285 | */ |
---|
| 286 | void CameraMan::attachCurrCamera(const std::string& className, const std::string& targetEntity) |
---|
[10483] | 287 | { |
---|
[10593] | 288 | this->attachCamera( (State::getCamera())->getName(), className, targetEntity); |
---|
[10483] | 289 | } |
---|
| 290 | |
---|
[10593] | 291 | /** |
---|
| 292 | * Sets the current camera to a new position |
---|
| 293 | * |
---|
| 294 | * @param x the new x coordinate |
---|
| 295 | * @param y the new y coordinate |
---|
| 296 | * @param z the new z coordinate |
---|
| 297 | */ |
---|
| 298 | void CameraMan::jumpCurrCam(float x, float y, float z) |
---|
[10424] | 299 | { |
---|
[10593] | 300 | this->jumpCam((State::getCamera())->getName(), x, y, z); |
---|
[10424] | 301 | } |
---|
[10406] | 302 | |
---|
[10593] | 303 | /** |
---|
| 304 | * Moves the current camera slowly to a new position |
---|
| 305 | * |
---|
| 306 | * @param x the new x coordinate |
---|
| 307 | * @param y the new y coordinate |
---|
| 308 | * @param z the new z coordinate |
---|
| 309 | */ |
---|
| 310 | void CameraMan::moveCurrCam(float x, float y, float z) |
---|
[10406] | 311 | { |
---|
[10593] | 312 | this->moveCam( (State::getCamera())->getName(), x, y, z); |
---|
[10406] | 313 | } |
---|
| 314 | |
---|
[10593] | 315 | /** |
---|
| 316 | * Toggles the fadestate (from in to out and via versa) |
---|
| 317 | */ |
---|
| 318 | void CameraMan::togglFade() |
---|
[10406] | 319 | { |
---|
[10593] | 320 | if( this->fadeToBlack) |
---|
| 321 | fadeToBlack->toggleFade(); |
---|
[10406] | 322 | } |
---|
| 323 | |
---|
[10593] | 324 | /** |
---|
| 325 | * Sets the fadestate immediately to black (fade out) |
---|
| 326 | */ |
---|
| 327 | void CameraMan::initFadeBlack() |
---|
[10482] | 328 | { |
---|
[10593] | 329 | if( this->fadeToBlack) |
---|
| 330 | fadeToBlack->initFadeBlack(); |
---|
[10482] | 331 | } |
---|