Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10149 was 10147, checked in by patrick, 17 years ago

merged the mount_point branche back to trunk to use the new std::* based obj file importer

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