Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/single_player_map/src/world_entities/world_entity.cc @ 8940

Last change on this file since 8940 was 8940, checked in by patrick, 18 years ago

falling down fps player

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