Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/world_entity.cc @ 10772

Last change on this file since 10772 was 10698, checked in by snellen, 17 years ago

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

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