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