Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

huge diff
cleaned the individual weapons, moved stuff to weapon.{cc,h}
and some minor fixes which popped up then and when

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