Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10099 was 10096, checked in by bknecht, 17 years ago

connected the track with the worldentities and the spaceship

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