Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the single_player branche to trunk

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