Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

—-

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