Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/world_entity.cc @ 10742

Last change on this file since 10742 was 10742, checked in by nicolasc, 17 years ago

moved loadHealth to WE

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