Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/world_entity.cc @ 9836

Last change on this file since 9836 was 9836, checked in by bensch, 18 years ago

orxonox/new_class_id: Taken out the old ResourceManager.
On the way, i had to desintegrate the old MD3-model loading process (via ResourceManager) MD3 is untouched, but also not loaded anymore neither from ResourceMangers nor from the WorldEntity

@patrick: MD3-ModelLoading class must be implemented… this should be quite easy, with the way MD3 is, and the new Resource-paradigm
cheers

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