Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/world_entity.cc @ 7444

Last change on this file since 7444 was 7444, checked in by rennerc, 18 years ago

new network system implemented. yet with a lot of empty function bodys

File size: 13.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:
14   main-programmer: Patrick Boenzli
[2190]15   co-programmer: Christian Meyer
[2036]16*/
[5300]17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
[2036]18
19#include "world_entity.h"
[5208]20#include "shell_command.h"
[5143]21
[5511]22#include "model.h"
[6222]23#include "md2Model.h"
[7193]24#include "util/loading/resource_manager.h"
25#include "util/loading/load_param.h"
[3803]26#include "vector.h"
[4682]27#include "obb_tree.h"
[3608]28
[6430]29#include "glgui_bar.h"
30
[6002]31#include "state.h"
[7014]32#include "camera.h"
[6002]33
[2036]34using namespace std;
35
[5208]36SHELL_COMMAND(model, WorldEntity, loadModel)
[6430]37->describe("sets the Model of the WorldEntity")
[7198]38->defaultValues("models/ships/fighter.obj", 1.0);
[5208]39
[6424]40SHELL_COMMAND(debugEntity, WorldEntity, debugWE);
[5208]41
[2043]42/**
[4836]43 *  Loads the WordEntity-specific Part of any derived Class
[5498]44 *
45 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
46 *              that can calls WorldEntities loadParams for itself.
47 */
[6430]48WorldEntity::WorldEntity()
49    : Synchronizeable()
[2190]50{
[4320]51  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
[4597]52
[4682]53  this->obbTree = NULL;
[6700]54  this->healthWidget = NULL;
55  this->healthMax = 1.0f;
56  this->health = 1.0f;
[6695]57  this->scaling = 1.0f;
[4261]58
[6695]59  /* OSOLETE */
60  this->bVisible = true;
61  this->bCollide = true;
62
[6142]63  this->objectListNumber = OM_INIT;
64  this->objectListIterator = NULL;
65
66  this->toList(OM_NULL);
[7444]67 
68  modelFileName_handle = registerVarId( new SynchronizeableString( &modelFileName, &modelFileName, "modelFileName" ) );
69  scaling_handle = registerVarId( new SynchronizeableFloat( &scaling, &scaling, "scaling" ) );
[2190]70}
[2043]71
72/**
[4836]73 *  standard destructor
[2043]74*/
[2190]75WorldEntity::~WorldEntity ()
[2036]76{
[7125]77  State::getObjectManager()->toList(this, OM_INIT);
78
79  // Delete the model (unregister it with the ResourceManager)
80  for (unsigned int i = 0; i < this->models.size(); i++)
81    this->setModel(NULL, i);
82
[5498]83  // Delete the obbTree
[5302]84  if( this->obbTree != NULL)
[4814]85    delete this->obbTree;
[5994]86
[6700]87  if (this->healthWidget != NULL)
88    delete this->healthWidget;
[3531]89}
90
[5498]91/**
92 * loads the WorldEntity Specific Parameters.
93 * @param root: the XML-Element to load the Data From
94 */
[4436]95void WorldEntity::loadParams(const TiXmlElement* root)
96{
[5498]97  // Do the PNode loading stuff
[6512]98  PNode::loadParams(root);
[5498]99
[6222]100  LoadParam(root, "md2texture", this, WorldEntity, loadMD2Texture)
[6430]101  .describe("the fileName of the texture, that should be loaded onto this world-entity. (must be relative to the data-dir)")
[7198]102  .defaultValues("");
[6222]103
[4436]104  // Model Loading
[5671]105  LoadParam(root, "model", this, WorldEntity, loadModel)
[6430]106  .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
[7198]107  .defaultValues("", 1.0f, 0);
[6430]108
[6700]109  LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax)
110  .describe("The Maximum health that can be loaded onto this entity")
[7198]111  .defaultValues(1.0f);
[6430]112
[6700]113  LoadParam(root, "health", this, WorldEntity, setHealth)
114  .describe("The Health the WorldEntity has at this moment")
[7198]115  .defaultValues(1.0f);
[4436]116}
117
[6222]118
[3531]119/**
[4885]120 * loads a Model onto a WorldEntity
[4836]121 * @param fileName the name of the model to load
[5057]122 * @param scaling the Scaling of the model
[7221]123 */
124void WorldEntity::loadModel(const std::string& fileName, float scaling, unsigned int modelNumber)
[4261]125{
[6695]126  this->modelLODName = fileName;
[6424]127  this->scaling = scaling;
[7444]128 
129  std::string name = fileName;
130
131  if (  name.find( ResourceManager::getInstance()->getDataDir() ) == 0 ) 
132  {
133    name.erase(ResourceManager::getInstance()->getDataDir().size());
134  }
135
136  this->modelFileName = name;
137 
[7221]138  if (!fileName.empty())
[6142]139  {
[6430]140    // search for the special character # in the LoadParam
[7221]141    if (fileName.find('#') != std::string::npos)
[6222]142    {
[7221]143      PRINTF(4)("Found # in %s... searching for LOD's\n", fileName.c_str());
144      std::string lodFile = fileName;
145      unsigned int offset = lodFile.find('#');
[6720]146      for (unsigned int i = 0; i < 3; i++)
[6005]147      {
[7221]148        lodFile[offset] = 48+(int)i;
[6222]149        if (ResourceManager::isInDataDir(lodFile))
150          this->loadModel(lodFile, scaling, i);
[6005]151      }
[6222]152      return;
153    }
[6720]154    if (this->scaling <= 0.0)
[6424]155    {
[7193]156      PRINTF(1)("YOU GAVE ME A CRAPY SCALE resetting to 1.0\n");
[6720]157      this->scaling = 1.0;
[6424]158    }
[7221]159    if(fileName.find(".obj") != std::string::npos)
[6222]160    {
[7221]161      PRINTF(4)("fetching OBJ file: %s\n", fileName.c_str());
[7193]162      BaseObject* loadedModel = ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, this->scaling);
163      if (loadedModel != NULL)
164        this->setModel(dynamic_cast<Model*>(loadedModel), modelNumber);
[7221]165      else
166        PRINTF(1)("OBJ-File %s not found.\n", fileName.c_str());
[6222]167
168      if( modelNumber == 0)
169        this->buildObbTree(4);
170    }
[7221]171    else if(fileName.find(".md2") != std::string::npos)
[6222]172    {
[7221]173      PRINTF(4)("fetching MD2 file: %s\n", fileName.c_str());
[7055]174      Model* m = new MD2Model(fileName, this->md2TextureFileName, this->scaling);
[6430]175      //this->setModel((Model*)ResourceManager::getInstance()->load(fileName, MD2, RP_CAMPAIGN), 0);
[6222]176      this->setModel(m, 0);
[7068]177
178      if( m != NULL)
179        this->buildObbTree(4);
[6222]180    }
[4732]181  }
182  else
[6341]183  {
[5995]184    this->setModel(NULL);
[6341]185  }
[4261]186}
187
[5061]188/**
[5994]189 * sets a specific Model for the Object.
190 * @param model The Model to set
191 * @param modelNumber the n'th model in the List to get.
192 */
193void WorldEntity::setModel(Model* model, unsigned int modelNumber)
194{
[5995]195  if (this->models.size() <= modelNumber)
196    this->models.resize(modelNumber+1, NULL);
197
198  if (this->models[modelNumber] != NULL)
[6004]199  {
[7193]200    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(dynamic_cast<BaseObject*>(this->models[modelNumber]));
[7123]201    if (resource != NULL)
202      ResourceManager::getInstance()->unload(resource, RP_LEVEL);
203    else
204    {
205      PRINTF(4)("Forcing model deletion\n");
206      delete this->models[modelNumber];
207    }
[5994]208  }
[6222]209
[5995]210  this->models[modelNumber] = model;
[5994]211
[6222]212
[6430]213  //   if (this->model != NULL)
214  //     this->buildObbTree(4);
[5994]215}
216
217
218/**
[5061]219 * builds the obb-tree
220 * @param depth the depth to calculate
221 */
222bool WorldEntity::buildObbTree(unsigned int depth)
223{
[5428]224  if (this->obbTree)
225    delete this->obbTree;
226
[5995]227  if (this->models[0] != NULL)
[5428]228  {
229    PRINTF(4)("creating obb tree\n");
[5708]230
231
[5995]232    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[5428]233    return true;
234  }
235  else
236  {
237    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
238    this->obbTree = NULL;
239    return false;
240  }
[5061]241}
[5057]242
[6142]243/**
244 * @brief moves this entity to the List OM_List
245 * @param list the list to set this Entity to.
246 *
247 * this is the same as a call to State::getObjectManager()->toList(entity , list);
248 * directly, but with an easier interface.
249 *
250 * @todo inline this (peut etre)
251 */
252void WorldEntity::toList(OM_LIST list)
253{
254  State::getObjectManager()->toList(this, list);
255}
[5061]256
[6142]257
258
[4261]259/**
[4885]260 * sets the character attributes of a worldentity
[4836]261 * @param character attributes
[4885]262 *
263 * these attributes don't have to be set, only use them, if you need them
[2043]264*/
[5498]265//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
266//{}
[2036]267
[3583]268
[2043]269/**
[5029]270 *  this function is called, when two entities collide
271 * @param entity: the world entity with whom it collides
272 *
273 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
274 */
275void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
276{
[5498]277  /**
278   * THIS IS A DEFAULT COLLISION-Effect.
279   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
280   * USE::
281   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
282   *
283   * You can always define a default Action.... don't be affraid just test it :)
284   */
[6430]285  //  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]286}
287
[2043]288
289/**
[5498]290 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]291 *
[5498]292 */
[3229]293void WorldEntity::postSpawn ()
[6430]294{}
[2043]295
[3583]296
[2043]297/**
[6959]298 *  this method is called by the world if the WorldEntity leaves the game
[5498]299 */
[6959]300void WorldEntity::leaveWorld ()
[6430]301{}
[2043]302
[3583]303
[2190]304/**
[7085]305 * resets the WorldEntity to its initial values. eg. used for multiplayer games: respawning
306 */
307void WorldEntity::reset()
308{}
309
310/**
[4836]311 *  this method is called every frame
312 * @param time: the time in seconds that has passed since the last tick
[4885]313 *
314 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]315*/
[4570]316void WorldEntity::tick(float time)
[6430]317{}
[3583]318
[5498]319
[3583]320/**
[4836]321 *  the entity is drawn onto the screen with this function
[4885]322 *
323 * This is a central function of an entity: call it to let the entity painted to the screen.
324 * Just override this function with whatever you want to be drawn.
[3365]325*/
[5500]326void WorldEntity::draw() const
[3803]327{
[6430]328  //PRINTF(0)("(%s::%s)\n", this->getClassName(), this->getName());
[6281]329  //  assert(!unlikely(this->models.empty()));
[6002]330  {
331    glMatrixMode(GL_MODELVIEW);
332    glPushMatrix();
[4570]333
[6002]334    /* translate */
335    glTranslatef (this->getAbsCoor ().x,
336                  this->getAbsCoor ().y,
337                  this->getAbsCoor ().z);
[6004]338    Vector tmpRot = this->getAbsDir().getSpacialAxis();
339    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[2043]340
[6004]341
342    // This Draws the LOD's
[7014]343    float cameraDistance = State::getCamera()->distance(this);
[6004]344    if (cameraDistance > 30 && this->models.size() >= 3 && this->models[2] != NULL)
[6002]345    {
[6222]346      this->models[2]->draw();
[6004]347    }
348    else if (cameraDistance > 10 && this->models.size() >= 2 && this->models[1] != NULL)
[6002]349    {
350      this->models[1]->draw();
[6004]351    }
352    else if (this->models.size() >= 1 && this->models[0] != NULL)
[6002]353    {
354      this->models[0]->draw();
355    }
356    glPopMatrix();
357  }
[3803]358}
[3583]359
[6430]360/**
[6700]361 * @param health the Health to add.
362 * @returns the health left (this->healthMax - health+this->health)
[6430]363 */
[6700]364float WorldEntity::increaseHealth(float health)
[6430]365{
[6700]366  this->health += health;
367  if (this->health > this->healthMax)
[6430]368  {
[6700]369    float retHealth = this->healthMax - this->health;
370    this->health = this->healthMax;
371    this->updateHealthWidget();
372    return retHealth;
[6430]373  }
[6700]374  this->updateHealthWidget();
[6430]375  return 0.0;
376}
[6281]377
[5498]378/**
[6700]379 * @param health the Health to be removed
[6430]380 * @returns 0.0 or the rest, that was not substracted (bellow 0.0)
381 */
[6700]382float WorldEntity::decreaseHealth(float health)
[6430]383{
[6700]384  this->health -= health;
[6430]385
[6700]386  if (this->health < 0)
[6430]387  {
[6700]388    float retHealth = -this->health;
389    this->health = 0.0f;
390    this->updateHealthWidget();
391    return retHealth;
[6430]392  }
[6700]393  this->updateHealthWidget();
[6430]394  return 0.0;
395
396}
397
398/**
[6700]399 * @param maxHealth the maximal health that can be loaded onto the entity.
[6430]400 */
[6700]401void WorldEntity::setHealthMax(float healthMax)
[6430]402{
[6700]403  this->healthMax = healthMax;
404  if (this->health > this->healthMax)
[6430]405  {
[6700]406    PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName());
407    this->health = this->healthMax;
[6430]408  }
[6700]409  this->updateHealthWidget();
[6430]410}
411
[6431]412/**
[6700]413 * @brief creates the HealthWidget
[6431]414 *
[6700]415 * since not all entities need an HealthWidget, it is only created on request.
[6431]416 */
[6700]417void WorldEntity::createHealthWidget()
[6430]418{
[6700]419  if (this->healthWidget == NULL)
[6430]420  {
[6700]421    this->healthWidget = new GLGuiBar();
422    this->healthWidget->setSize2D(30,400);
423    this->healthWidget->setAbsCoor2D(10,100);
[6430]424
[6700]425    this->updateHealthWidget();
[6430]426  }
427  else
[6700]428    PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName());
[6430]429}
430
[6700]431void WorldEntity::increaseHealthMax(float increaseHealth)
[6440]432{
[6700]433  this->healthMax += increaseHealth;
434  this->updateHealthWidget();
[6440]435}
436
[6700]437
438GLGuiWidget* WorldEntity::getHealthWidget()
439{
440  this->createHealthWidget();
441  return this->healthWidget;
442}
443
[6431]444/**
[6700]445 * @param visibility shows or hides the health-bar
[6431]446 * (creates the widget if needed)
447 */
[6700]448void WorldEntity::setHealthWidgetVisibilit(bool visibility)
[6430]449{
[7198]450  if (visibility)
451  {
452    if (this->healthWidget != NULL)
453      this->healthWidget->show();
454    else
[6430]455    {
[7198]456      this->createHealthWidget();
457      this->updateHealthWidget();
458      this->healthWidget->show();
[6430]459    }
[7198]460  }
461  else if (this->healthWidget != NULL)
462    this->healthWidget->hide();
[6430]463}
464
[6431]465/**
[6700]466 * @brief updates the HealthWidget
[6431]467 */
[6700]468void WorldEntity::updateHealthWidget()
[6430]469{
[6700]470  if (this->healthWidget != NULL)
[6430]471  {
[6700]472    this->healthWidget->setMaximum(this->healthMax);
473    this->healthWidget->setValue(this->health);
[6430]474  }
475}
476
477
478/**
[5498]479 * DEBUG-DRAW OF THE BV-Tree.
480 * @param depth What depth to draw
481 * @param drawMode the mode to draw this entity under
482 */
[5501]483void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]484{
485  glMatrixMode(GL_MODELVIEW);
486  glPushMatrix();
487  /* translate */
488  glTranslatef (this->getAbsCoor ().x,
489                this->getAbsCoor ().y,
490                this->getAbsCoor ().z);
491  /* rotate */
[4998]492  Vector tmpRot = this->getAbsDir().getSpacialAxis();
493  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]494
495  if (this->obbTree)
496    this->obbTree->drawBV(depth, drawMode);
497  glPopMatrix();
498}
[6341]499
[6424]500
[6341]501/**
[6424]502 * Debug the WorldEntity
503 */
504void WorldEntity::debugEntity() const
505{
506  PRINT(0)("WorldEntity %s::%s  (DEBUG)\n", this->getClassName(), this->getName());
507  this->debugNode();
508  PRINT(0)("List: %s ; ModelCount %d - ", ObjectManager::OMListToString(this->objectListNumber) , this->models.size());
509  for (unsigned int i = 0; i < this->models.size(); i++)
510  {
511    if (models[i] != NULL)
512      PRINT(0)(" : %d:%s", i, this->models[i]->getName());
513  }
514  PRINT(0)("\n");
515
516}
517
518
519/**
[7444]520 * handler for changes on registred vars
521 * @param id id's which changed
[6341]522 */
[7444]523void WorldEntity::varChangeHandler( std::list< int > & id )
[6341]524{
[7444]525#warning implement this
[6341]526}
Note: See TracBrowser for help on using the repository browser.