Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5996 was 5996, checked in by patrick, 18 years ago

orxonox/trunk: merged network branche into trunk with command svn merge -r 5824:HEAD

File size: 6.8 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"
[5143]23#include "resource_manager.h"
24#include "load_param.h"
[3608]25#include "list.h"
[3803]26#include "vector.h"
[4682]27#include "obb_tree.h"
[3608]28
[2036]29using namespace std;
30
[5208]31SHELL_COMMAND(model, WorldEntity, loadModel)
32    ->describe("sets the Model of the WorldEntity")
[5555]33    ->defaultValues(2, "models/ships/fighter.obj", 1.0);
[5208]34
35
[2043]36/**
[4836]37 *  Loads the WordEntity-specific Part of any derived Class
[5498]38 *
39 * @param root: Normally NULL, as the Derived Entities define a loadParams Function themeselves,
40 *              that can calls WorldEntities loadParams for itself.
41 */
[4261]42WorldEntity::WorldEntity(const TiXmlElement* root)
[5996]43  : Synchronizeable()
[2190]44{
[4320]45  this->setClassID(CL_WORLD_ENTITY, "WorldEntity");
[4597]46
[4682]47  this->obbTree = NULL;
[4261]48
[5995]49  if (root != NULL)
[4261]50    this->loadParams(root);
51
[4885]52  this->setVisibiliy(true);
[2190]53}
[2043]54
55/**
[4836]56 *  standard destructor
[2043]57*/
[2190]58WorldEntity::~WorldEntity ()
[2036]59{
[5498]60  // Delete the obbTree
[5302]61  if( this->obbTree != NULL)
[4814]62    delete this->obbTree;
[5994]63
64  // Delete the model (unregister it with the ResourceManager)
65  this->setModel(NULL);
[3531]66}
67
[5498]68/**
69 * loads the WorldEntity Specific Parameters.
70 * @param root: the XML-Element to load the Data From
71 */
[4436]72void WorldEntity::loadParams(const TiXmlElement* root)
73{
[5498]74  // Do the PNode loading stuff
[4436]75  static_cast<PNode*>(this)->loadParams(root);
[5498]76
[4436]77  // Model Loading
[5671]78  LoadParam(root, "model", this, WorldEntity, loadModel)
[5652]79      .describe("the fileName of the model, that should be loaded onto this world-entity. (must be relative to the data-dir)")
[5995]80      .defaultValues(3, NULL, 1.0f, 0);
[5465]81
[4436]82}
83
[3531]84/**
[4885]85 * loads a Model onto a WorldEntity
[4836]86 * @param fileName the name of the model to load
[5057]87 * @param scaling the Scaling of the model
[5498]88 *
89 * @todo fix this, so it only has one loadModel-Function.
[4261]90*/
[5995]91void WorldEntity::loadModel(const char* fileName, float scaling, unsigned int modelNumber)
[4261]92{
[4732]93  if (fileName != NULL)
94  {
[5066]95    PRINTF(4)("fetching %s\n", fileName);
[5057]96    if (scaling == 1.0)
[5994]97      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN));
[5057]98    else
[5994]99      this->setModel((Model*)ResourceManager::getInstance()->load(fileName, OBJ, RP_CAMPAIGN, &scaling));
[5057]100
[5061]101    this->buildObbTree(4);
[4732]102  }
103  else
[5995]104    this->setModel(NULL);
[4261]105}
106
[5061]107/**
[5994]108 * sets a specific Model for the Object.
109 * @param model The Model to set
110 * @param modelNumber the n'th model in the List to get.
111 */
112void WorldEntity::setModel(Model* model, unsigned int modelNumber)
113{
[5995]114  if (this->models.size() <= modelNumber)
115    this->models.resize(modelNumber+1, NULL);
116
117  if (this->models[modelNumber] != NULL)
[5994]118  { 
[5995]119    Resource* resource = ResourceManager::getInstance()->locateResourceByPointer(this->models[modelNumber]);
[5994]120    if (resource != NULL)
121      ResourceManager::getInstance()->unload(resource, RP_LEVEL);
122    else
[5995]123      delete this->models[modelNumber];
[5994]124  }
[5995]125  this->models[modelNumber] = model;
[5994]126
127//   if (this->model != NULL) 
128//     this->buildObbTree(4);
129}
130
131
132/**
[5061]133 * builds the obb-tree
134 * @param depth the depth to calculate
135 */
136bool WorldEntity::buildObbTree(unsigned int depth)
137{
[5428]138  if (this->obbTree)
139    delete this->obbTree;
140
[5995]141  if (this->models[0] != NULL)
[5428]142  {
143    PRINTF(4)("creating obb tree\n");
[5708]144
145
[5995]146    this->obbTree = new OBBTree(depth, (sVec3D*)this->models[0]->getVertexArray(), this->models[0]->getVertexCount());
[5428]147    return true;
148  }
149  else
150  {
151    PRINTF(2)("could not create obb-tree, because no model was loaded yet\n");
152    this->obbTree = NULL;
153    return false;
154  }
[5061]155}
[5057]156
[5061]157
[4261]158/**
[4885]159 * sets the character attributes of a worldentity
[4836]160 * @param character attributes
[4885]161 *
162 * these attributes don't have to be set, only use them, if you need them
[2043]163*/
[5498]164//void WorldEntity::setCharacterAttributes(CharacterAttributes* charAttr)
165//{}
[2036]166
[3583]167
[2043]168/**
[5029]169 *  this function is called, when two entities collide
170 * @param entity: the world entity with whom it collides
171 *
172 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
173 */
174void WorldEntity::collidesWith(WorldEntity* entity, const Vector& location)
175{
[5498]176  /**
177   * THIS IS A DEFAULT COLLISION-Effect.
178   * IF YOU WANT TO CREATE A SPECIFIC COLLISION ON EACH OBJECT
179   * USE::
180   * if (entity->isA(CL_WHAT_YOU_ARE_LOOKING_FOR)) { printf "dothings"; };
181   *
182   * You can always define a default Action.... don't be affraid just test it :)
183   */
[5257]184//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
[5029]185}
186
[2043]187
188/**
[5498]189 *  this is called immediately after the Entity has been constructed, initialized and then Spawned into the World
[4885]190 *
[5498]191 */
[3229]192void WorldEntity::postSpawn ()
[2190]193{
194}
[2043]195
[3583]196
[2043]197/**
[4836]198 *  this method is called by the world if the WorldEntity leaves valid gamespace
[4885]199 *
200 * For free entities this means it left the Track boundaries. With bound entities it means its Location adresses a
201 * place that is not in the world anymore. In both cases you might have to take extreme measures (a.k.a. call destroy).
[5498]202 *
203 * NOT YET IMPLEMENTED
204 */
[3583]205void WorldEntity::leftWorld ()
[2190]206{
207}
[2043]208
[3583]209
[2190]210/**
[4836]211 *  this method is called every frame
212 * @param time: the time in seconds that has passed since the last tick
[4885]213 *
214 * Handle all stuff that should update with time inside this method (movement, animation, etc.)
[2043]215*/
[4570]216void WorldEntity::tick(float time)
[2190]217{
218}
[3583]219
[5498]220
[3583]221/**
[4836]222 *  the entity is drawn onto the screen with this function
[4885]223 *
224 * This is a central function of an entity: call it to let the entity painted to the screen.
225 * Just override this function with whatever you want to be drawn.
[3365]226*/
[5500]227void WorldEntity::draw() const
[3803]228{
229  glMatrixMode(GL_MODELVIEW);
230  glPushMatrix();
231  float matrix[4][4];
[4570]232
[3803]233  /* translate */
[4570]234  glTranslatef (this->getAbsCoor ().x,
235                this->getAbsCoor ().y,
236                this->getAbsCoor ().z);
[5435]237  /* rotate */ // FIXME: devise a new Way to rotate this
[3803]238  this->getAbsDir ().matrix (matrix);
239  glMultMatrixf((float*)matrix);
[2043]240
[5995]241  if (this->models[0])
242    this->models[0]->draw();
[3803]243  glPopMatrix();
244}
[3583]245
[5498]246/**
247 * DEBUG-DRAW OF THE BV-Tree.
248 * @param depth What depth to draw
249 * @param drawMode the mode to draw this entity under
250 */
[5501]251void WorldEntity::drawBVTree(unsigned int depth, int drawMode) const
[4684]252{
253  glMatrixMode(GL_MODELVIEW);
254  glPushMatrix();
255  /* translate */
256  glTranslatef (this->getAbsCoor ().x,
257                this->getAbsCoor ().y,
258                this->getAbsCoor ().z);
259  /* rotate */
[4998]260  Vector tmpRot = this->getAbsDir().getSpacialAxis();
261  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[4684]262
263  if (this->obbTree)
264    this->obbTree->drawBV(depth, drawMode);
265  glPopMatrix();
266}
Note: See TracBrowser for help on using the repository browser.