Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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