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