Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability.new/src/world_entities/world_entity.cc @ 10362

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

merged playability. but got strange bug

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