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