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