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