Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mount_points/src/world_entities/world_entity.cc @ 10243

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

still on this resource thingy

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