Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/world_entity.cc @ 9656

Last change on this file since 9656 was 9656, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy bache back with no conflicts

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