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