Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10262 was 10209, checked in by bknecht, 18 years ago

track updates

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