[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 | |
---|
[5511] | 22 | #include "model.h" |
---|
[6222] | 23 | #include "md2Model.h" |
---|
[7193] | 24 | #include "util/loading/resource_manager.h" |
---|
| 25 | #include "util/loading/load_param.h" |
---|
[3803] | 26 | #include "vector.h" |
---|
[4682] | 27 | #include "obb_tree.h" |
---|
[3608] | 28 | |
---|
[6430] | 29 | #include "glgui_bar.h" |
---|
| 30 | |
---|
[6002] | 31 | #include "state.h" |
---|
[7014] | 32 | #include "camera.h" |
---|
[6002] | 33 | |
---|
[7927] | 34 | #include "collision_handle.h" |
---|
[7945] | 35 | #include "collision.h" |
---|
[7927] | 36 | |
---|
[7933] | 37 | #include <stdarg.h> |
---|
[7927] | 38 | |
---|
[7933] | 39 | |
---|
[2036] | 40 | using namespace std; |
---|
| 41 | |
---|
[5208] | 42 | SHELL_COMMAND(model, WorldEntity, loadModel) |
---|
[6430] | 43 | ->describe("sets the Model of the WorldEntity") |
---|
[7711] | 44 | ->defaultValues("models/ships/fighter.obj", 1.0f); |
---|
[5208] | 45 | |
---|
[6424] | 46 | SHELL_COMMAND(debugEntity, WorldEntity, debugWE); |
---|
[5208] | 47 | |
---|
[2043] | 48 | /** |
---|
[4836] | 49 | * Loads the WordEntity-specific Part of any derived Class |
---|
[5498] | 50 | * |
---|
| 51 | * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves, |
---|
| 52 | * that can calls WorldEntities loadParams for itself. |
---|
| 53 | */ |
---|
[6430] | 54 | WorldEntity::WorldEntity() |
---|
| 55 | : Synchronizeable() |
---|
[2190] | 56 | { |
---|
[4320] | 57 | this->setClassID(CL_WORLD_ENTITY, "WorldEntity"); |
---|
[4597] | 58 | |
---|
[4682] | 59 | this->obbTree = NULL; |
---|
[6700] | 60 | this->healthWidget = NULL; |
---|
| 61 | this->healthMax = 1.0f; |
---|
| 62 | this->health = 1.0f; |
---|
[6695] | 63 | this->scaling = 1.0f; |
---|
[4261] | 64 | |
---|
[6695] | 65 | /* OSOLETE */ |
---|
| 66 | this->bVisible = true; |
---|
| 67 | this->bCollide = true; |
---|
| 68 | |
---|
[6142] | 69 | this->objectListNumber = OM_INIT; |
---|
| 70 | this->objectListIterator = NULL; |
---|
| 71 | |
---|
[7945] | 72 | // reset all collision handles to NULL == unsubscribed state |
---|
| 73 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 74 | this->collisionHandles[i] = NULL; |
---|
| 75 | |
---|
[6142] | 76 | this->toList(OM_NULL); |
---|
[2190] | 77 | } |
---|
[2043] | 78 | |
---|
| 79 | /** |
---|
[4836] | 80 | * standard destructor |
---|
[2043] | 81 | */ |
---|
[2190] | 82 | WorldEntity::~WorldEntity () |
---|
[2036] | 83 | { |
---|
[7125] | 84 | State::getObjectManager()->toList(this, OM_INIT); |
---|
| 85 | |
---|
| 86 | // Delete the model (unregister it with the ResourceManager) |
---|
| 87 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
| 88 | this->setModel(NULL, i); |
---|
| 89 | |
---|
[5498] | 90 | // Delete the obbTree |
---|
[5302] | 91 | if( this->obbTree != NULL) |
---|
[4814] | 92 | delete this->obbTree; |
---|
[5994] | 93 | |
---|
[6700] | 94 | if (this->healthWidget != NULL) |
---|
| 95 | delete this->healthWidget; |
---|
[7946] | 96 | |
---|
| 97 | this->unsubscribeReaction(); |
---|
[3531] | 98 | } |
---|
| 99 | |
---|
[5498] | 100 | /** |
---|
| 101 | * loads the WorldEntity Specific Parameters. |
---|
| 102 | * @param root: the XML-Element to load the Data From |
---|
| 103 | */ |
---|
[4436] | 104 | void WorldEntity::loadParams(const TiXmlElement* root) |
---|
| 105 | { |
---|
[5498] | 106 | // Do the PNode loading stuff |
---|
[6512] | 107 | PNode::loadParams(root); |
---|
[5498] | 108 | |
---|
[6222] | 109 | LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture) |
---|
[6430] | 110 | .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
[7198] | 111 | .defaultValues(""); |
---|
[6222] | 112 | |
---|
[4436] | 113 | // Model Loading |
---|
[5671] | 114 | LoadParam(root, "model", this, WorldEntity, loadModel) |
---|
[6430] | 115 | .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)") |
---|
[7198] | 116 | .defaultValues("", 1.0f, 0); |
---|
[6430] | 117 | |
---|
[6700] | 118 | LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax) |
---|
| 119 | .describe("The Maximum health that can be loaded onto this entity") |
---|
[7198] | 120 | .defaultValues(1.0f); |
---|
[6430] | 121 | |
---|
[6700] | 122 | LoadParam(root, "health", this, WorldEntity, setHealth) |
---|
| 123 | .describe("The Health the WorldEntity has at this moment") |
---|
[7198] | 124 | .defaultValues(1.0f); |
---|
[4436] | 125 | } |
---|
| 126 | |
---|
[6222] | 127 | |
---|
[3531] | 128 | /** |
---|
[4885] | 129 | * loads a Model onto a WorldEntity |
---|
[4836] | 130 | * @param fileName the name of the model to load |
---|
[5057] | 131 | * @param scaling the Scaling of the model |
---|
[7711] | 132 | * |
---|
| 133 | * FIXME |
---|
| 134 | * @todo: separate the obb tree generation from the model |
---|
[7221] | 135 | */ |
---|
[7711] | 136 | void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber, unsigned int obbTreeDepth) |
---|
[4261] | 137 | { |
---|
[6695] | 138 | this->modelLODName = fileName; |
---|
[6424] | 139 | this->scaling = scaling; |
---|
[7221] | 140 | if (!fileName.empty()) |
---|
[6142] | 141 | { |
---|
[6430] | 142 | // search for the special character # in the LoadParam |
---|
[7221] | 143 | if (fileName.find('#') != std::string::npos) |
---|
[6222] | 144 | { |
---|
[7221] | 145 | PRINTF(4)("Found # in %s... searching for LOD's\n", fileName.c_str()); |
---|
| 146 | std::string lodFile = fileName; |
---|
| 147 | unsigned int offset = lodFile.find('#'); |
---|
[6720] | 148 | for (unsigned int i = 0; i < 3; i++) |
---|
[6005] | 149 | { |
---|
[7221] | 150 | lodFile[offset] = 48+(int)i; |
---|
[6222] | 151 | if (ResourceManager::isInDataDir(lodFile)) |
---|
| 152 | this->loadModel(lodFile, scaling, i); |
---|
[6005] | 153 | } |
---|
[6222] | 154 | return; |
---|
| 155 | } |
---|
[6720] | 156 | if (this->scaling <= 0.0) |
---|
[6424] | 157 | { |
---|
[7193] | 158 | PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n"); |
---|
[6720] | 159 | this->scaling = 1.0; |
---|
[6424] | 160 | } |
---|
[7221] | 161 | if(fileName.find(".obj") != std::string::npos) |
---|
[6222] | 162 | { |
---|
[7221] | 163 | PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str()); |
---|
[7193] | 164 | BaseObject* loadedModel = ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, this->scaling); |
---|
| 165 | if (loadedModel != NULL) |
---|
| 166 | this->setModel(dynamic_cast<Model*>(loadedModel), modelNumber); |
---|
[7221] | 167 | else |
---|
| 168 | PRINTF(1)("OBJ-File %s not found.\n", fileName.c_str()); |
---|
[6222] | 169 | |
---|
| 170 | if( modelNumber == 0) |
---|
[7711] | 171 | this->buildObbTree(obbTreeDepth); |
---|
[6222] | 172 | } |
---|
[7221] | 173 | else if(fileName.find(".md2") != std::string::npos) |
---|
[6222] | 174 | { |
---|
[7221] | 175 | PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str()); |
---|
[7055] | 176 | Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling); |
---|
[6430] | 177 | //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0); |
---|
[6222] | 178 | this->setModel(m, 0); |
---|
[7068] | 179 | |
---|
| 180 | if( m != NULL) |
---|
[7711] | 181 | this->buildObbTree(obbTreeDepth); |
---|
[6222] | 182 | } |
---|
[4732] | 183 | } |
---|
| 184 | else |
---|
[6341] | 185 | { |
---|
[5995] | 186 | this->setModel(NULL); |
---|
[6341] | 187 | } |
---|
[4261] | 188 | } |
---|
| 189 | |
---|
[5061] | 190 | /** |
---|
[5994] | 191 | * sets a specific Model for the Object. |
---|
| 192 | * @param model The Model to set |
---|
| 193 | * @param modelNumber the n'th model in the List to get. |
---|
| 194 | */ |
---|
| 195 | void WorldEntity::setModel(Model* model, unsigned int modelNumber) |
---|
| 196 | { |
---|
[5995] | 197 | if (this->models.size() <= modelNumber) |
---|
| 198 | this->models.resize(modelNumber+1, NULL); |
---|
| 199 | |
---|
| 200 | if (this->models[modelNumber] != NULL) |
---|
[6004] | 201 | { |
---|
[7193] | 202 | Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(dynamic_cast<BaseObject*>(this->models[modelNumber])); |
---|
[7123] | 203 | if (resource != NULL) |
---|
| 204 | ResourceManager::getInstance()->unload(resource, RP_LEVEL); |
---|
| 205 | else |
---|
| 206 | { |
---|
| 207 | PRINTF(4)("Forcing model deletion\n"); |
---|
| 208 | delete this->models[modelNumber]; |
---|
| 209 | } |
---|
[5994] | 210 | } |
---|
[6222] | 211 | |
---|
[5995] | 212 | this->models[modelNumber] = model; |
---|
[5994] | 213 | |
---|
[6222] | 214 | |
---|
[6430] | 215 | // if (this->model != NULL) |
---|
| 216 | // this->buildObbTree(4); |
---|
[5994] | 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | /** |
---|
[5061] | 221 | * builds the obb-tree |
---|
| 222 | * @param depth the depth to calculate |
---|
| 223 | */ |
---|
[7711] | 224 | bool WorldEntity::buildObbTree(int depth) |
---|
[5061] | 225 | { |
---|
[5428] | 226 | if (this->obbTree) |
---|
| 227 | delete this->obbTree; |
---|
| 228 | |
---|
[5995] | 229 | if (this->models[0] != NULL) |
---|
[5428] | 230 | { |
---|
[7711] | 231 | this->obbTree = new OBBTree(depth, models[0]->getModelInfo(), this); |
---|
[5428] | 232 | return true; |
---|
| 233 | } |
---|
| 234 | else |
---|
| 235 | { |
---|
[7711] | 236 | PRINTF(1)("could not create obb-tree, because no model was loaded yet\n"); |
---|
[5428] | 237 | this->obbTree = NULL; |
---|
| 238 | return false; |
---|
| 239 | } |
---|
[5061] | 240 | } |
---|
[5057] | 241 | |
---|
[7927] | 242 | |
---|
[6142] | 243 | /** |
---|
[7927] | 244 | * subscribes this world entity to a collision reaction |
---|
| 245 | * @param type the type of reaction to subscribe to |
---|
| 246 | * @param nrOfTargets number of target filters |
---|
| 247 | * @param ... the targets as classIDs |
---|
| 248 | */ |
---|
[7933] | 249 | void WorldEntity::subscribeReaction(CREngine::CRType type, int nrOfTargets, long target...) |
---|
[7932] | 250 | { |
---|
[7945] | 251 | if( this->collisionHandles[type] != NULL) { |
---|
| 252 | PRINTF(2)("Registering for a CollisionReaction already subscribed to! Skipping\n"); |
---|
| 253 | return; |
---|
| 254 | } |
---|
[7927] | 255 | |
---|
[7932] | 256 | this->collisionHandles[type] = CREngine::getInstance()->subscribeReaction(this, type); |
---|
[7927] | 257 | |
---|
[7932] | 258 | va_list itemlist; |
---|
[7933] | 259 | va_start (itemlist, target); |
---|
[7932] | 260 | for (int i = 0; i < nrOfTargets; i++) |
---|
[7933] | 261 | this->collisionHandles[type]->addTarget(va_arg(itemlist, long)); |
---|
[7932] | 262 | va_end(itemlist); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
[7927] | 266 | /** |
---|
[7946] | 267 | * unsubscribes a specific reaction from the worldentity |
---|
| 268 | * @param type the reaction to unsubscribe |
---|
| 269 | */ |
---|
| 270 | void WorldEntity::unsubscribeReaction(CREngine::CRType type) |
---|
| 271 | { |
---|
| 272 | if( this->collisionHandles[type] == NULL) |
---|
| 273 | return; |
---|
| 274 | |
---|
| 275 | CREngine::getInstance()->unsubscribeReaction(this->collisionHandles[type]); |
---|
| 276 | this->collisionHandles[type] = NULL; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * unsubscribes all collision reactions |
---|
| 282 | */ |
---|
| 283 | void WorldEntity::unsubscribeReaction() |
---|
| 284 | { |
---|
| 285 | for( int i = 0; i < CREngine::CR_NUMBER; i++) |
---|
| 286 | this->unsubscribeReaction((CREngine::CRType)i); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | /** |
---|
[7944] | 291 | * registers a new collision to this world entity |
---|
| 292 | * @param collisionEvent the event |
---|
| 293 | */ |
---|
[7945] | 294 | bool WorldEntity::registerCollision(WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB) |
---|
[7944] | 295 | { |
---|
| 296 | // create a collision event |
---|
| 297 | Collision* c = CREngine::getInstance()->popCollisionObject(); |
---|
[7955] | 298 | assert(c != NULL); |
---|
[7944] | 299 | c->collide(entityA, entityB, bvA, bvB); |
---|
| 300 | |
---|
| 301 | for(int i = 0; i < CREngine::CR_NUMBER; ++i) |
---|
| 302 | if( this->collisionHandles[i] != NULL) |
---|
| 303 | this->collisionHandles[i]->registerCollision(c); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | /** |
---|
[6142] | 308 | * @brief moves this entity to the List OM_List |
---|
| 309 | * @param list the list to set this Entity to. |
---|
| 310 | * |
---|
| 311 | * this is the same as a call to State::getObjectManager()->toList(entity , list); |
---|
| 312 | * directly, but with an easier interface. |
---|
| 313 | * |
---|
| 314 | * @todo inline this (peut etre) |
---|
| 315 | */ |
---|
| 316 | void WorldEntity::toList(OM_LIST list) |
---|
| 317 | { |
---|
| 318 | State::getObjectManager()->toList(this, list); |
---|
| 319 | } |
---|
[5061] | 320 | |
---|
[6142] | 321 | |
---|
| 322 | |
---|
[4261] | 323 | /** |
---|
[4885] | 324 | * sets the character attributes of a worldentity |
---|
[4836] | 325 | * @param character attributes |
---|
[4885] | 326 | * |
---|
| 327 | * these attributes don't have to be set, only use them, if you need them |
---|
[2043] | 328 | */ |
---|
[5498] | 329 | //void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr) |
---|
| 330 | //{} |
---|
[2036] | 331 | |
---|
[3583] | 332 | |
---|
[2043] | 333 | /** |
---|
[5029] | 334 | * this function is called, when two entities collide |
---|
| 335 | * @param entity: the world entity with whom it collides |
---|
| 336 | * |
---|
| 337 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 338 | */ |
---|
| 339 | void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 340 | { |
---|
[5498] | 341 | /** |
---|
| 342 | * THIS IS A DEFAULT COLLISION-Effect. |
---|
| 343 | * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT |
---|
| 344 | * USE:: |
---|
| 345 | * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; }; |
---|
| 346 | * |
---|
| 347 | * You can always define a default Action.... don't be affraid just test it :) |
---|
| 348 | */ |
---|
[6430] | 349 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
[5029] | 350 | } |
---|
| 351 | |
---|
[2043] | 352 | |
---|
| 353 | /** |
---|
[5498] | 354 | * this is called immediately after the Entity has been constructed, initialized and then Spawned into the World |
---|
[4885] | 355 | * |
---|
[5498] | 356 | */ |
---|
[3229] | 357 | void WorldEntity::postSpawn () |
---|
[6430] | 358 | {} |
---|
[2043] | 359 | |
---|
[3583] | 360 | |
---|
[2043] | 361 | /** |
---|
[6959] | 362 | * this method is called by the world if the WorldEntity leaves the game |
---|
[5498] | 363 | */ |
---|
[6959] | 364 | void WorldEntity::leaveWorld () |
---|
[6430] | 365 | {} |
---|
[2043] | 366 | |
---|
[3583] | 367 | |
---|
[2190] | 368 | /** |
---|
[7085] | 369 | * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning |
---|
| 370 | */ |
---|
| 371 | void WorldEntity::reset() |
---|
| 372 | {} |
---|
| 373 | |
---|
| 374 | /** |
---|
[4836] | 375 | * this method is called every frame |
---|
| 376 | * @param time: the time in seconds that has passed since the last tick |
---|
[4885] | 377 | * |
---|
| 378 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
[2043] | 379 | */ |
---|
[4570] | 380 | void WorldEntity::tick(float time) |
---|
[6430] | 381 | {} |
---|
[3583] | 382 | |
---|
[5498] | 383 | |
---|
[3583] | 384 | /** |
---|
[4836] | 385 | * the entity is drawn onto the screen with this function |
---|
[4885] | 386 | * |
---|
| 387 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
| 388 | * Just override this function with whatever you want to be drawn. |
---|
[3365] | 389 | */ |
---|
[5500] | 390 | void WorldEntity::draw() const |
---|
[3803] | 391 | { |
---|
[6430] | 392 | //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName()); |
---|
[6281] | 393 | // assert(!unlikely(this->models.empty())); |
---|
[6002] | 394 | { |
---|
| 395 | glMatrixMode(GL_MODELVIEW); |
---|
| 396 | glPushMatrix(); |
---|
[4570] | 397 | |
---|
[6002] | 398 | /* translate */ |
---|
| 399 | glTranslatef (this->getAbsCoor ().x, |
---|
| 400 | this->getAbsCoor ().y, |
---|
| 401 | this->getAbsCoor ().z); |
---|
[6004] | 402 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 403 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[2043] | 404 | |
---|
[6004] | 405 | |
---|
| 406 | // This Draws the LOD's |
---|
[7014] | 407 | float cameraDistance = State::getCamera()->distance(this); |
---|
[6004] | 408 | if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL) |
---|
[6002] | 409 | { |
---|
[6222] | 410 | this->models[2]->draw(); |
---|
[6004] | 411 | } |
---|
| 412 | else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL) |
---|
[6002] | 413 | { |
---|
| 414 | this->models[1]->draw(); |
---|
[6004] | 415 | } |
---|
| 416 | else if (this->models.size() >= 1 && this->models[0] != NULL) |
---|
[6002] | 417 | { |
---|
| 418 | this->models[0]->draw(); |
---|
| 419 | } |
---|
| 420 | glPopMatrix(); |
---|
| 421 | } |
---|
[3803] | 422 | } |
---|
[3583] | 423 | |
---|
[6430] | 424 | /** |
---|
[6700] | 425 | * @param health the Health to add. |
---|
| 426 | * @returns the health left (this->healthMax - health+this->health) |
---|
[6430] | 427 | */ |
---|
[6700] | 428 | float WorldEntity::increaseHealth(float health) |
---|
[6430] | 429 | { |
---|
[6700] | 430 | this->health += health; |
---|
| 431 | if (this->health > this->healthMax) |
---|
[6430] | 432 | { |
---|
[6700] | 433 | float retHealth = this->healthMax - this->health; |
---|
| 434 | this->health = this->healthMax; |
---|
| 435 | this->updateHealthWidget(); |
---|
| 436 | return retHealth; |
---|
[6430] | 437 | } |
---|
[6700] | 438 | this->updateHealthWidget(); |
---|
[6430] | 439 | return 0.0; |
---|
| 440 | } |
---|
[6281] | 441 | |
---|
[5498] | 442 | /** |
---|
[6700] | 443 | * @param health the Health to be removed |
---|
[6430] | 444 | * @returns 0.0 or the rest, that was not substracted (bellow 0.0) |
---|
| 445 | */ |
---|
[6700] | 446 | float WorldEntity::decreaseHealth(float health) |
---|
[6430] | 447 | { |
---|
[6700] | 448 | this->health -= health; |
---|
[6430] | 449 | |
---|
[6700] | 450 | if (this->health < 0) |
---|
[6430] | 451 | { |
---|
[6700] | 452 | float retHealth = -this->health; |
---|
| 453 | this->health = 0.0f; |
---|
| 454 | this->updateHealthWidget(); |
---|
| 455 | return retHealth; |
---|
[6430] | 456 | } |
---|
[6700] | 457 | this->updateHealthWidget(); |
---|
[6430] | 458 | return 0.0; |
---|
| 459 | |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | /** |
---|
[6700] | 463 | * @param maxHealth the maximal health that can be loaded onto the entity. |
---|
[6430] | 464 | */ |
---|
[6700] | 465 | void WorldEntity::setHealthMax(float healthMax) |
---|
[6430] | 466 | { |
---|
[6700] | 467 | this->healthMax = healthMax; |
---|
| 468 | if (this->health > this->healthMax) |
---|
[6430] | 469 | { |
---|
[6700] | 470 | PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName()); |
---|
| 471 | this->health = this->healthMax; |
---|
[6430] | 472 | } |
---|
[6700] | 473 | this->updateHealthWidget(); |
---|
[6430] | 474 | } |
---|
| 475 | |
---|
[6431] | 476 | /** |
---|
[6700] | 477 | * @brief creates the HealthWidget |
---|
[6431] | 478 | * |
---|
[6700] | 479 | * since not all entities need an HealthWidget, it is only created on request. |
---|
[6431] | 480 | */ |
---|
[6700] | 481 | void WorldEntity::createHealthWidget() |
---|
[6430] | 482 | { |
---|
[6700] | 483 | if (this->healthWidget == NULL) |
---|
[6430] | 484 | { |
---|
[7779] | 485 | this->healthWidget = new OrxGui::GLGuiBar(); |
---|
[6700] | 486 | this->healthWidget->setSize2D(30,400); |
---|
| 487 | this->healthWidget->setAbsCoor2D(10,100); |
---|
[6430] | 488 | |
---|
[6700] | 489 | this->updateHealthWidget(); |
---|
[6430] | 490 | } |
---|
| 491 | else |
---|
[6700] | 492 | PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName()); |
---|
[6430] | 493 | } |
---|
| 494 | |
---|
[6700] | 495 | void WorldEntity::increaseHealthMax(float increaseHealth) |
---|
[6440] | 496 | { |
---|
[6700] | 497 | this->healthMax += increaseHealth; |
---|
| 498 | this->updateHealthWidget(); |
---|
[6440] | 499 | } |
---|
| 500 | |
---|
[6700] | 501 | |
---|
[7779] | 502 | OrxGui::GLGuiWidget* WorldEntity::getHealthWidget() |
---|
[6700] | 503 | { |
---|
| 504 | this->createHealthWidget(); |
---|
| 505 | return this->healthWidget; |
---|
| 506 | } |
---|
| 507 | |
---|
[6431] | 508 | /** |
---|
[6700] | 509 | * @param visibility shows or hides the health-bar |
---|
[6431] | 510 | * (creates the widget if needed) |
---|
| 511 | */ |
---|
[6700] | 512 | void WorldEntity::setHealthWidgetVisibilit(bool visibility) |
---|
[6430] | 513 | { |
---|
[7198] | 514 | if (visibility) |
---|
| 515 | { |
---|
| 516 | if (this->healthWidget != NULL) |
---|
| 517 | this->healthWidget->show(); |
---|
| 518 | else |
---|
[6430] | 519 | { |
---|
[7198] | 520 | this->createHealthWidget(); |
---|
| 521 | this->updateHealthWidget(); |
---|
| 522 | this->healthWidget->show(); |
---|
[6430] | 523 | } |
---|
[7198] | 524 | } |
---|
| 525 | else if (this->healthWidget != NULL) |
---|
| 526 | this->healthWidget->hide(); |
---|
[6430] | 527 | } |
---|
| 528 | |
---|
[6431] | 529 | /** |
---|
[6700] | 530 | * @brief updates the HealthWidget |
---|
[6431] | 531 | */ |
---|
[6700] | 532 | void WorldEntity::updateHealthWidget() |
---|
[6430] | 533 | { |
---|
[6700] | 534 | if (this->healthWidget != NULL) |
---|
[6430] | 535 | { |
---|
[6700] | 536 | this->healthWidget->setMaximum(this->healthMax); |
---|
| 537 | this->healthWidget->setValue(this->health); |
---|
[6430] | 538 | } |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | |
---|
| 542 | /** |
---|
[5498] | 543 | * DEBUG-DRAW OF THE BV-Tree. |
---|
| 544 | * @param depth What depth to draw |
---|
| 545 | * @param drawMode the mode to draw this entity under |
---|
| 546 | */ |
---|
[7711] | 547 | void WorldEntity::drawBVTree(int depth, int drawMode) const |
---|
[4684] | 548 | { |
---|
| 549 | glMatrixMode(GL_MODELVIEW); |
---|
| 550 | glPushMatrix(); |
---|
| 551 | /* translate */ |
---|
| 552 | glTranslatef (this->getAbsCoor ().x, |
---|
| 553 | this->getAbsCoor ().y, |
---|
| 554 | this->getAbsCoor ().z); |
---|
| 555 | /* rotate */ |
---|
[4998] | 556 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 557 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[4684] | 558 | |
---|
[7711] | 559 | |
---|
[4684] | 560 | if (this->obbTree) |
---|
| 561 | this->obbTree->drawBV(depth, drawMode); |
---|
[7711] | 562 | |
---|
| 563 | |
---|
[4684] | 564 | glPopMatrix(); |
---|
| 565 | } |
---|
[6341] | 566 | |
---|
[6424] | 567 | |
---|
[6341] | 568 | /** |
---|
[6424] | 569 | * Debug the WorldEntity |
---|
| 570 | */ |
---|
| 571 | void WorldEntity::debugEntity() const |
---|
| 572 | { |
---|
| 573 | PRINT(0)("WorldEntity %s::%s (DEBUG)\n", this->getClassName(), this->getName()); |
---|
| 574 | this->debugNode(); |
---|
| 575 | PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size()); |
---|
| 576 | for (unsigned int i = 0; i < this->models.size(); i++) |
---|
| 577 | { |
---|
| 578 | if (models[i] != NULL) |
---|
| 579 | PRINT(0)(" : %d:%s", i, this->models[i]->getName()); |
---|
| 580 | } |
---|
| 581 | PRINT(0)("\n"); |
---|
| 582 | |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | |
---|
[6498] | 586 | |
---|
| 587 | |
---|
| 588 | /******************************************************************************************** |
---|
| 589 | NETWORK STUFF |
---|
| 590 | ********************************************************************************************/ |
---|
| 591 | |
---|
| 592 | |
---|
[6424] | 593 | /** |
---|
[6341] | 594 | * Writes data from network containing information about the state |
---|
| 595 | * @param data pointer to data |
---|
| 596 | * @param length length of data |
---|
| 597 | * @param sender hostID of sender |
---|
| 598 | */ |
---|
| 599 | int WorldEntity::writeState( const byte * data, int length, int sender ) |
---|
| 600 | { |
---|
[7230] | 601 | std::string modelFileName; |
---|
[6341] | 602 | SYNCHELP_READ_BEGIN(); |
---|
| 603 | |
---|
[6815] | 604 | SYNCHELP_READ_FKT( PNode::writeState, NWT_WE_PN_WRITESTATE ); |
---|
[6341] | 605 | |
---|
[7230] | 606 | SYNCHELP_READ_STRING( modelFileName, NWT_WE_PN_MODELFILENAME ); |
---|
[6815] | 607 | SYNCHELP_READ_FLOAT( scaling, NWT_WE_PN_SCALING ); |
---|
[6341] | 608 | //check if modelFileName is relative to datadir or absolute |
---|
[6424] | 609 | |
---|
[6695] | 610 | |
---|
[7230] | 611 | PRINTF(0)("================ LOADING MODEL %s, %f\n", modelFileName.c_str(), scaling); |
---|
[6695] | 612 | |
---|
[7230] | 613 | if ( modelFileName != "" ) |
---|
[6341] | 614 | { |
---|
[6695] | 615 | loadModel( modelFileName, scaling); |
---|
| 616 | PRINTF(0)("modelfilename: %s\n", getModel( 0 )->getName()); |
---|
[6341] | 617 | } |
---|
| 618 | |
---|
[6634] | 619 | /*SYNCHELP_READ_STRINGM( modelFileName ); |
---|
[6498] | 620 | |
---|
[6424] | 621 | if ( strcmp(modelFileName, "") ) |
---|
[6498] | 622 | if ( strstr(modelFileName, ResourceManager::getInstance()->getDataDir()) ) |
---|
| 623 | { |
---|
| 624 | this->md2TextureFileName = new char[strlen(modelFileName)-strlen(ResourceManager::getInstance()->getDataDir())+1]; |
---|
| 625 | strcpy((char*)this->md2TextureFileName, modelFileName+strlen(ResourceManager::getInstance()->getDataDir())); |
---|
| 626 | } |
---|
| 627 | else |
---|
| 628 | { |
---|
| 629 | this->md2TextureFileName = modelFileName; |
---|
| 630 | } |
---|
[6634] | 631 | */ |
---|
[6424] | 632 | |
---|
[6341] | 633 | return SYNCHELP_READ_N; |
---|
| 634 | } |
---|
| 635 | |
---|
[6498] | 636 | |
---|
[6341] | 637 | /** |
---|
| 638 | * data copied in data will bee sent to another host |
---|
| 639 | * @param data pointer to data |
---|
| 640 | * @param maxLength max length of data |
---|
| 641 | * @return the number of bytes writen |
---|
| 642 | */ |
---|
| 643 | int WorldEntity::readState( byte * data, int maxLength ) |
---|
| 644 | { |
---|
| 645 | SYNCHELP_WRITE_BEGIN(); |
---|
| 646 | |
---|
[6815] | 647 | SYNCHELP_WRITE_FKT( PNode::readState, NWT_WE_PN_WRITESTATE ); |
---|
[6341] | 648 | |
---|
[7230] | 649 | if ( getModel(0) && getModel(0)->getName() != "" ) |
---|
[6634] | 650 | { |
---|
[7230] | 651 | std::string name = getModel( 0 )->getName(); |
---|
[6634] | 652 | |
---|
[7711] | 653 | if ( name.find( ResourceManager::getInstance()->getDataDir() ) == 0 ) |
---|
[6634] | 654 | { |
---|
[7230] | 655 | name.erase(ResourceManager::getInstance()->getDataDir().size()); |
---|
[6634] | 656 | } |
---|
| 657 | |
---|
[6815] | 658 | SYNCHELP_WRITE_STRING( name, NWT_WE_PN_MODELFILENAME ); |
---|
[6634] | 659 | } |
---|
| 660 | else |
---|
| 661 | { |
---|
[6815] | 662 | SYNCHELP_WRITE_STRING("", NWT_WE_PN_MODELFILENAME); |
---|
[6634] | 663 | } |
---|
| 664 | |
---|
[6815] | 665 | SYNCHELP_WRITE_FLOAT( scaling, NWT_WE_PN_SCALING ); |
---|
[6634] | 666 | /*if ( this->md2TextureFileName!=NULL && strcmp(this->md2TextureFileName, "") ) |
---|
[6424] | 667 | { |
---|
| 668 | SYNCHELP_WRITE_STRING(this->md2TextureFileName); |
---|
| 669 | } |
---|
| 670 | else |
---|
| 671 | { |
---|
| 672 | SYNCHELP_WRITE_STRING(""); |
---|
[7198] | 673 | }*/ |
---|
[6424] | 674 | |
---|
[6341] | 675 | return SYNCHELP_WRITE_N; |
---|
| 676 | } |
---|