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