[2036] | 1 | |
---|
| 2 | |
---|
[4570] | 3 | /* |
---|
[2036] | 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
[2190] | 15 | co-programmer: Christian Meyer |
---|
[2036] | 16 | */ |
---|
[5300] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
[2036] | 18 | |
---|
| 19 | #include "world_entity.h" |
---|
[5208] | 20 | #include "shell_command.h" |
---|
[5143] | 21 | |
---|
[5511] | 22 | #include "model.h" |
---|
[8490] | 23 | #include "md2/md2Model.h" |
---|
| 24 | #include "md3/md3_model.h" |
---|
| 25 | |
---|
[7193] | 26 | #include "util/loading/resource_manager.h" |
---|
| 27 | #include "util/loading/load_param.h" |
---|
[3803] | 28 | #include "vector.h" |
---|
[4682] | 29 | #include "obb_tree.h" |
---|
[3608] | 30 | |
---|
[6430] | 31 | #include "glgui_bar.h" |
---|
| 32 | |
---|
[6002] | 33 | #include "state.h" |
---|
[7014] | 34 | #include "camera.h" |
---|
[6002] | 35 | |
---|
[7927] | 36 | #include "collision_handle.h" |
---|
[8190] | 37 | #include "collision_event.h" |
---|
[7927] | 38 | |
---|
[8190] | 39 | #include <stdarg.h> |
---|
[7927] | 40 | |
---|
[8190] | 41 | |
---|
[2036] | 42 | using namespace std; |
---|
| 43 | |
---|
[5208] | 44 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
[6430] | 45 | ->describe("sets the Model of the WorldEntity") |
---|
[7711] | 46 | ->defaultValues("models/ships/fighter.obj", 1.0f); |
---|
[5208] | 47 | |
---|
[6424] | 48 | SHELL_COMMAND(debugEntity, WorldEntity, debugWE); |
---|
[5208] | 49 | |
---|
[2043] | 50 | /** |
---|
[4836] | 51 | * Loads the WordEntity-specific Part of any derived Class |
---|
[5498] | 52 | * |
---|
| 53 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
| 54 | * that can calls WorldEntities loadParams for itself. |
---|
| 55 | */ |
---|
[6430] | 56 | WorldEntity::WorldEntity() |
---|
| 57 | : Synchronizeable() |
---|
[2190] | 58 | { |
---|
[4320] | 59 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
[4597] | 60 | |
---|
[4682] | 61 | this->obbTree = NULL; |
---|
[6700] | 62 | this->healthWidget = NULL; |
---|
| 63 | this->healthMax = 1.0f; |
---|
| 64 | this->health = 1.0f; |
---|
[8190] | 65 | this->damage = 0.0f; // no damage dealt by a default entity |
---|
[6695] | 66 | this->scaling = 1.0f; |
---|
[4261] | 67 | |
---|
[6695] | 68 | /* OSOLETE */ |
---|
| 69 | this->bVisible = true; |
---|
| 70 | this->bCollide = true; |
---|
| 71 | |
---|
[6142] | 72 | this->objectListNumber = OM_INIT; |
---|
| 73 | this->objectListIterator = NULL; |
---|
| 74 | |
---|
[8190] | 75 | // reset all collision handles to NULL == unsubscribed state |
---|
| 76 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 77 | this->collisionHandles[i] = NULL; |
---|
| 78 | this->bReactive = false; |
---|
| 79 | |
---|
| 80 | // registering default reactions: |
---|
[8490] | 81 | // this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, CL_WORLD_ENTITY); |
---|
[8190] | 82 | |
---|
[6142] | 83 | this->toList(OM_NULL); |
---|
[7927] | 84 | |
---|
[7954] | 85 | modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) ); |
---|
| 86 | scaling_handle = registerVarId( new SynchronizeableFloat( &scaling, &scaling, "scaling" ) ); |
---|
[2190] | 87 | } |
---|
[2043] | 88 | |
---|
| 89 | /** |
---|
[4836] | 90 | * standard destructor |
---|
[2043] | 91 | */ |
---|
[2190] | 92 | WorldEntity::~WorldEntity () |
---|
[2036] | 93 | { |
---|
[7125] | 94 | State::getObjectManager()->toList(this, OM_INIT); |
---|
| 95 | |
---|
| 96 | // Delete the model (unregister it with the ResourceManager) |
---|
| 97 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
| 98 | this->setModel(NULL, i); |
---|
| 99 | |
---|
[5498] | 100 | // Delete the obbTree |
---|
[5302] | 101 | if( this->obbTree != NULL) |
---|
[4814] | 102 | delete this->obbTree; |
---|
[5994] | 103 | |
---|
[6700] | 104 | if (this->healthWidget != NULL) |
---|
| 105 | delete this->healthWidget; |
---|
[8190] | 106 | |
---|
| 107 | this->unsubscribeReaction(); |
---|
[3531] | 108 | } |
---|
| 109 | |
---|
[5498] | 110 | /** |
---|
| 111 | * loads the WorldEntity Specific Parameters. |
---|
| 112 | * @param root: the XML-Element to load the Data From |
---|
| 113 | */ |
---|
[4436] | 114 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
| 115 | { |
---|
[5498] | 116 | // Do the PNode loading stuff |
---|
[6512] | 117 | PNode::loadParams(root); |
---|
[5498] | 118 | |
---|
[6222] | 119 | LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture) |
---|
[6430] | 120 | .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
[7198] | 121 | .defaultValues(""); |
---|
[6222] | 122 | |
---|
[4436] | 123 | // Model Loading |
---|
[5671] | 124 | LoadParam(root, "model", this, WorldEntity, loadModel) |
---|
[6430] | 125 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
[7198] | 126 | .defaultValues("", 1.0f, 0); |
---|
[6430] | 127 | |
---|
[6700] | 128 | LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax) |
---|
| 129 | .describe("The Maximum health that can be loaded onto this entity") |
---|
[7198] | 130 | .defaultValues(1.0f); |
---|
[6430] | 131 | |
---|
[6700] | 132 | LoadParam(root, "health", this, WorldEntity, setHealth) |
---|
| 133 | .describe("The Health the WorldEntity has at this moment") |
---|
[7198] | 134 | .defaultValues(1.0f); |
---|
[4436] | 135 | } |
---|
| 136 | |
---|
[6222] | 137 | |
---|
[3531] | 138 | /** |
---|
[4885] | 139 | * loads a Model onto a WorldEntity |
---|
[4836] | 140 | * @param fileName the name of the model to load |
---|
[5057] | 141 | * @param scaling the Scaling of the model |
---|
[7711] | 142 | * |
---|
| 143 | * FIXME |
---|
| 144 | * @todo: separate the obb tree generation from the model |
---|
[7221] | 145 | */ |
---|
[7711] | 146 | void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber, unsigned int obbTreeDepth) |
---|
[4261] | 147 | { |
---|
[6695] | 148 | this->modelLODName = fileName; |
---|
[6424] | 149 | this->scaling = scaling; |
---|
[7954] | 150 | |
---|
| 151 | std::string name = fileName; |
---|
| 152 | |
---|
| 153 | if ( name.find( ResourceManager::getInstance()->getDataDir() ) == 0 ) |
---|
| 154 | { |
---|
| 155 | name.erase(ResourceManager::getInstance()->getDataDir().size()); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | this->modelFileName = name; |
---|
| 159 | |
---|
[7221] | 160 | if (!fileName.empty()) |
---|
[6142] | 161 | { |
---|
[6430] | 162 | // search for the special character # in the LoadParam |
---|
[7221] | 163 | if (fileName.find('#') != std::string::npos) |
---|
[6222] | 164 | { |
---|
[7221] | 165 | PRINTF(4)("Found # in %s... searching for LOD's\n", fileName.c_str()); |
---|
| 166 | std::string lodFile = fileName; |
---|
| 167 | unsigned int offset = lodFile.find('#'); |
---|
[6720] | 168 | for (unsigned int i = 0; i < 3; i++) |
---|
[6005] | 169 | { |
---|
[7221] | 170 | lodFile[offset] = 48+(int)i; |
---|
[6222] | 171 | if (ResourceManager::isInDataDir(lodFile)) |
---|
| 172 | this->loadModel(lodFile, scaling, i); |
---|
[6005] | 173 | } |
---|
[6222] | 174 | return; |
---|
| 175 | } |
---|
[6720] | 176 | if (this->scaling <= 0.0) |
---|
[6424] | 177 | { |
---|
[7193] | 178 | PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n"); |
---|
[6720] | 179 | this->scaling = 1.0; |
---|
[6424] | 180 | } |
---|
[7221] | 181 | if(fileName.find(".obj") != std::string::npos) |
---|
[6222] | 182 | { |
---|
[7221] | 183 | PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str()); |
---|
[7193] | 184 | BaseObject* loadedModel = ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, this->scaling); |
---|
| 185 | if (loadedModel != NULL) |
---|
| 186 | this->setModel(dynamic_cast<Model*>(loadedModel), modelNumber); |
---|
[7221] | 187 | else |
---|
| 188 | PRINTF(1)("OBJ-File %s not found.\n", fileName.c_str()); |
---|
[6222] | 189 | |
---|
| 190 | if( modelNumber == 0) |
---|
[7711] | 191 | this->buildObbTree(obbTreeDepth); |
---|
[6222] | 192 | } |
---|
[7221] | 193 | else if(fileName.find(".md2") != std::string::npos) |
---|
[6222] | 194 | { |
---|
[7221] | 195 | PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str()); |
---|
[7055] | 196 | Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling); |
---|
[6430] | 197 | //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0); |
---|
[6222] | 198 | this->setModel(m, 0); |
---|
[7068] | 199 | |
---|
| 200 | if( m != NULL) |
---|
[7711] | 201 | this->buildObbTree(obbTreeDepth); |
---|
[6222] | 202 | } |
---|
[8490] | 203 | else if(fileName.find(".md3") != std::string::npos) |
---|
| 204 | { |
---|
| 205 | PRINTF(4)("fetching MD3 file: %s\n", fileName.c_str()); |
---|
| 206 | Model* m = new md3::MD3Model(fileName, this->scaling); |
---|
| 207 | this->setModel(m, 0); |
---|
| 208 | |
---|
| 209 | // if( m != NULL) |
---|
| 210 | // this->buildObbTree(obbTreeDepth); |
---|
| 211 | } |
---|
[4732] | 212 | } |
---|
| 213 | else |
---|
[6341] | 214 | { |
---|
[5995] | 215 | this->setModel(NULL); |
---|
[6341] | 216 | } |
---|
[4261] | 217 | } |
---|
| 218 | |
---|
[5061] | 219 | /** |
---|
[5994] | 220 | * sets a specific Model for the Object. |
---|
| 221 | * @param model The Model to set |
---|
| 222 | * @param modelNumber the n'th model in the List to get. |
---|
| 223 | */ |
---|
| 224 | void WorldEntity::setModel(Model* model, unsigned int modelNumber) |
---|
| 225 | { |
---|
[5995] | 226 | if (this->models.size() <= modelNumber) |
---|
| 227 | this->models.resize(modelNumber+1, NULL); |
---|
| 228 | |
---|
| 229 | if (this->models[modelNumber] != NULL) |
---|
[6004] | 230 | { |
---|
[7193] | 231 | Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(dynamic_cast<BaseObject*>(this->models[modelNumber])); |
---|
[7123] | 232 | if (resource != NULL) |
---|
| 233 | ResourceManager::getInstance()->unload(resource, RP_LEVEL); |
---|
| 234 | else |
---|
| 235 | { |
---|
| 236 | PRINTF(4)("Forcing model deletion\n"); |
---|
| 237 | delete this->models[modelNumber]; |
---|
| 238 | } |
---|
[5994] | 239 | } |
---|
[6222] | 240 | |
---|
[5995] | 241 | this->models[modelNumber] = model; |
---|
[5994] | 242 | |
---|
[6222] | 243 | |
---|
[6430] | 244 | // if (this->model != NULL) |
---|
| 245 | // this->buildObbTree(4); |
---|
[5994] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | /** |
---|
[5061] | 250 | * builds the obb-tree |
---|
| 251 | * @param depth the depth to calculate |
---|
| 252 | */ |
---|
[7711] | 253 | bool WorldEntity::buildObbTree(int depth) |
---|
[5061] | 254 | { |
---|
[5428] | 255 | if (this->obbTree) |
---|
| 256 | delete this->obbTree; |
---|
| 257 | |
---|
[5995] | 258 | if (this->models[0] != NULL) |
---|
[5428] | 259 | { |
---|
[7711] | 260 | this->obbTree = new OBBTree(depth, models[0]->getModelInfo(), this); |
---|
[5428] | 261 | return true; |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
[7711] | 265 | PRINTF(1)("could not create obb-tree, because no model was loaded yet\n"); |
---|
[5428] | 266 | this->obbTree = NULL; |
---|
| 267 | return false; |
---|
| 268 | } |
---|
[5061] | 269 | } |
---|
[5057] | 270 | |
---|
[7927] | 271 | |
---|
[6142] | 272 | /** |
---|
[7927] | 273 | * subscribes this world entity to a collision reaction |
---|
| 274 | * @param type the type of reaction to subscribe to |
---|
[8190] | 275 | * @param target1 a filter target (classID) |
---|
| 276 | */ |
---|
| 277 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1) |
---|
| 278 | { |
---|
| 279 | this->subscribeReaction(type); |
---|
| 280 | |
---|
| 281 | // add the target filter |
---|
| 282 | this->collisionHandles[type]->addTarget(target1); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * subscribes this world entity to a collision reaction |
---|
| 288 | * @param type the type of reaction to subscribe to |
---|
| 289 | * @param target1 a filter target (classID) |
---|
| 290 | */ |
---|
| 291 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2) |
---|
| 292 | { |
---|
| 293 | this->subscribeReaction(type); |
---|
| 294 | |
---|
| 295 | // add the target filter |
---|
| 296 | this->collisionHandles[type]->addTarget(target1); |
---|
| 297 | this->collisionHandles[type]->addTarget(target2); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * subscribes this world entity to a collision reaction |
---|
| 303 | * @param type the type of reaction to subscribe to |
---|
| 304 | * @param target1 a filter target (classID) |
---|
| 305 | */ |
---|
| 306 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3) |
---|
| 307 | { |
---|
| 308 | this->subscribeReaction(type); |
---|
| 309 | |
---|
| 310 | // add the target filter |
---|
| 311 | this->collisionHandles[type]->addTarget(target1); |
---|
| 312 | this->collisionHandles[type]->addTarget(target2); |
---|
| 313 | this->collisionHandles[type]->addTarget(target3); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * subscribes this world entity to a collision reaction |
---|
| 319 | * @param type the type of reaction to subscribe to |
---|
| 320 | * @param target1 a filter target (classID) |
---|
| 321 | */ |
---|
| 322 | void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3, long target4) |
---|
| 323 | { |
---|
| 324 | this->subscribeReaction(type); |
---|
| 325 | |
---|
| 326 | // add the target filter |
---|
| 327 | this->collisionHandles[type]->addTarget(target1); |
---|
| 328 | this->collisionHandles[type]->addTarget(target2); |
---|
| 329 | this->collisionHandles[type]->addTarget(target3); |
---|
| 330 | this->collisionHandles[type]->addTarget(target4); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | * subscribes this world entity to a collision reaction |
---|
| 336 | * @param type the type of reaction to subscribe to |
---|
[7927] | 337 | * @param nrOfTargets number of target filters |
---|
| 338 | * @param ... the targets as classIDs |
---|
| 339 | */ |
---|
[8190] | 340 | void WorldEntity::subscribeReaction(CREngine::CRType type) |
---|
| 341 | { |
---|
| 342 | if( this->collisionHandles[type] != NULL) { |
---|
| 343 | PRINTF(2)("Registering for a CollisionReaction already subscribed to! Skipping\n"); |
---|
| 344 | return; |
---|
| 345 | } |
---|
[7927] | 346 | |
---|
[8190] | 347 | this->collisionHandles[type] = CREngine::getInstance()->subscribeReaction(this, type); |
---|
[7927] | 348 | |
---|
[8190] | 349 | // now there is at least one collision reaction subscribed |
---|
| 350 | this->bReactive = true; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | |
---|
[7927] | 354 | /** |
---|
[8190] | 355 | * unsubscribes a specific reaction from the worldentity |
---|
| 356 | * @param type the reaction to unsubscribe |
---|
| 357 | */ |
---|
| 358 | void WorldEntity::unsubscribeReaction(CREngine::CRType type) |
---|
| 359 | { |
---|
| 360 | if( this->collisionHandles[type] == NULL) |
---|
| 361 | return; |
---|
| 362 | |
---|
| 363 | CREngine::getInstance()->unsubscribeReaction(this->collisionHandles[type]); |
---|
| 364 | this->collisionHandles[type] = NULL; |
---|
| 365 | |
---|
| 366 | // check if there is still any handler registered |
---|
| 367 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 368 | { |
---|
| 369 | if( this->collisionHandles[i] != NULL) |
---|
| 370 | { |
---|
| 371 | this->bReactive = true; |
---|
| 372 | return; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | this->bReactive = false; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | |
---|
| 379 | /** |
---|
| 380 | * unsubscribes all collision reactions |
---|
| 381 | */ |
---|
| 382 | void WorldEntity::unsubscribeReaction() |
---|
| 383 | { |
---|
| 384 | for( int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
| 385 | this->unsubscribeReaction((CREngine::CRType)i); |
---|
| 386 | |
---|
| 387 | // there are no reactions subscribed from now on |
---|
| 388 | this->bReactive = false; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * registers a new collision event to this world entity |
---|
| 394 | * @param entityA entity of the collision |
---|
| 395 | * @param entityB entity of the collision |
---|
| 396 | * @param bvA colliding bounding volume of entityA |
---|
| 397 | * @param bvB colliding bounding volume of entityA |
---|
| 398 | */ |
---|
| 399 | bool WorldEntity::registerCollision(WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB) |
---|
| 400 | { |
---|
| 401 | // is there any handler listening? |
---|
| 402 | if( !this->bReactive) |
---|
| 403 | return false; |
---|
| 404 | |
---|
| 405 | // get a collision event |
---|
| 406 | CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject(); |
---|
| 407 | assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h |
---|
| 408 | c->collide(entityA, entityB, bvA, bvB); |
---|
| 409 | |
---|
| 410 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 411 | if( this->collisionHandles[i] != NULL) |
---|
| 412 | this->collisionHandles[i]->registerCollisionEvent(c); |
---|
[8316] | 413 | return true; |
---|
[8190] | 414 | } |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | /** |
---|
| 418 | * registers a new collision event to this woeld entity |
---|
| 419 | * @param entity the entity that collides |
---|
| 420 | * @param plane it stands on |
---|
| 421 | * @param position it collides on the plane |
---|
| 422 | */ |
---|
[8490] | 423 | bool WorldEntity::registerCollision(WorldEntity* entity, WorldEntity* groundEntity, Vector normal, Vector position) |
---|
[8190] | 424 | { |
---|
| 425 | // is there any handler listening? |
---|
| 426 | if( !this->bReactive) |
---|
| 427 | return false; |
---|
| 428 | |
---|
| 429 | // get a collision event |
---|
| 430 | CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject(); |
---|
| 431 | assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h |
---|
[8490] | 432 | c->collide(entity, groundEntity, normal, position); |
---|
[8190] | 433 | |
---|
| 434 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 435 | if( this->collisionHandles[i] != NULL) |
---|
| 436 | this->collisionHandles[i]->registerCollisionEvent(c); |
---|
[8316] | 437 | return true; |
---|
[8190] | 438 | } |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | /** |
---|
[6142] | 442 | * @brief moves this entity to the List OM_List |
---|
| 443 | * @param list the list to set this Entity to. |
---|
| 444 | * |
---|
| 445 | * this is the same as a call to State::getObjectManager()->toList(entity , list); |
---|
| 446 | * directly, but with an easier interface. |
---|
| 447 | * |
---|
| 448 | * @todo inline this (peut etre) |
---|
| 449 | */ |
---|
| 450 | void WorldEntity::toList(OM_LIST list) |
---|
| 451 | { |
---|
| 452 | State::getObjectManager()->toList(this, list); |
---|
| 453 | } |
---|
[5061] | 454 | |
---|
[8037] | 455 | void WorldEntity::toReflectionList() |
---|
| 456 | { |
---|
| 457 | State::getObjectManager()->toReflectionList( this ); |
---|
| 458 | } |
---|
[6142] | 459 | |
---|
[8037] | 460 | void removeFromReflectionList() |
---|
| 461 | { |
---|
| 462 | /// TODO |
---|
| 463 | /// State::getObject |
---|
| 464 | } |
---|
[6142] | 465 | |
---|
[4261] | 466 | /** |
---|
[4885] | 467 | * sets the character attributes of a worldentity |
---|
[4836] | 468 | * @param character attributes |
---|
[4885] | 469 | * |
---|
| 470 | * these attributes don't have to be set, only use them, if you need them |
---|
[2043] | 471 | */ |
---|
[5498] | 472 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
| 473 | //{} |
---|
[2036] | 474 | |
---|
[3583] | 475 | |
---|
[2043] | 476 | /** |
---|
[5029] | 477 | * this function is called, when two entities collide |
---|
| 478 | * @param entity: the world entity with whom it collides |
---|
| 479 | * |
---|
| 480 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 481 | */ |
---|
| 482 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 483 | { |
---|
[5498] | 484 | /** |
---|
| 485 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
| 486 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
| 487 | * USE:: |
---|
| 488 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
| 489 | * |
---|
| 490 | * You can always define a default Action.... don't be affraid just test it :) |
---|
| 491 | */ |
---|
[6430] | 492 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
[5029] | 493 | } |
---|
| 494 | |
---|
[2043] | 495 | |
---|
| 496 | /** |
---|
[8186] | 497 | * this function is called, when two entities collide |
---|
| 498 | * @param entity: the world entity with whom it collides |
---|
| 499 | * |
---|
| 500 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 501 | */ |
---|
| 502 | void WorldEntity::collidesWithGround(const Vector& location) |
---|
| 503 | { |
---|
| 504 | PRINTF(0)("BSP_GROUND: %s collides \n", this->getClassName() ); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | void WorldEntity::collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2) |
---|
| 508 | { |
---|
[8190] | 509 | |
---|
[8186] | 510 | // PRINTF(0)("BSP_GROUND: Player collides \n", this->getClassName() ); |
---|
[8190] | 511 | |
---|
[8186] | 512 | Vector v = this->getAbsDirX(); |
---|
[8490] | 513 | v.x *= 10.1; |
---|
| 514 | v.y *= 10.1; |
---|
| 515 | v.z *= 10.1; |
---|
| 516 | Vector u = Vector(0.0,-20.0,0.0); |
---|
[8190] | 517 | |
---|
[8490] | 518 | |
---|
| 519 | if(!(this->getAbsCoor().x == ray_2.x && this->getAbsCoor().y == ray_2.y && this->getAbsCoor().z == ray_2.z) ) |
---|
[8186] | 520 | { |
---|
[8190] | 521 | |
---|
[8186] | 522 | this->setAbsCoor(ray_2 - v); |
---|
[8490] | 523 | |
---|
[8186] | 524 | } |
---|
[8490] | 525 | else |
---|
[8186] | 526 | { |
---|
| 527 | if(ray_1.x == this->getAbsCoor().x + v.x && ray_1.y == this->getAbsCoor().y + v.y + 0.1 && ray_1.z ==this->getAbsCoor().z + v.z) |
---|
| 528 | { |
---|
[8190] | 529 | this->setAbsCoor(feet -u ); |
---|
[8186] | 530 | } |
---|
[8190] | 531 | |
---|
| 532 | this->setAbsCoor(ray_2 - v); |
---|
| 533 | |
---|
[8186] | 534 | } |
---|
[8490] | 535 | |
---|
| 536 | |
---|
[8186] | 537 | } |
---|
| 538 | |
---|
| 539 | /** |
---|
[5498] | 540 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
[4885] | 541 | * |
---|
[5498] | 542 | */ |
---|
[3229] | 543 | void WorldEntity::postSpawn () |
---|
[6430] | 544 | {} |
---|
[2043] | 545 | |
---|
[3583] | 546 | |
---|
[2043] | 547 | /** |
---|
[6959] | 548 | * this method is called by the world if the WorldEntity leaves the game |
---|
[5498] | 549 | */ |
---|
[6959] | 550 | void WorldEntity::leaveWorld () |
---|
[6430] | 551 | {} |
---|
[2043] | 552 | |
---|
[3583] | 553 | |
---|
[2190] | 554 | /** |
---|
[7085] | 555 | * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning |
---|
| 556 | */ |
---|
| 557 | void WorldEntity::reset() |
---|
| 558 | {} |
---|
| 559 | |
---|
| 560 | /** |
---|
[4836] | 561 | * this method is called every frame |
---|
| 562 | * @param time: the time in seconds that has passed since the last tick |
---|
[4885] | 563 | * |
---|
| 564 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
[2043] | 565 | */ |
---|
[4570] | 566 | void WorldEntity::tick(float time) |
---|
[6430] | 567 | {} |
---|
[3583] | 568 | |
---|
[5498] | 569 | |
---|
[3583] | 570 | /** |
---|
[4836] | 571 | * the entity is drawn onto the screen with this function |
---|
[4885] | 572 | * |
---|
| 573 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
| 574 | * Just override this function with whatever you want to be drawn. |
---|
[3365] | 575 | */ |
---|
[5500] | 576 | void WorldEntity::draw() const |
---|
[3803] | 577 | { |
---|
[6430] | 578 | //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName()); |
---|
[6281] | 579 | // assert(!unlikely(this->models.empty())); |
---|
[6002] | 580 | { |
---|
| 581 | glMatrixMode(GL_MODELVIEW); |
---|
| 582 | glPushMatrix(); |
---|
[4570] | 583 | |
---|
[6002] | 584 | /* translate */ |
---|
| 585 | glTranslatef (this->getAbsCoor ().x, |
---|
| 586 | this->getAbsCoor ().y, |
---|
| 587 | this->getAbsCoor ().z); |
---|
[6004] | 588 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 589 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[2043] | 590 | |
---|
[6004] | 591 | |
---|
| 592 | // This Draws the LOD's |
---|
[7014] | 593 | float cameraDistance = State::getCamera()->distance(this); |
---|
[6004] | 594 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
---|
[6002] | 595 | { |
---|
[6222] | 596 | this->models[2]->draw(); |
---|
[6004] | 597 | } |
---|
| 598 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
---|
[6002] | 599 | { |
---|
| 600 | this->models[1]->draw(); |
---|
[6004] | 601 | } |
---|
| 602 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
---|
[6002] | 603 | { |
---|
| 604 | this->models[0]->draw(); |
---|
| 605 | } |
---|
| 606 | glPopMatrix(); |
---|
| 607 | } |
---|
[3803] | 608 | } |
---|
[3583] | 609 | |
---|
[6430] | 610 | /** |
---|
[6700] | 611 | * @param health the Health to add. |
---|
| 612 | * @returns the health left (this->healthMax - health+this->health) |
---|
[6430] | 613 | */ |
---|
[6700] | 614 | float WorldEntity::increaseHealth(float health) |
---|
[6430] | 615 | { |
---|
[6700] | 616 | this->health += health; |
---|
| 617 | if (this->health > this->healthMax) |
---|
[6430] | 618 | { |
---|
[6700] | 619 | float retHealth = this->healthMax - this->health; |
---|
| 620 | this->health = this->healthMax; |
---|
| 621 | this->updateHealthWidget(); |
---|
| 622 | return retHealth; |
---|
[6430] | 623 | } |
---|
[6700] | 624 | this->updateHealthWidget(); |
---|
[6430] | 625 | return 0.0; |
---|
| 626 | } |
---|
[6281] | 627 | |
---|
[5498] | 628 | /** |
---|
[6700] | 629 | * @param health the Health to be removed |
---|
[6430] | 630 | * @returns 0.0 or the rest, that was not substracted (bellow 0.0) |
---|
| 631 | */ |
---|
[6700] | 632 | float WorldEntity::decreaseHealth(float health) |
---|
[6430] | 633 | { |
---|
[6700] | 634 | this->health -= health; |
---|
[6430] | 635 | |
---|
[6700] | 636 | if (this->health < 0) |
---|
[6430] | 637 | { |
---|
[6700] | 638 | float retHealth = -this->health; |
---|
| 639 | this->health = 0.0f; |
---|
| 640 | this->updateHealthWidget(); |
---|
| 641 | return retHealth; |
---|
[6430] | 642 | } |
---|
[6700] | 643 | this->updateHealthWidget(); |
---|
[6430] | 644 | return 0.0; |
---|
| 645 | |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /** |
---|
[6700] | 649 | * @param maxHealth the maximal health that can be loaded onto the entity. |
---|
[6430] | 650 | */ |
---|
[6700] | 651 | void WorldEntity::setHealthMax(float healthMax) |
---|
[6430] | 652 | { |
---|
[6700] | 653 | this->healthMax = healthMax; |
---|
| 654 | if (this->health > this->healthMax) |
---|
[6430] | 655 | { |
---|
[6700] | 656 | PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName()); |
---|
| 657 | this->health = this->healthMax; |
---|
[6430] | 658 | } |
---|
[6700] | 659 | this->updateHealthWidget(); |
---|
[6430] | 660 | } |
---|
| 661 | |
---|
[6431] | 662 | /** |
---|
[6700] | 663 | * @brief creates the HealthWidget |
---|
[6431] | 664 | * |
---|
[6700] | 665 | * since not all entities need an HealthWidget, it is only created on request. |
---|
[6431] | 666 | */ |
---|
[6700] | 667 | void WorldEntity::createHealthWidget() |
---|
[6430] | 668 | { |
---|
[6700] | 669 | if (this->healthWidget == NULL) |
---|
[6430] | 670 | { |
---|
[7779] | 671 | this->healthWidget = new OrxGui::GLGuiBar(); |
---|
[6700] | 672 | this->healthWidget->setSize2D(30,400); |
---|
| 673 | this->healthWidget->setAbsCoor2D(10,100); |
---|
[6430] | 674 | |
---|
[6700] | 675 | this->updateHealthWidget(); |
---|
[6430] | 676 | } |
---|
| 677 | else |
---|
[6700] | 678 | PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName()); |
---|
[6430] | 679 | } |
---|
| 680 | |
---|
[6700] | 681 | void WorldEntity::increaseHealthMax(float increaseHealth) |
---|
[6440] | 682 | { |
---|
[6700] | 683 | this->healthMax += increaseHealth; |
---|
| 684 | this->updateHealthWidget(); |
---|
[6440] | 685 | } |
---|
| 686 | |
---|
[6700] | 687 | |
---|
[7779] | 688 | OrxGui::GLGuiWidget* WorldEntity::getHealthWidget() |
---|
[6700] | 689 | { |
---|
| 690 | this->createHealthWidget(); |
---|
| 691 | return this->healthWidget; |
---|
| 692 | } |
---|
| 693 | |
---|
[6431] | 694 | /** |
---|
[6700] | 695 | * @param visibility shows or hides the health-bar |
---|
[6431] | 696 | * (creates the widget if needed) |
---|
| 697 | */ |
---|
[6700] | 698 | void WorldEntity::setHealthWidgetVisibilit(bool visibility) |
---|
[6430] | 699 | { |
---|
[7198] | 700 | if (visibility) |
---|
| 701 | { |
---|
| 702 | if (this->healthWidget != NULL) |
---|
| 703 | this->healthWidget->show(); |
---|
| 704 | else |
---|
[6430] | 705 | { |
---|
[7198] | 706 | this->createHealthWidget(); |
---|
| 707 | this->updateHealthWidget(); |
---|
| 708 | this->healthWidget->show(); |
---|
[6430] | 709 | } |
---|
[7198] | 710 | } |
---|
| 711 | else if (this->healthWidget != NULL) |
---|
| 712 | this->healthWidget->hide(); |
---|
[6430] | 713 | } |
---|
| 714 | |
---|
[6431] | 715 | /** |
---|
[6700] | 716 | * @brief updates the HealthWidget |
---|
[6431] | 717 | */ |
---|
[6700] | 718 | void WorldEntity::updateHealthWidget() |
---|
[6430] | 719 | { |
---|
[6700] | 720 | if (this->healthWidget != NULL) |
---|
[6430] | 721 | { |
---|
[6700] | 722 | this->healthWidget->setMaximum(this->healthMax); |
---|
| 723 | this->healthWidget->setValue(this->health); |
---|
[6430] | 724 | } |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | |
---|
| 728 | /** |
---|
[5498] | 729 | * DEBUG-DRAW OF THE BV-Tree. |
---|
| 730 | * @param depth What depth to draw |
---|
| 731 | * @param drawMode the mode to draw this entity under |
---|
| 732 | */ |
---|
[7711] | 733 | void WorldEntity::drawBVTree(int depth, int drawMode) const |
---|
[4684] | 734 | { |
---|
| 735 | glMatrixMode(GL_MODELVIEW); |
---|
| 736 | glPushMatrix(); |
---|
| 737 | /* translate */ |
---|
| 738 | glTranslatef (this->getAbsCoor ().x, |
---|
| 739 | this->getAbsCoor ().y, |
---|
| 740 | this->getAbsCoor ().z); |
---|
| 741 | /* rotate */ |
---|
[4998] | 742 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 743 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[4684] | 744 | |
---|
[7711] | 745 | |
---|
[4684] | 746 | if (this->obbTree) |
---|
| 747 | this->obbTree->drawBV(depth, drawMode); |
---|
[7711] | 748 | |
---|
| 749 | |
---|
[4684] | 750 | glPopMatrix(); |
---|
| 751 | } |
---|
[6341] | 752 | |
---|
[6424] | 753 | |
---|
[6341] | 754 | /** |
---|
[6424] | 755 | * Debug the WorldEntity |
---|
| 756 | */ |
---|
| 757 | void WorldEntity::debugEntity() const |
---|
| 758 | { |
---|
| 759 | PRINT(0)("WorldEntity %s::%s (DEBUG)\n", this->getClassName(), this->getName()); |
---|
| 760 | this->debugNode(); |
---|
| 761 | PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size()); |
---|
| 762 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
| 763 | { |
---|
| 764 | if (models[i] != NULL) |
---|
| 765 | PRINT(0)(" : %d:%s", i, this->models[i]->getName()); |
---|
| 766 | } |
---|
| 767 | PRINT(0)("\n"); |
---|
| 768 | |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | |
---|
| 772 | /** |
---|
[7954] | 773 | * handler for changes on registred vars |
---|
| 774 | * @param id id's which changed |
---|
[6341] | 775 | */ |
---|
[7954] | 776 | void WorldEntity::varChangeHandler( std::list< int > & id ) |
---|
[6341] | 777 | { |
---|
[7954] | 778 | if ( std::find( id.begin(), id.end(), modelFileName_handle ) != id.end() || |
---|
| 779 | std::find( id.begin(), id.end(), scaling_handle ) != id.end() |
---|
| 780 | ) |
---|
[6341] | 781 | { |
---|
[7954] | 782 | loadModel( modelFileName, scaling ); |
---|
[6341] | 783 | } |
---|
| 784 | |
---|
[7954] | 785 | PNode::varChangeHandler( id ); |
---|
[6341] | 786 | } |
---|
| 787 | |
---|