| 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 "util/loading/resource_manager.h" |
|---|
| 23 | #include "resource_obj.h" |
|---|
| 24 | #include "md2/md2Model.h" |
|---|
| 25 | #include "md3/md3_model.h" |
|---|
| 26 | |
|---|
| 27 | #include "aabb_tree_node.h" |
|---|
| 28 | |
|---|
| 29 | #include "util/loading/load_param.h" |
|---|
| 30 | #include "obb_tree.h" |
|---|
| 31 | |
|---|
| 32 | #include "elements/glgui_energywidget.h" |
|---|
| 33 | |
|---|
| 34 | #include "state.h" |
|---|
| 35 | #include "camera.h" |
|---|
| 36 | |
|---|
| 37 | #include "collision_handle.h" |
|---|
| 38 | #include "collision_event.h" |
|---|
| 39 | #include "game_rules.h" |
|---|
| 40 | #include "kill.h" |
|---|
| 41 | #include "debug.h" |
|---|
| 42 | |
|---|
| 43 | #include "projectiles/projectile.h" |
|---|
| 44 | |
|---|
| 45 | SHELL_COMMAND(model, WorldEntity, loadModel) |
|---|
| 46 | ->describe("sets the Model of the WorldEntity") |
|---|
| 47 | ->defaultValues("models/ships/fighter.obj", 1.0f); |
|---|
| 48 | |
|---|
| 49 | SHELL_COMMAND(debugEntity, WorldEntity, debugWE); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | ObjectListDefinition(WorldEntity); |
|---|
| 53 | /** |
|---|
| 54 | * Loads the WordEntity-specific Part of any derived Class |
|---|
| 55 | * |
|---|
| 56 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
|---|
| 57 | * that can calls WorldEntities loadParams for itself. |
|---|
| 58 | */ |
|---|
| 59 | WorldEntity::WorldEntity() |
|---|
| 60 | : Synchronizeable() |
|---|
| 61 | { |
|---|
| 62 | this->registerObject(this, WorldEntity::_objectList); |
|---|
| 63 | |
|---|
| 64 | this->obbTree = NULL; |
|---|
| 65 | this->aabbNode = NULL; |
|---|
| 66 | this->healthWidget = NULL; |
|---|
| 67 | this->healthMax = 1.0f; |
|---|
| 68 | this->health = 1.0f; |
|---|
| 69 | this->damage = 0.0f; // no damage dealt by a default entity |
|---|
| 70 | this->scaling = 1.0f; |
|---|
| 71 | |
|---|
| 72 | /* OSOLETE */ |
|---|
| 73 | this->bVisible = true; |
|---|
| 74 | this->bCollide = true; |
|---|
| 75 | |
|---|
| 76 | this->objectListNumber = OM_INIT; |
|---|
| 77 | this->lastObjectListNumber = OM_INIT; |
|---|
| 78 | |
|---|
| 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; |
|---|
| 83 | this->bOnGround = false; |
|---|
| 84 | |
|---|
| 85 | // registering default reactions: |
|---|
| 86 | this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, /* WorldEntity::staticClassID(), */ Projectile::staticClassID()); |
|---|
| 87 | |
|---|
| 88 | this->toList(OM_NULL); |
|---|
| 89 | |
|---|
| 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 ) ); |
|---|
| 94 | |
|---|
| 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 ) ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * standard destructor |
|---|
| 101 | */ |
|---|
| 102 | WorldEntity::~WorldEntity () |
|---|
| 103 | { |
|---|
| 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 | |
|---|
| 110 | // Delete the obbTree |
|---|
| 111 | if( this->obbTree != NULL) |
|---|
| 112 | delete this->obbTree; |
|---|
| 113 | |
|---|
| 114 | if (this->healthWidget != NULL) |
|---|
| 115 | delete this->healthWidget; |
|---|
| 116 | |
|---|
| 117 | this->unsubscribeReaction(); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * loads the WorldEntity Specific Parameters. |
|---|
| 122 | * @param root: the XML-Element to load the Data From |
|---|
| 123 | */ |
|---|
| 124 | void WorldEntity::loadParams(const TiXmlElement* root) |
|---|
| 125 | { |
|---|
| 126 | // Do the PNode loading stuff |
|---|
| 127 | PNode::loadParams(root); |
|---|
| 128 | |
|---|
| 129 | LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture) |
|---|
| 130 | .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
|---|
| 131 | .defaultValues(""); |
|---|
| 132 | |
|---|
| 133 | // Model Loading |
|---|
| 134 | LoadParam(root, "model", this, WorldEntity, loadModel) |
|---|
| 135 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
|---|
| 136 | .defaultValues("", 1.0f, 0); |
|---|
| 137 | |
|---|
| 138 | LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax) |
|---|
| 139 | .describe("The Maximum health that can be loaded onto this entity") |
|---|
| 140 | .defaultValues(1.0f); |
|---|
| 141 | |
|---|
| 142 | LoadParam(root, "health", this, WorldEntity, setHealth) |
|---|
| 143 | .describe("The Health the WorldEntity has at this moment") |
|---|
| 144 | .defaultValues(1.0f); |
|---|
| 145 | |
|---|
| 146 | LoadParam(root, "list", this, WorldEntity, toListS); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * loads a Model onto a WorldEntity |
|---|
| 152 | * @param fileName the name of the model to load |
|---|
| 153 | * @param scaling the Scaling of the model |
|---|
| 154 | * |
|---|
| 155 | * FIXME |
|---|
| 156 | * @todo: separate the obb tree generation from the model |
|---|
| 157 | */ |
|---|
| 158 | void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber, unsigned int obbTreeDepth) |
|---|
| 159 | { |
|---|
| 160 | this->modelLODName = fileName; |
|---|
| 161 | this->scaling = scaling; |
|---|
| 162 | |
|---|
| 163 | std::string name = fileName; |
|---|
| 164 | |
|---|
| 165 | if ( name.find( Resources::ResourceManager::getInstance()->mainGlobalPath().name() ) == 0 ) |
|---|
| 166 | { |
|---|
| 167 | name.erase(Resources::ResourceManager::getInstance()->mainGlobalPath().name().size()); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | this->modelFileName = name; |
|---|
| 171 | |
|---|
| 172 | if (!fileName.empty()) |
|---|
| 173 | { |
|---|
| 174 | // search for the special character # in the LoadParam |
|---|
| 175 | if (fileName.find('#') != std::string::npos) |
|---|
| 176 | { |
|---|
| 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('#'); |
|---|
| 180 | for (unsigned int i = 0; i < 3; i++) |
|---|
| 181 | { |
|---|
| 182 | lodFile[offset] = 48+(int)i; |
|---|
| 183 | if (Resources::ResourceManager::getInstance()->checkFileInMainPath( lodFile)) |
|---|
| 184 | this->loadModel(lodFile, scaling, i); |
|---|
| 185 | } |
|---|
| 186 | return; |
|---|
| 187 | } |
|---|
| 188 | if (this->scaling <= 0.0) |
|---|
| 189 | { |
|---|
| 190 | PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n"); |
|---|
| 191 | this->scaling = 1.0; |
|---|
| 192 | } |
|---|
| 193 | /// LOADING AN OBJ FILE |
|---|
| 194 | if(fileName.find(".obj") != std::string::npos) |
|---|
| 195 | { |
|---|
| 196 | PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str()); |
|---|
| 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 | } |
|---|
| 205 | else |
|---|
| 206 | delete model; |
|---|
| 207 | } |
|---|
| 208 | /// LOADING AN MD2-model |
|---|
| 209 | else if(fileName.find(".md2") != std::string::npos) |
|---|
| 210 | { |
|---|
| 211 | PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str()); |
|---|
| 212 | Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling); |
|---|
| 213 | //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0); |
|---|
| 214 | this->setModel(m, 0); |
|---|
| 215 | |
|---|
| 216 | if( m != NULL) |
|---|
| 217 | this->buildObbTree(obbTreeDepth); |
|---|
| 218 | } |
|---|
| 219 | /// LOADING AN MD3-MODEL. |
|---|
| 220 | else if(fileName.find(".md3") != std::string::npos) |
|---|
| 221 | { |
|---|
| 222 | PRINTF(4)("fetching MD3 file: %s\n", fileName.c_str()); |
|---|
| 223 | // Model* m = new md3::MD3Model(fileName, this->scaling); |
|---|
| 224 | // this->setModel(m, 0); |
|---|
| 225 | |
|---|
| 226 | // if( m != NULL) |
|---|
| 227 | // this->buildObbTree(obbTreeDepth); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | this->setModel(NULL); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 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 | { |
|---|
| 243 | if (this->models.size() <= modelNumber) |
|---|
| 244 | this->models.resize(modelNumber+1, NULL); |
|---|
| 245 | |
|---|
| 246 | if (this->models[modelNumber] != NULL) |
|---|
| 247 | { |
|---|
| 248 | delete this->models[modelNumber]; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | this->models[modelNumber] = model; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | /** |
|---|
| 256 | * builds the obb-tree |
|---|
| 257 | * @param depth the depth to calculate |
|---|
| 258 | */ |
|---|
| 259 | bool WorldEntity::buildObbTree(int depth) |
|---|
| 260 | { |
|---|
| 261 | if( this->obbTree != NULL) |
|---|
| 262 | { |
|---|
| 263 | delete this->obbTree; |
|---|
| 264 | this->obbTree = NULL; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | if (this->models[0] != NULL) |
|---|
| 268 | this->obbTree = new OBBTree(depth, models[0]->getModelInfo(), this); |
|---|
| 269 | else |
|---|
| 270 | { |
|---|
| 271 | PRINTF(1)("could not create obb-tree, because no model was loaded yet\n"); |
|---|
| 272 | this->obbTree = NULL; |
|---|
| 273 | return false; |
|---|
| 274 | } |
|---|
| 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 | |
|---|
| 284 | if( this->models[0] != NULL) |
|---|
| 285 | { |
|---|
| 286 | this->aabbNode = new AABBTreeNode(); |
|---|
| 287 | this->aabbNode->spawnBVTree(this->models[0]); |
|---|
| 288 | } |
|---|
| 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 | } |
|---|
| 295 | return true; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * subscribes this world entity to a collision reaction |
|---|
| 301 | * @param type the type of reaction to subscribe to |
|---|
| 302 | * @param target1 a filter target (classID) |
|---|
| 303 | */ |
|---|
| 304 | void WorldEntity::subscribeReaction(CREngine::CRType type, const ClassID& target1) |
|---|
| 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 | */ |
|---|
| 318 | void WorldEntity::subscribeReaction(CREngine::CRType type, const ClassID& target1, const ClassID& target2) |
|---|
| 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 | */ |
|---|
| 333 | void WorldEntity::subscribeReaction(CREngine::CRType type, const ClassID& target1, const ClassID& target2, const ClassID& target3) |
|---|
| 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 | */ |
|---|
| 349 | void WorldEntity::subscribeReaction(CREngine::CRType type, const ClassID& target1, const ClassID& target2, const ClassID& target3, const ClassID& target4) |
|---|
| 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 |
|---|
| 364 | * @param nrOfTargets number of target filters |
|---|
| 365 | * @param ... the targets as classIDs |
|---|
| 366 | */ |
|---|
| 367 | void WorldEntity::subscribeReaction(CREngine::CRType type) |
|---|
| 368 | { |
|---|
| 369 | if( this->collisionHandles[type] != NULL) |
|---|
| 370 | { |
|---|
| 371 | PRINTF(2)("Registering for a CollisionReaction already subscribed to! Skipping\n"); |
|---|
| 372 | return; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | this->collisionHandles[type] = CREngine::getInstance()->subscribeReaction(this, type); |
|---|
| 376 | |
|---|
| 377 | // now there is at least one collision reaction subscribed |
|---|
| 378 | this->bReactive = true; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | /** |
|---|
| 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 | { |
|---|
| 429 | PRINTF(5)("registering collision of type: %s vs %s\n", entityA->getClassCName(), entityB->getClassCName()); |
|---|
| 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 |
|---|
| 437 | c->collide(COLLISION_TYPE_OBB, entityA, entityB, bvA, bvB); |
|---|
| 438 | |
|---|
| 439 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
|---|
| 440 | if( this->collisionHandles[i] != NULL) |
|---|
| 441 | this->collisionHandles[i]->registerCollisionEvent(c); |
|---|
| 442 | return true; |
|---|
| 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 | */ |
|---|
| 452 | bool WorldEntity::registerCollision(int type, WorldEntity* entity, WorldEntity* groundEntity, Vector normal, Vector position, bool bInWall) |
|---|
| 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 |
|---|
| 461 | c->collide(type, entity, groundEntity, normal, position, bInWall); |
|---|
| 462 | |
|---|
| 463 | for( int i = 0; i < CREngine::CR_NUMBER; ++i) |
|---|
| 464 | if( this->collisionHandles[i] != NULL) |
|---|
| 465 | this->collisionHandles[i]->registerCollisionEvent(c); |
|---|
| 466 | return true; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | /** |
|---|
| 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 | } |
|---|
| 483 | |
|---|
| 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 | |
|---|
| 494 | void WorldEntity::toReflectionList() |
|---|
| 495 | { |
|---|
| 496 | State::getObjectManager()->toReflectionList( this ); |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | void removeFromReflectionList() |
|---|
| 500 | { |
|---|
| 501 | /// TODO |
|---|
| 502 | /// State::getObject |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | /** |
|---|
| 506 | * sets the character attributes of a worldentity |
|---|
| 507 | * @param character attributes |
|---|
| 508 | * |
|---|
| 509 | * these attributes don't have to be set, only use them, if you need them |
|---|
| 510 | */ |
|---|
| 511 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
|---|
| 512 | //{} |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | /** |
|---|
| 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 | { |
|---|
| 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 | */ |
|---|
| 531 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassCName(), entity->getClassCName(), location.x, location.y, location.z); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | /** |
|---|
| 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 | { |
|---|
| 543 | PRINTF(0)("BSP_GROUND: %s collides \n", this->getClassCName() ); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | void WorldEntity::collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2) |
|---|
| 547 | { |
|---|
| 548 | |
|---|
| 549 | // PRINTF(0)("BSP_GROUND: Player collides \n", this->getClassCName() ); |
|---|
| 550 | |
|---|
| 551 | Vector v = this->getAbsDirX(); |
|---|
| 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); |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | if(!(this->getAbsCoor().x == ray_2.x && this->getAbsCoor().y == ray_2.y && this->getAbsCoor().z == ray_2.z) ) |
|---|
| 559 | { |
|---|
| 560 | |
|---|
| 561 | this->setAbsCoor(ray_2 - v); |
|---|
| 562 | |
|---|
| 563 | } |
|---|
| 564 | else |
|---|
| 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 | { |
|---|
| 568 | this->setAbsCoor(feet -u ); |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | this->setAbsCoor(ray_2 - v); |
|---|
| 572 | |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | /** |
|---|
| 579 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
|---|
| 580 | * |
|---|
| 581 | */ |
|---|
| 582 | void WorldEntity::postSpawn () |
|---|
| 583 | {} |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | /** |
|---|
| 587 | * this method is called by the world if the WorldEntity leaves the game |
|---|
| 588 | */ |
|---|
| 589 | void WorldEntity::leaveWorld () |
|---|
| 590 | {} |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | /** |
|---|
| 594 | * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning |
|---|
| 595 | */ |
|---|
| 596 | void WorldEntity::reset() |
|---|
| 597 | { |
|---|
| 598 | this->setHealth( this->getHealthMax() ); |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | /** |
|---|
| 602 | * this method is called every frame |
|---|
| 603 | * @param time: the time in seconds that has passed since the last tick |
|---|
| 604 | * |
|---|
| 605 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
|---|
| 606 | */ |
|---|
| 607 | void WorldEntity::tick(float time) |
|---|
| 608 | {} |
|---|
| 609 | |
|---|
| 610 | |
|---|
| 611 | /** |
|---|
| 612 | * the entity is drawn onto the screen with this function |
|---|
| 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. |
|---|
| 616 | */ |
|---|
| 617 | void WorldEntity::draw() const |
|---|
| 618 | { |
|---|
| 619 | //PRINTF(0)("(%s::%s)\n", this->getClassCName(), this->getName()); |
|---|
| 620 | // assert(!unlikely(this->models.empty())); |
|---|
| 621 | { |
|---|
| 622 | glMatrixMode(GL_MODELVIEW); |
|---|
| 623 | glPushMatrix(); |
|---|
| 624 | |
|---|
| 625 | /* translate */ |
|---|
| 626 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 627 | this->getAbsCoor ().y, |
|---|
| 628 | this->getAbsCoor ().z); |
|---|
| 629 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
|---|
| 630 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
|---|
| 631 | |
|---|
| 632 | |
|---|
| 633 | // This Draws the LOD's |
|---|
| 634 | float cameraDistance = State::getCamera()->distance(this); |
|---|
| 635 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
|---|
| 636 | { |
|---|
| 637 | this->models[2]->draw(); |
|---|
| 638 | } |
|---|
| 639 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
|---|
| 640 | { |
|---|
| 641 | this->models[1]->draw(); |
|---|
| 642 | } |
|---|
| 643 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
|---|
| 644 | { |
|---|
| 645 | this->models[0]->draw(); |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | // if( this->aabbNode != NULL) |
|---|
| 649 | // this->aabbNode->drawBV(0, DRAW_BV_POLYGON, Vector(1, 0.6, 0.2), true); |
|---|
| 650 | |
|---|
| 651 | glPopMatrix(); |
|---|
| 652 | } |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | /** |
|---|
| 656 | * @param health the Health to add. |
|---|
| 657 | * @returns the health left (this->healthMax - health+this->health) |
|---|
| 658 | */ |
|---|
| 659 | float WorldEntity::increaseHealth(float health) |
|---|
| 660 | { |
|---|
| 661 | this->health += health; |
|---|
| 662 | if (this->health > this->healthMax) |
|---|
| 663 | { |
|---|
| 664 | float retHealth = this->healthMax - this->health; |
|---|
| 665 | this->health = this->healthMax; |
|---|
| 666 | this->updateHealthWidget(); |
|---|
| 667 | return retHealth; |
|---|
| 668 | } |
|---|
| 669 | this->updateHealthWidget(); |
|---|
| 670 | return 0.0; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | /** |
|---|
| 674 | * @param health the Health to be removed |
|---|
| 675 | * @returns 0.0 or the rest, that was not substracted (bellow 0.0) |
|---|
| 676 | */ |
|---|
| 677 | float WorldEntity::decreaseHealth(float health) |
|---|
| 678 | { |
|---|
| 679 | this->health -= health; |
|---|
| 680 | |
|---|
| 681 | if (this->health < 0) |
|---|
| 682 | { |
|---|
| 683 | float retHealth = -this->health; |
|---|
| 684 | this->health = 0.0f; |
|---|
| 685 | this->updateHealthWidget(); |
|---|
| 686 | return retHealth; |
|---|
| 687 | } |
|---|
| 688 | this->updateHealthWidget(); |
|---|
| 689 | return 0.0; |
|---|
| 690 | |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | /** |
|---|
| 694 | * @param maxHealth the maximal health that can be loaded onto the entity. |
|---|
| 695 | */ |
|---|
| 696 | void WorldEntity::setHealthMax(float healthMax) |
|---|
| 697 | { |
|---|
| 698 | this->healthMax = healthMax; |
|---|
| 699 | if (this->health > this->healthMax) |
|---|
| 700 | { |
|---|
| 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()); |
|---|
| 702 | this->health = this->healthMax; |
|---|
| 703 | } |
|---|
| 704 | this->updateHealthWidget(); |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | /** |
|---|
| 708 | * @brief creates the HealthWidget |
|---|
| 709 | * |
|---|
| 710 | * since not all entities need an HealthWidget, it is only created on request. |
|---|
| 711 | */ |
|---|
| 712 | void WorldEntity::createHealthWidget() |
|---|
| 713 | { |
|---|
| 714 | if (this->healthWidget == NULL) |
|---|
| 715 | { |
|---|
| 716 | this->healthWidget = new OrxGui::GLGuiEnergyWidget(); |
|---|
| 717 | this->healthWidget->setDisplayedName(std::string(this->getClassName()) + " Energy:"); |
|---|
| 718 | this->healthWidget->setSize2D(30,400); |
|---|
| 719 | this->healthWidget->setAbsCoor2D(10,100); |
|---|
| 720 | |
|---|
| 721 | this->updateHealthWidget(); |
|---|
| 722 | } |
|---|
| 723 | else |
|---|
| 724 | PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassCName(), this->getCName()); |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | void WorldEntity::increaseHealthMax(float increaseHealth) |
|---|
| 728 | { |
|---|
| 729 | this->healthMax += increaseHealth; |
|---|
| 730 | this->updateHealthWidget(); |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | OrxGui::GLGuiWidget* WorldEntity::getHealthWidget() |
|---|
| 735 | { |
|---|
| 736 | this->createHealthWidget(); |
|---|
| 737 | return this->healthWidget; |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | /** |
|---|
| 741 | * @param visibility shows or hides the health-bar |
|---|
| 742 | * (creates the widget if needed) |
|---|
| 743 | */ |
|---|
| 744 | void WorldEntity::setHealthWidgetVisibilit(bool visibility) |
|---|
| 745 | { |
|---|
| 746 | if (visibility) |
|---|
| 747 | { |
|---|
| 748 | if (this->healthWidget != NULL) |
|---|
| 749 | this->healthWidget->show(); |
|---|
| 750 | else |
|---|
| 751 | { |
|---|
| 752 | this->createHealthWidget(); |
|---|
| 753 | this->updateHealthWidget(); |
|---|
| 754 | this->healthWidget->show(); |
|---|
| 755 | } |
|---|
| 756 | } |
|---|
| 757 | else if (this->healthWidget != NULL) |
|---|
| 758 | this->healthWidget->hide(); |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | /** |
|---|
| 763 | * hit the world entity with |
|---|
| 764 | * @param damage damage to be dealt |
|---|
| 765 | */ |
|---|
| 766 | void WorldEntity::hit(float damage, WorldEntity* killer) |
|---|
| 767 | { |
|---|
| 768 | this->decreaseHealth(damage); |
|---|
| 769 | |
|---|
| 770 | PRINTF(5)("Hit me: %s::%s now only %f/%f health\n", this->getClassCName(), this->getCName(), this->getHealth(), this->getHealthMax()); |
|---|
| 771 | |
|---|
| 772 | if( this->getHealth() > 0) |
|---|
| 773 | { |
|---|
| 774 | // any small explosion animaitions |
|---|
| 775 | } |
|---|
| 776 | else |
|---|
| 777 | { |
|---|
| 778 | this->destroy( killer ); |
|---|
| 779 | } |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | /** |
|---|
| 784 | * destoys the world entity |
|---|
| 785 | */ |
|---|
| 786 | void WorldEntity::destroy(WorldEntity* killer) |
|---|
| 787 | { |
|---|
| 788 | this->toList(OM_DEAD); |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | /** |
|---|
| 793 | * @brief updates the HealthWidget |
|---|
| 794 | */ |
|---|
| 795 | void WorldEntity::updateHealthWidget() |
|---|
| 796 | { |
|---|
| 797 | if (this->healthWidget != NULL) |
|---|
| 798 | { |
|---|
| 799 | this->healthWidget->setMaximum(this->healthMax); |
|---|
| 800 | this->healthWidget->setValue(this->health); |
|---|
| 801 | } |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | /** |
|---|
| 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 | */ |
|---|
| 810 | void WorldEntity::drawBVTree(int depth, int drawMode) const |
|---|
| 811 | { |
|---|
| 812 | glMatrixMode(GL_MODELVIEW); |
|---|
| 813 | glPushMatrix(); |
|---|
| 814 | /* translate */ |
|---|
| 815 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 816 | this->getAbsCoor ().y, |
|---|
| 817 | this->getAbsCoor ().z); |
|---|
| 818 | /* rotate */ |
|---|
| 819 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
|---|
| 820 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
|---|
| 821 | |
|---|
| 822 | |
|---|
| 823 | if (this->obbTree) |
|---|
| 824 | this->obbTree->drawBV(depth, drawMode); |
|---|
| 825 | |
|---|
| 826 | |
|---|
| 827 | glPopMatrix(); |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | /** |
|---|
| 832 | * Debug the WorldEntity |
|---|
| 833 | */ |
|---|
| 834 | void WorldEntity::debugEntity() const |
|---|
| 835 | { |
|---|
| 836 | PRINT(0)("WorldEntity %s::%s (DEBUG)\n", this->getClassCName(), this->getCName()); |
|---|
| 837 | this->debugNode(); |
|---|
| 838 | PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber).c_str(), this->models.size()); |
|---|
| 839 | for (unsigned int i = 0; i < this->models.size(); i++) |
|---|
| 840 | { |
|---|
| 841 | if (models[i] != NULL) |
|---|
| 842 | PRINT(0)(" : %d:%s", i, this->models[i]->getCName()); |
|---|
| 843 | } |
|---|
| 844 | PRINT(0)("\n"); |
|---|
| 845 | |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | |
|---|
| 849 | /** |
|---|
| 850 | * handler for changes on registred vars |
|---|
| 851 | * @param id id's which changed |
|---|
| 852 | */ |
|---|
| 853 | void WorldEntity::varChangeHandler( std::list< int > & id ) |
|---|
| 854 | { |
|---|
| 855 | if ( std::find( id.begin(), id.end(), modelFileName_handle ) != id.end() || |
|---|
| 856 | std::find( id.begin(), id.end(), scaling_handle ) != id.end() |
|---|
| 857 | ) |
|---|
| 858 | { |
|---|
| 859 | loadModel( modelFileName, scaling ); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | if ( std::find( id.begin(), id.end(), list_handle ) != id.end() ) |
|---|
| 863 | { |
|---|
| 864 | this->toList( (OM_LIST)list_write ); |
|---|
| 865 | } |
|---|
| 866 | |
|---|
| 867 | if ( std::find( id.begin(), id.end(), health_handle ) != id.end() ) |
|---|
| 868 | { |
|---|
| 869 | this->setHealth( health_write ); |
|---|
| 870 | } |
|---|
| 871 | |
|---|
| 872 | if ( std::find( id.begin(), id.end(), healthMax_handle ) != id.end() ) |
|---|
| 873 | { |
|---|
| 874 | this->setHealthMax( healthMax_write ); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | PNode::varChangeHandler( id ); |
|---|
| 878 | } |
|---|
| 879 | |
|---|